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.

1 day 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 day ago
ralsaiwithagun

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

1 day ago
propthink

Gonna need to declare an enum to access the list items

21 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

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

Possibly told by a badly written linter.

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

1 day ago
VibrantGypsyDildo

Oh pylint....

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

1 day ago
didntplaymysummercar

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

1 day 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“.

1 day 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.

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

1 day ago
Sw0rDz

What are magic numbers in this context?

1 day 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

1 day 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.

1 day ago
code_investigator

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

22 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

20 hours ago
DowntownLizard

Apparently forgot to explain that val is also not descriptive lol

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

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

19 hours ago
hollowman8904

Yes, but this doesn’t avoid magic numbers. You don’t convey any additional useful information by substituting “VAL_1” for “1”

16 hours ago
le_birb
:py:

There's bonus points here because VAL_1 means 0

6 hours ago
soupster__

Doesn't clean code demand descriptive variable names?

1 day ago
neoteraflare

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

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

The his is the way, and its code safe

1 day ago
Turbulent-Garlic8467

*exec, eval returns an expression

1 day ago
RyukenSaab

We found the JS dev

1 day ago
Accomplished_Ant5895

That’s the worst slur anyone has ever called me

1 day ago
ultimate_placeholder
:c:

The valiant .NET engineer VS the perfidious JS developer

1 day 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)
1 day ago
Snudget
:rust::py::asm:

What about using `VAL_4 + VAL_2`?

1 day 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!

1 day 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;
}
20 hours ago
SlightlyMadman

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

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

1 day 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

1 day ago
ShindouHikaru

PirateSoftware is that you?

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

nah piratesoftware doesn't use constants, at all

18 hours ago
emetcalf

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

public static final int ONE = 1;

1 day ago
Wooden-Contract-2760

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

21 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 day ago
Revolutionary_Dog_63

Aliasing the index operator for low-number hardcoded integers is hardly the same.

15 hours ago
EyesOfEris

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

1 day ago
redlaWw

1900 is still part of the 19th century though.

1 day ago
EyesOfEris

Even worse

1 day 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 day ago
ZinniaGibs

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

1 day ago
WeeziMonkey

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

23 hours ago
Anaxamander57 OP
:py::rust:

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

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

17 hours ago
RyukenSaab

Python 3.13 supports enumeration… would have been much cleaner

1 day ago
AfterTheEarthquake2

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

21 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. 😐

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

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

18 hours ago
Anaxamander57 OP
:py::rust:

I assume they call the other function and negate the output and "we expect the compiler knows what to do".

14 hours ago
Informal_Branch1065

Having to see this should be considered thorture

11 hours ago
That-Cpp-Girl
:cp::lua::p:

Given that it's for indexes, it can be quite useful to have such constants when they're shared between C/C++ and Lua, for example.

10 hours ago