ProgrammerHumor

whatCleanCodeDoesToMfs

whatCleanCodeDoesToMfs

Please for the love of Ritchie, don't do this. What happened to the Pythonersisto who made this? What did they live through?

https://i.redd.it/bl8cse25cucf1.png
Reddit

Discussion

beisenhauer

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.

2 hours ago
quailman654

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.

1 hour ago
-LeopardShark-
:py::rust::hsk::js:

Possibly told by a badly written linter.

*Cough, cough, cough, Pylint, cough cough.*

1 hour ago
VibrantGypsyDildo

Oh pylint....

I love to use it, but I have to disable 10-15 warning types.

1 hour ago
didntplaymysummercar

I'm curious which. I only found "line too long" overly annoying, especially when using SQLite.

1 hour ago
gloritown7

Would you mind sharing which ones? I’ve had thought about it quite a bit but not sure which ones are „fine to disable“.

40 minutes ago
ActivisionBlizzard

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.

1 hour ago
Sw0rDz

What are magic numbers in this context?

1 hour ago
Punman_5

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.

17 minutes ago
beisenhauer

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.

15 minutes ago
soupster__

Doesn't clean code demand descriptive variable names?

2 hours ago
Accomplished_Ant5895
For i in range(4):
    eval(f”VAL_{i+1} = {i}”)
3 hours ago
RyukenSaab

We found the JS dev

2 hours ago
Accomplished_Ant5895

That’s the worst slur anyone has ever called me

2 hours ago
ultimate_placeholder
:c:

The valiant .NET engineer VS the perfidious JS developer

1 hour ago
Turbulent-Garlic8467

*exec, eval returns an expression

31 minutes ago
Snudget
:rust::py::asm:
for i in range(4):
    glboals()[f'VAL_{i+1}'] = i
29 minutes ago
neoteraflare

This is not even clean code. Do the names tell you what they mean by the position in the array/list?

2 hours ago
Sw429
:rust:

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.

3 hours ago
Anaxamander57 OP
:py::rust:

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

2 hours ago
SlightlyMadman

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)
2 hours ago
Snudget
:rust::py::asm:

What about using `VAL_4 + VAL_2`?

28 minutes ago
SlightlyMadman

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!

25 minutes ago
ShindouHikaru

PirateSoftware is that you?

2 hours ago
RyukenSaab

Python 3.13 supports enumeration… would have been much cleaner

2 hours ago
B_bI_L
:cs::js::ts::dart::asm::rust:

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)

1 hour ago
EyesOfEris

This is how i feel about the fact that 1900's = 20th century

1 hour ago
redlaWw

1900 is still part of the 19th century though.

1 hour ago
EyesOfEris

Even worse

1 hour ago
TheShirou97

yeah both centuries and years start at 1. So on 1st January 2000, only 1999 years had elapsed since the origin of the calendar

1 hour ago
ZinniaGibs

Ah yes, the classic off-by-one error: Baby's first nightmare in programming. 😂😂

1 hour ago
emetcalf

Real code that I found in a Production service at my job:

public static final int ONE = 1;

4 minutes ago