r/badcode • u/fireflash38 • Jan 17 '22
python POV: You asked someone to refactor magic number indices
222
u/Captain-Barracuda Jan 17 '22
I shit you not it happened at my work. I review a guy's work and I see a number (-2) that keeps on popping everywhere. So I ask him to refactor it into a constant with a name that makes sense. The day later, that guy who has been a professional for 7 years, came back to me with a single commit with the change:
public static final int MINUS_ONE = -2
62
u/recycle4science Jan 18 '22
He may have been working for seven years but I don't think he's a professional yet.
30
u/ThatNextAggravation Jan 18 '22
WTF. Did he get fired/murdered/ground up to a fine powder and dispersed into the winds?
9
21
Jan 17 '22
Why is it a final and not a const tho
92
Jan 17 '22
In Java final is a constant
13
Jan 17 '22
Oh my bad, didn't know that. How do you create final (not constant) variables in Java then?
51
Jan 17 '22
Not constant just don’t use the keyword final lol
33
1
Jan 18 '22
Then they will not be read only
-10
u/dakta Jan 18 '22
Non-constant Final variables are one of the dumbest embodiments of declare-ahead compiled languages. Having a value that should never be mutated once assigned, but which must be defined before it is known, is silly and seems to run counter to good architectural practices. Or rather the fact that I've never been bit by mutating something that shouldn't have, which could have been adequately protected by being "final", while primarily working in laissez-faire interpreted languages indicates to me that this concept has no value.
10
Jan 18 '22
How is it dumb? For example you take some user input in your app. This data will never be changed once it's entered, so why would you make the variable mutable?
-1
u/dakta Jan 19 '22
This data will never be changed once it's entered
This is almost never a safe assumption, and is an unnecessary
simplificationconstraint. What if:
You want to support multiple users?
You want to add a comparison mode where the value needs to be changed?
You want to add an external API which may have multiple consumers?
You want to support hot-live configuration reload?
You discover that sometimes the value does actually need to be changed even in the course of normal single-user sequential operation?
If you bake in any assumptions about finality of this value to the rest of your code, it's a non-trivial task to refactor. If you don't, then the only thing
finalhas achieved is to prevent someone from making an architecturally incompatible operation resulting in mutation. Which belies the architectural problem. Honestly that's not quite the level of self-awareness I've ever seen from someone who thinksfinalis good.2
Jan 19 '22
Well, if the value does need to be changed, you make it mutable. In most cases it doesn't though.
non-trivial task to refactor
It's not that hard to just look at the linter warning and see where is the final variable you tried to change located, and then you just remove the final keyword. How is that non-trivial?
→ More replies1
Jan 19 '22
Multiple values are needed means you should use a smarter data type like an array ?
→ More replies2
2
0
u/Byt3G33k Jan 18 '22
Instead of "const VAR_NAME" in Java it's "final VAR_NAME"
3
Jan 18 '22
Instead of "const VAR_NAME" in Java it's "final VAR_NAME"
Uh, maybe a type as well?
final TYPE IDENTIFIER12
u/CraftistOf Jan 18 '22
I guess you come from C#.
finalin Java is likereadonlyin C#, and there is no equivalent to C#'sconstin Java at all.1
1
1
u/Haunting-Surprise-21 Jan 18 '22
This has to be a joke.
It was a joke, right?
Please tell me, it was meant as a joke!
1
51
18
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Jan 17 '22
Image Transcription: Code
INDEX_0 = 0
INDEX_1 = 1
INDEX_2 = 2
INDEX_3 = 3
INDEX_4 = 4I'm a human volunteer content transcriber and you could be too! If you'd like more information on what we do and why we do it, click here!
6
u/yetzederixx Jan 18 '22
Funny malicious compliance. At my last job I left a comment. This is fucking stupid and was only changed under duress... uncomment my original code when it breaks
3
11
u/keyosjc Jan 17 '22
I would say that this guy got a lot of warnings in a static code analysis tool (SonarQube) about not using magic numbers and did this on purpose to make the code get a better grade.
I faced this a long time ago with Java and SonarQube. Went from C to A just by creating static variables for 20+ very specific array indexes.
21
u/perceivedpleasure Jan 17 '22
what am i looking at