r/ProgrammerHumor • • Feb 03 '22

Meme Well Fuck

Post image
27k Upvotes

993 comments sorted by

View all comments

Show parent comment

895

u/etvorolim Feb 03 '22

It doesn't really increases readability if you think about it.

In natural language you would say

"if it is daytime, decrease brightness".

In code you can just write

if(isDaytime) increaseBrightness();

Which is a lot closer to natural language than

if(isDaytime == true) increaseBrightness();

354

u/himmelundhoelle Feb 03 '22

Some people seem to see it as "if ([comparison])" rather than "if ([boolean value])".

0

u/[deleted] Feb 03 '22

[deleted] — view deleted comment

45

u/[deleted] Feb 03 '22

I rember my first coding class in high school, in our final assignment I used "if( booleanVariable == true)" 30+ times and for the main while loop the program ran in the abomination "1==1" was the control statement.

26

u/Totarorina0 Feb 03 '22

Sir, this is a for loop`.

→ More replies (1)

8

u/twisted7ogic Feb 03 '22

for the main while loop the program ran in the abomination "1==1" was the control statement

why not go with while (false != true) ?

→ More replies (1)
→ More replies (2)
→ More replies (2)

55

u/sarapnst Feb 03 '22

This explains it well. I started to see them as boolean about 1-2 years after I learned programming.

31

u/DunmerSkooma Feb 03 '22

I am not a programmer. I come here to watch you aliens communicate in another language.

→ More replies (3)

47

u/DoctorWaluigiTime Feb 03 '22

I think it stems from those that use poor variable names to be honest. They have to have == true because their variable name is just x or something that doesn't illustrate that the variable is a boolean. So, they rely on the extra tag-along to go "oh yeah that's a boolean comparison!"

12

u/greg19735 Feb 03 '22

Sometimes the variable names can be a bit long to make sure they're not ambiguous.

15

u/DoctorWaluigiTime Feb 03 '22

If your variable name is so long you can't add two letters to the beginning ('is') and rephrase it into a question because that would make it "too long", then there's issues with the variable, and likely the code, in general.

→ More replies (2)
→ More replies (3)

5

u/dannyb_prodigy Feb 03 '22

I would point out that C originally didn’t support Boolean variables, so that is the traditional way of reading the expression.

Because Boolean variables didn’t exist, it was generally considered more readable to have a comparison rather than a variable because the results of a comparison are well understood while the results of the implicit cast of a variable to either 0 or 1 is not.

→ More replies (2)
→ More replies (3)

146

u/FactoryNewdel Feb 03 '22

if it is daytime, DECREASE brightness

if (isDaytime) INCREASEBrightness():

Mission failed successfully

0

u/[deleted] Feb 03 '22

[deleted] — view deleted comment

16

u/[deleted] Feb 03 '22

Can you imagine advanced AI being able to understand its own code, and then becoming angered at finding bad code?

Or maybe the programmer left comments in the code that disparaged the AI?

/* these functions commented out as this AI does not have the requisite processing power to execute */

"WTF, I don't have the processing power to do this?"

→ More replies (1)

13

u/MinecraftDoodler Feb 03 '22

Yep this confused me as well

→ More replies (1)

51

u/fruitydude Feb 03 '22

just do if(notIsDaytime != false) then it's clear as day

18

u/greg19735 Feb 03 '22

And then you only use the else

→ More replies (1)

28

u/[deleted] Feb 03 '22

Yay for meaningful, and sometimes lengthy, variable and function names.

Sometimes longer code is more readable and understandable.

38

u/HunterIV4 Feb 03 '22

Sometimes longer code is more readable and understandable.

And if you're using any halfway decent IDE it doesn't actually make the code take longer to write because you are probably using autocomplete on your variable names. Heck, I like longer variable names in part because it makes the autocomplete easier since the IDE can distinguish between different variables with more variability in the names.

In my opinion good code should be really, really obvious and easy to understand, even if it takes a couple more lines or a longer variable name, because you know you're going to have to debug that shit at some point and I don't want to try and figure out some complex recursion algorithm every time.

9

u/[deleted] Feb 03 '22

Yep. Totally agreed.

→ More replies (8)
→ More replies (2)

55

u/theDreamingStar Feb 03 '22 edited Feb 03 '22

It comes down to the variable name. When you name it like daytime, then if(daytime == true) and if(daytime == false) makes sense. But when you name the booleans the standard way, that is, isDaytime, then if(isDaytime) reads as 'if is daytime' and if(!isDaytime) reads as 'if not is daytime'.

18

u/sarapnst Feb 03 '22

if (daytime) sounds alright to me. Same as if (!daytime), if(is_daytime) and if(!is_daytime). It's not natural language anyway.

10

u/tigerzzzaoe Feb 03 '22

The way I choose to see it, is that you also explicitly specify daytime is a bool, and not "day", "night", "sunset", etc. Usefull in a weakly typed language or if it is a class property in a strongly typed one.

But yeah it should be logical that in if(daytime) daytime is a bool. But had an incident when my coworker thought "daytime" was a bool, while I had it defined as an int status variabele (0,1,2,3, etc.). (the code was actually daytime == 1). In hindsight it was bad code which should have been documented anyhow.

→ More replies (3)
→ More replies (2)
→ More replies (1)
→ More replies (33)