開源日報 每天推薦一個 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/