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.
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!"
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.
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.
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.
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'.
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.
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
In code you can just write
Which is a lot closer to natural language than