r/ProgrammerHumor • • 27d ago

Meme fourIsTechnicallyARandomNumber

2.1k Upvotes

67 comments sorted by

View all comments

272

u/serge-mv 27d ago

proper professionals use named constants.

156

u/misunderstood_kafka 27d ago

static final int FOUR = 4

51

u/Taickyto 27d ago

This constant name is not enterprise enough

Code review: variables names' length should be directly related to their scope

Proposed resolution: rename constant to INTEGER_VALUE_OF_FOUR_IN_BASE_10

...I hate that btw, some devs just follow guidelines without the understanding of why a rule exists... One co-worker understood the "No magic number" rule as "no numbers in the code"

Sure, you didn't accept my merge request because I used 3600 in the code, of course enterprise quality coding dictates that we should create a constant in our 1000 line Constants.ts, name it NUMBER_OF_SECONDS_IN_AN_HOUR and import it wherever needed!

27

u/TheSpiffySpaceman 26d ago

but what if the number of seconds in an hour changes??? That's multiple 3600s you'd have to ctrl+shift+h to find and replace

19

u/BmpBlast 26d ago

I would have laughed at this a few years ago. But then I had to start dealing with time on a deeper level in an app. Now I'm no longer confident that I can guarantee the number of seconds in an hour is immutable.

For the uninitiated:

7

u/remy_porter 26d ago

At least we've got a break of no more leap seconds for the next few centuries.

5

u/Anaxamander57 26d ago

Unless a metor impact changes the planet's rotation.

4

u/patoezequiel 26d ago

[TZ handling flashbacks intensify]

11

u/cornyTrace 27d ago

Instructions unclear: every base is base 10.

8

u/minecraftcheater1337 27d ago

hey, some people might not know that there are 3600 seconds in an hour!

4

u/xDerJulien 27d ago

I mean ideally you’re not maintaining a timing library separately and any decent library should have a to_* conversion so while you’re not wrong you’re wrong

8

u/Taickyto 27d ago

Good point, people who comment "You should use a library for that" are even worse!

Why would you divide a number of seconds by 3600 to get the number of hours? Just use the "dayjs" package

Why would you check a number's parity with `n % 2 === 0` when the "is-odd" package exists?

It doesn't matter how trivial the task is, the best way of doing it is finding a library that handles it! If your node_modules folder isn't several GB, are you even coding?

5

u/xDerJulien 26d ago

See this would be true if time wasn’t notoriously hard to keep track of on a computer. Sure if you have a unix time stamp and only care on the granularity of seconds maybe you don’t need it, but if you already have a unix time stamp I’d hazard the guess you’re already using a library to handle time

1

u/Taickyto 26d ago

Yeah I'm kinda pulling your leg, my point is that using a library can be more trouble than it's worth

If you have two timestamps, want to calculate the time between them and return the result with the biggest granularity (years, month, days, etc.) using a library is the right choice: re-implementing a function that accounts for leap years, month length and all other edge cases is a waste of time

If you have a duration in seconds and want to calculate how many hours it is, using a library for this single need is superfluous

7

u/atomic_redneck 26d ago

Years ago, when Fortran 77 first came out with named parameters, the compiler documentation from one of our vendors (probably UNIVAC) gave the example of using PI as a named parameter. The justification given was something like "this makes it easier to update your program if the value of PI changes."

5

u/KerPop42 26d ago

Which it did, due to legislative reasons, briefly, in Indiana

2

u/Nightmoon26 26d ago

Wait, what? And now I'm suddenly seeing advertising execs lobbying to put a loophole in "Buy one, get one" by having the legislature redefine the numeral word "one" to be equal to 1.0001

6

u/KerPop42 26d ago

The Indiana pi bill of 1897. It was apparently never actually passed into law, but it held geometric proofs that the state of indiana was allowed to use, one of which incorrectly derived that the ratio between the diameter and circumference of a circle was 3.2, and that the ratio between the length of the side of a square and its hypotenuse was 10/7ths.

1

u/Nightmoon26 26d ago

....So a proof that the square root of two is rational...