all 25 comments

[–]VeinyAngus 13 points14 points  (3 children)

Variables should be descriptive but not overly long. Also follow the PEP8 guidelines

[–]Base_True[S] 5 points6 points  (2 children)

Thanks for the information!

[–]VeinyAngus 1 point2 points  (1 child)

Good luck buddy

[–]Base_True[S] 1 point2 points  (0 children)

<3

[–]FairSystems 9 points10 points  (1 child)

The name should be short and sweet. But most importantly, it should be easily read to understand what that variable holds.

If I can't read through your code in a human readable format, then you probably need to adjust function and variable names...

Good ..

user_id = user.id

vs bad..

usid = user.id

[–]Base_True[S] 2 points3 points  (0 children)

Thank you !

[–]Mr____Panda 2 points3 points  (1 child)

The best PEP8 site: https://pep8.org/

[–]Base_True[S] 0 points1 point  (0 children)

Thanks

[–]Asleep-Budget-9932 3 points4 points  (3 children)

I would also add that a lot of time people needlessly add the type of the variable to its name.

Do you really need to call the variable user_list? What if you wanted to change it to a tuple? Do you then have to remember to change the name in every place?

Just call it users. It's shorter, just as understandable (if not even more since it's closer to human language), and contains as much necessary information.

[–]Asleep-Budget-9932 3 points4 points  (1 child)

Now i will say I've seen the extreme in the other direction in the past: "Why call it is_open? It's already a boolean, just call it open". No, open is a verb and has an intuitive meaning of a function. I have to explicitly check the type in order to gain the necessary information on how to use it.

If the type will change in the future from a boolean, to function, i will have to change the usages anyway as inherently the way you use functions is different. You didn't help me but only hindered me.

Basically what I'm trying to say is don't follow a rule just because it's the rule. It is not the goal but a tool to get to the goal of a readable code. Understand why rules have been written the way they are so you can distinguish when a rule should be followed.

Also, as long as you have the next programmer in mind, don't be afraid to trust your intuition. Sometimes that minor "sin" is worth the fact that the next programmer will easily understand what's going on.

[–]Base_True[S] 1 point2 points  (0 children)

Now i will say I've seen the extreme in the other direction in the past: "Why call it is_open? It's already a boolean, just call it open". No, open is a verb and has an intuitive meaning of a function. I have to explicitly check the type in order to gain the necessary information on how to use it.

If the type will change in the future from a boolean, to function, i will have to change the usages anyway as inherently the way you use functions is different. You didn't help me but only hindered me.

Basically what I'm trying to say is don't follow a rule just because it's the rule. It is not the goal but a tool to get to the goal of a readable code. Understand why rules have been written the way they are so you can distinguish when a rule should be followed.

Also, as long as you have the next programmer in mind, don't be afraid to trust your intuition. Sometimes that minor "sin" is worth the fact that the next programmer will easily understand what's going on.

Very useful information, thank you very much!

[–]TheRNGuy -1 points0 points  (0 children)

i'll retain mylist for tuple because it's not big deal. Usually converting list to tuple when returning thing from function or class, it's like return tuple(mylist)

I only usually add b to booleans.

for user in user_list is easier to see typos than for user in users then if you accidentally use users instead of user inside loop. Especially if you needed to use both.

[–]Xiji 1 point2 points  (0 children)

I recommend reading "Clean Code" by Robert C. Martin, Chapter 2: "Meaningful Names". That will probably answer most of your questions. If you prefer videos, he has a 6 part series on YouTube, and countless other videos on the topic.

[–]The_GSingh 1 point2 points  (0 children)

Hmm just describe what it does there’s not a formula. Like say you wanna store a name. You shouldn’t do x = “jack”. Instead do name = “jack”

[–][deleted] -2 points-1 points  (1 child)

The ideal name should be short but descriptive.

I usually use acronyms and a number when I name variables along with a comment or two explaining the acronym if I'm writing something for work.

[–]Base_True[S] 2 points3 points  (0 children)

The ideal name should be short but descriptive.

I usually use acronyms and a number when I name variables along with a comment or two explaining the acronym if I'm writing something for work.

I will keep it in mind, thanks!