Explanatory part
I recently came across an article in Code magazine called "Comparison: Classes vs. Functions . " I read it and it seemed to me ... strange. The magazine positions itself as a publication for novice programmers. But even with a discount on audience level, the article raises many questions.
This publication is an attempt to contact the editorial staff of Code magazine. I am by no means writing a hate post. On the contrary, my goal is to disassemble the article and point out the shortcomings. I do not want to offend either the author or the editorial board. I admit that nothing will change in the current article, but maybe the editors will take note of something.
Let me emphasize that throughout the text I am considering the article through the eyes of a beginner . I have modest experience in this: I did programming lessons, wrote tutorials, and do mentoring. Therefore, I find fault with only one criterion - how the beginner programmer will learn the information . It seems to me that this coincides with the subject matter of the publication.
The editorial office advised me to write my thoughts in the comments. However, the text came out too large to fit. And secondly, I have a feeling that comments are being ignored, which I will talk about separately.
For completeness of context, read the original, it shouldn't take long.
What's wrong in the article
So, the purpose of the article is to compare two paradigms: procedural style and OOP. Quite a good topic for beginners. What went wrong?
Two languages
The first mistake is that the author uses two languages: Python and JavaScript. What's the point of this? On the contrary, the comparison must take place within the framework of one language, so that the difference can be clearly seen. Another language is context switching and syntax differences. It's like comparing two programs for a phone, but running one on an iPhone and the other on an Android, overlooking the fact that platform differences can be dramatic.
, β . β - , , . , , , . , , β , JavaScrip .
, JavaScript? , :
, JS . JS : private- ; β , . Python : . JavaScript, , . , .
. " ", , . , , .
β , . :
class User:
user_count = 0
def __init__(self, name, age, adress):
self.name = name
self.age = age
self.adress = adress
user.user_count += 1
user_count
. . , . user_count
: . ?
, . user_count
, . __init__
, self
. , , .
, , .
, , β . , , . , , :
user1 = User("ivan", 20, "addr1")
user2 = User("huan", 30, "addr2")
user3 = User("juan", 40, "addr3")
print(User.user_count)
# 3
, . , , del
. :
del user3
print(User.user_count)
# 3
:
def __del__(self):
User.user_count -= 1
del user3
print(User.user_count)
# 2
, . ? . ? . , .
, ? , β . len
. :
users = [user1, user2, user3]
print(len(users))
# 3
users.remove(user3)
print(len(users))
# 2
, : :
//
user1 = ['', 23, ''];
//
user2 = ['', 19, ''];
. β , ( JS):
var user1 = {
name: '',
age: 23,
adress: ''
};
var user2 = {
name: '',
age: 19,
adress: ''
};
, "" , . β , .
, . :
function user1_add_bonus(bonus_count) {
user1[3] += bonus_count;
user1[4] = Math.floor(user1[3]/10000);
if (user1[4] > 3) {
user1[4] = 3;
}
console.log(' ', user1[0], ' : ', user1[4])
}
function user2_add_bonus(bonus_count) {
user2[3] += bonus_count;
user2[4] = Math.floor(user2[3]/10000);
if (user2[4] > 3) {
user2[4] = 3;
}
console.log(' ', user2[0], ' : ', user2[4])
}
, . β . . , :
function user_add_bonus(user, bonus_count) {
user.bouns_count += bonus_count;
user.bonus_level = Math.floor(user.bouns_count / 10000);
if (user.bonus_level > 3) {
user.bonus_level = 3;
}
console.log(' ', user.name, ' : ', user.bonus_level)
}
, . user1_add_bonus
- , NaN
- . , 3 4:
[ "", 23, "", NaN, NaN ]
, , . , β ? , . -, . ", ?"
, , , β . , . β , , .
, user1_add_bonus
user2_add_bonus
. . , user1
user2
. :
β . , , 10.
, β . , , - . , , user
.
: :
function user1_add_bonus(bonus_count) {
user1[3] += bonus_count;
user1[4] = Math.floor(user1[3]/10000);
if (user1[4] > 3) {
user1[4] = 3;
}
console.log(' ', user1[0], ' : ', user1[4])
}
function user2_add_bonus(bonus_count) {
user2[3] += bonus_count;
user2[4] = Math.floor(user2[3]/10000);
if (user2[4] > 3) {
user2[4] = 3;
}
console.log(' ', user2[0], ' : ', user2[4])
}
function user3_add_bonus(bonus_count) {
user3[3] += bonus_count;
user3[4] = Math.floor(user3[3]/10000);
if (user3[4] > 3) {
user3[4] = 3;
}
console.log(' ', user3[0], ' : ', user3[4])
}
function user4_add_bonus(bonus_count) {
user4[3] += bonus_count;
user4[4] = Math.floor(user4[3]/10000);
if (user4[4] > 3) {
user4[4] = 3;
}
console.log(' ', user4[0], ' : ', user4[4])
}
function user5_add_bonus(bonus_count) {
user5[3] += bonus_count;
user5[4] = Math.floor(user5[3]/10000);
if (user5[4] > 3) {
user5[4] = 3;
}
console.log(' ', user5[0], ' : ', user5[4])
}
//
user1 = ['',23,'',0,0];
// 15000
user1_add_bonus(15000);
//
user2 = ['',19,'',3000,0];
// 5000
user2_add_bonus(5000);
//
user3 = ['',31,'',0,1]
//
user4 = ['',45,'',5000,2];
//
user5 = ['',32,'',8000,1];
// 10000
user5_add_bonus(10000);
:
, 20%, 5 . 5, . , β . : , . , . 100 , .
, . , , ? , , . , , , .
, , . . .
. :
class User1:
def __init__(self, name, age, adress):
self.name = name
self.age = age
self.adress = adress
class User2:
def __init__(self, name, age, adress):
self.name = name
self.age = age
self.adress = adress
# ...
user1 = User1(...)
user2 = User2(...)
user3 = User3(...)
user4 = User4(...)
user5 = User5(...)
# ...
! , ? , . , β .
, . , . β - : . β " " β .
, . , .
, β , , . . , , . , β ( β ), . β .
, . - : User
user
. Javascript .
: - , . , . .
: Javascript ,
__
(. ). : . , , .
, . Clean NPC :
β .
Emil Orekhov:
. , , , , ?
:
, . . user .
:
, , : user.user_count += 1
, user β User :)
.
- , , ? , ?
- Why is the editorial board ignoring comments? I understand that you will not please everyone, especially in terms of style and naming. But readers point out obvious mistakes. The comments are already six months old, but nothing has changed in the article.
- And the last one - it seems to me that the material from the article is more likely to harm the reader, because it is misleading. What does the editors think about this?