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.

9 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.

8 hours ago
ralsaiwithagun

Put the indices into a list so that you can easily index the indices later without hassle.

6 hours ago
propthink

Gonna need to declare an enum to access the list items

4 hours ago
ItsRyguy

Nah just using indices normally (doing nothing) is definitely better. If you really need to convey that only four items can exist in a list then a single small comment will do much more than these variables

2 hours ago
-LeopardShark-
:py::rust::hsk::js:

Possibly told by a badly written linter.

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

8 hours ago
VibrantGypsyDildo

Oh pylint....

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

8 hours ago
didntplaymysummercar

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

7 hours 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“.

7 hours ago
VibrantGypsyDildo

The general idea is that if you tool don't meet your desires, you change your tools, not your desires.

Variable/constant naming rules, requirements for docstrings, explicitly specifying utf-8 when opening a file -- all those rules make sense in specific contexts. Not in mine though.

There is a bunch of less annoying pylint rules, but I just forgot about them since I work on an other project for almost a year.

3 hours 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.

8 hours ago
Sw0rDz

What are magic numbers in this context?

7 hours 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.

Edit: I should add that this is really just for readability. Software that’s maintained by a revolving door of people over several decades will benefit greatly if the “no magic numbers” rule is followed from the start

7 hours 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.

7 hours ago
code_investigator

Exactly. The number of time I've seen people do shift like const ONE = 1, TWO = 2 ....

4 hours ago
henryeaterofpies

Am I gonna see this on a Pirate stream in a couple weeks before he gaslights me that its for the ARG

2 hours ago
Anaxamander57 OP
:py::rust:

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.

5 hours ago
Gorexxar

Yes, but giving them meaningful names is also a part of 'clean code'. Right now it reads like malicious (or ignorant) compliance.

1 hour ago
soupster__

Doesn't clean code demand descriptive variable names?

9 hours ago
Accomplished_Ant5895
For i in range(4):
    eval(f”VAL_{i+1} = {i}”)
9 hours ago
Snudget
:rust::py::asm:
for i in range(4):
    glboals()[f'VAL_{i+1}'] = i
7 hours ago
SkezzaB

The his is the way, and its code safe

6 hours ago
Turbulent-Garlic8467

*exec, eval returns an expression

7 hours ago
RyukenSaab

We found the JS dev

8 hours ago
Accomplished_Ant5895

That’s the worst slur anyone has ever called me

8 hours ago
ultimate_placeholder
:c:

The valiant .NET engineer VS the perfidious JS developer

8 hours ago
neoteraflare

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

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

What about using `VAL_4 + VAL_2`?

7 hours 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!

7 hours ago
foxer_arnt_trees

You can still use variables if you are willing to migrate to php

for ($i = 1; $i <= 2**63 - 1; $i++) {
    ${"val_$i"} = $i - 1;
}
2 hours ago
SlightlyMadman

Definitely worth building out a php interpreter in order to add this.

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.

9 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

9 hours ago
ShindouHikaru

PirateSoftware is that you?

9 hours ago
SignificantLet5701
:cp::c::j::rust:

nah piratesoftware doesn't use constants, at all

15 minutes ago
emetcalf

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

public static final int ONE = 1;

6 hours ago
Wooden-Contract-2760

Some static classes for FallbackValues can come in handy. They are usually kept internal, though.

3 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)

8 hours ago
WeeziMonkey

A die-hard clean code purist wouldn't use abbreviations like "VAL" when "VALUE" is only two extra letters.

5 hours ago
Anaxamander57 OP
:py::rust:

ONE = 0 TWO = 1 THREE = 2 FOUR = 3

5 hours ago
RyukenSaab

Python 3.13 supports enumeration… would have been much cleaner

8 hours ago
EyesOfEris

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

8 hours ago
redlaWw

1900 is still part of the 19th century though.

8 hours ago
EyesOfEris

Even worse

8 hours 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

7 hours ago
ZinniaGibs

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

7 hours ago
AfterTheEarthquake2

Dim s1 As String, s2 As String, s3 As String, s4 As String

3 hours ago
minju9

Had a junior dev that got sucked into the functional programming rabbit hole, wrote getTrue()/getFalse() functions that do exactly what you would think. 😐

1 hour ago
SignificantLet5701
:cp::c::j::rust:

Constants weren't enough for this guy, functions all the way

14 minutes ago
PogostickPower

I must bow to the SonarQube even when it demands the absurd. I begged project management to stop this nonsense but they refuse. The code smells must go away, they say, and the criteria for determining what's smelly might as well be carved in stone by Moses himself.

2 minutes ago