Please for the love of Ritchie, don't do this. What happened to the Pythonersisto who made this? What did they live through?
Doesn't clean code demand descriptive variable names?
For i in range(4):
eval(f”VAL_{i+1} = {i}”)
for i in range(4):
glboals()[f'VAL_{i+1}'] = i
The his is the way, and its code safe
*exec, eval returns an expression
We found the JS dev
That’s the worst slur anyone has ever called me
The valiant .NET engineer VS the perfidious JS developer
This is not even clean code. Do the names tell you what they mean by the position in the array/list?
This is bad, because you might think you only need up to the 4th index when you write it, but you could end up needing the 5th later and you'll be tempted to put in a magic number at that point. Better to use an array:
vals = []
vals.append(None) # blank out 0 so we can start at 1
for i in range(1, 2**63-1):
vals.append(i - 1)
What about using `VAL_4 + VAL_2`?
Sure, you just need to remember to add another VAL_1 for each operand you add to handle the offsets by 1. Works great though, lgtm!
But what if they want to change the value of VAL_1
later? Now we only have to make the change in one place. lol I can almost see the code review comments that led to this.
Changing VAL_1, specifically, will often crash at runtime because there are two paths where it is used to index a one element array. That decision seems to have been made to allow the code to be more compact when it is called with different arguments
PirateSoftware is that you?
wait till all those haters discover that lisps (i saw it in clojure and this one is much less 'let's put random things in' than sbcl and etc) actually do that and you can access up to 10th with (fifth array)
A die-hard clean code purist wouldn't use abbreviations like "VAL" when "VALUE" is only two extra letters.
ONE = 0 TWO = 1 THREE = 2 FOUR = 3
Python 3.13 supports enumeration… would have been much cleaner
This is how i feel about the fact that 1900's = 20th century
1900 is still part of the 19th century though.
Even worse
yeah both centuries and years start at 1. So on 1st January 2000, only 1999 years had elapsed since the origin of the calendar
Ah yes, the classic off-by-one error: Baby's first nightmare in programming. 😂😂
Real code that I found in a Production service at my job:
public static final int ONE = 1;
This isn't about clean code. This is written by someone who was told not to use "magic numbers," but didn't understand what that means or why.
100% true, but I still appreciate this junior’s attempt at conveying “these are the only four indices this code will use.” Still better than nothing.
Put the indices into a list so that you can easily index the indices later without hassle.
Gonna need to declare an enum to access the list items
Possibly told by a badly written linter.
*Cough, cough, cough, Pylint, cough cough.*
Oh pylint....
I love to use it, but I have to disable 10-15 warning types.
I'm curious which. I only found "line too long" overly annoying, especially when using SQLite.
Would you mind sharing which ones? I’ve had thought about it quite a bit but not sure which ones are „fine to disable“.
Six months into my first job my senior developer told me to replace integers with constants like this.
Even then I knew it was dumb.
What are magic numbers in this context?
Any number where it isn’t immediately clear what it means. For example, you have a function that is supposed to receive a parameter with a value between 1 and 3. You know the values correspond each to some behavior, like 1 = power on, 2 = standby, and 3 = power off. In your function, you can write out your if statements to be
if(parameter == 1)…
But that “1” there is a magic number. Instead, what is often suggested is to make constants with descriptive names for each of the 3 expected states. It makes it immediately clear what the possibilities are.
Basically any literal numeric constant with no explanation of what it is or where it came from.
As an example, I was working with some code involving greenhouse gas calculations and kept running across this ratio: 44 / 12. It was repeated in place after place. Eventually, I figured out that it's the mass ratio of CO2 to the elemental carbon it contains. So we gave that a name and used it instead of the constant. Hopefully the next person who has to read that bit of code will be spared some confusion.
Isn't avoiding magic numbers considered part of clean code? I don't do software development, more academic style code where generic names and magic numbers are expected to be understood. This specific code is part of an inexplicable Python implementation of a high performance PRNG.
Exactly. The number of time I've seen people do shift like const ONE = 1, TWO = 2 ....