开源日报 每天推荐一个 GitHub 优质开源项目和一篇精选英文科技或编程文章原文,坚持阅读《开源日报》,保持每日学习的好习惯。
今日推荐开源项目:《公务员 coder2gwy》
今日推荐英文原文:《Stop Using Magic Numbers and Variables in Your Code》
开源日报第1019期:《公务员 coder2gwy》
今日推荐开源项目:《公务员 coder2gwy》传送门:项目链接
推荐理由:这个项目是作者结合亲身经历写的程序员考公指南,不管是因为工作方式还是环境等等各种原因,想要离开这一行另寻出路的话,考公务员也并非不可能,尽管需要大量时间准备,过程也相对繁琐,不过读完之后想到作者上岸成功之后的种种好处……
今日推荐英文原文:《Stop Using Magic Numbers and Variables in Your Code》作者:Martin Andersson Aaberge
原文链接:https://medium.com/better-programming/stop-using-magic-numbers-and-variables-in-your-code-4e86f008b84c
推荐理由:尽可能使用对所有人而不是只是现在的自己有意义的写法

Stop Using Magic Numbers and Variables in Your Code

No one will understand your program. Not even you

Have you ever scratched your head reading code you didn’t write yourself? Of course you have. You’re not a bad programmer who can’t read code. Maybe the programmer who wrote the code was a magician — the wrong kind of magician. Keep this quote in the back of your head while coding.
“Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.” — John Wood
Many programs suffer from the fact that they aren’t written with another reader in mind. If you don’t have the violent psychopath in the back of your head while writing code, chances are some bad code will sneak in. You might have a hard time reading your own code as well after a while unless you write code that everyone will understand forever.

What Is Magic?

Please don’t confuse the magic we are talking about today with magic methods (Dunder methods). You have most likely used init as an initializer for your class. Have you tried using str to control what happens when you print your object? The magic we don’t like is magic variables and magic numbers.

Magic variables

A magic variable has a variable name that does not reflect what it represents. The name is complete gibberish, and it isn’t meaningful to the reader. The following code declares a magic variable: a can be anything. Sure, it’s a list ranging from 1–6, but how will we use it? Grades, doors in the room, leftovers in the fridge? There is no way you can know what this code is supposed to do. The code was all about dice, and the code created a list based on the number of faces the die had. Now it makes sense. Can you see a way to improve the code above? It is limited to a standard die. Because we are used to the dice with six faces, we are OK with this code. However, in the world of dice, you have a range of options. I bet you have seen a matrix created like this: A tiny adjustment turning i into row and j into column can go a long way.

Magic numbers

Consider the following code: The code can make sense because you understand it is about the drinking age. The code does not read well because x should have been age and y should have been legal_age. It could look something like this: As I said, this is not the worst offender of magic numbers. It becomes harder to understand if you use a number in the middle of an expression. Only a trained eye will catch the conversion to inches. Especially if this only relates to a small part of the code, where the rest uses metric numbers. Without any comments or a proper variable name, this will be hard to understand for many programmers.

There’s a library for that

In some cases, you can use libraries. We all know that pi is roughly 3.14. Because we talk about pi as 3.14 in our daily speech, it is tempting to use this value in our code. The code above returns 498.5064. The code and result look fine because we can assume that everyone knows that pi is 3.14, but it might not be good enough in your case. There is no confusion about what we are trying to say with our code using the math library, and it is more accurate. The code above returns 498.7592496839155.

Why Are We Implementing Bad Coding Habits?

In all honesty, it’s not all your fault. How many times have you seen courses where you do nested loops with j and k, loops with i, the parameter n in functions, and variable names like a, b or c? This does not happen on YouTube only. It happens all the time at universities and other locations where you learn programming. Most coding tutorials I have watched will, at some point, use magic variables and magic numbers in lessons. The main reason for this is that you don’t want to waste time watching someone make up good variable names. A real-time explanation is more forgiving. A poor variable name doesn’t hurt the teaching at that moment. However, code is often supplied as extra material, making it harder to understand on its own later. When you want to show someone a quick example, it is easy to make variable names that are meaningless and impossible to read later. After a while, you start to slip these into your real code, and you have begun implementing your bad habits into your daily work. A big red flag should be waving at that point.

Final Thoughts

Let’s make a deal. It’s OK to use poor variable names when you discuss a snippet of code with your peers. It’s OK to do this when you test your code. Hopefully, you will have time to refactor it later. (We know you never have time to refactor. Let’s not fool ourselves.) When you write code others will use or code you will go back to in a year to maintain, please keep the magic out of it. Of course, if your code is magical, that’s great. If it is full of magic variables, it’s not. Let’s kill the wrong kind of magic and keep the right one in our code moving forward. Thanks.
下载开源日报APP:https://openingsource.org/2579/
加入我们:https://openingsource.org/about/join/
关注我们:https://openingsource.org/about/love/