Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
I've been programming for years and I still do this. For me, it indicates that the value you're testing is specifically boolean. Since any non-zero value also evaluates to true, you can improve readability by doing this.
-
devmg1047yThis may seem silly but I try to avoid if-statements. Often there is actually no need to use them, as you can index into tables or make function calls instead. If you do that you can cache results and only recompute values when necessary. If you are not computing values, then you are instead relying on side effects, and although there are times when it's necessary, I've found that most often there is a better way to get efficient and simple code. You just need some library to manage the constructs you need for such data flow.
-
If the code is a victim to bad naming, it's best to do this actually.
For example:
if(getFinished())
Doesn't say anything. It's more informative to write
if(getFinished()==true)
even though it's redundant. It communicates type better and lets you understand what's going on very easily just by reading this line of code.
A good practice, for me at least, is to have variables, getters, answer a question:
if(isFinished())
That says it all. Or if you really insist on using 'get':
if(getIsFinished())
Then surely, skip the "==true" -
I work with elastic search (Java full text search engine) and they always do this:
if(someBool == false) { ...
I havent seen a single ! so far. I don't know why this annoys me so much -
devmg1047yIf in javascript, "if (x === true)" is very useful, and has a different meaning than "if (x)", i.e. "if (x == true)"
-
ggromx4367yI only came here wondering how many statements would it require to terminate a life, but the comments became so relevant in terms of different scenarios in which it may be relevant.
I really enjoy this community, always sharing knowledge. -
devios157707y@devmg In JavaScript if (x) is not the same as if (x == true). Consider x = "test". It's truthy, but not == true.
Related Rants
Everytime I see
if (x.getSomeBoolean() == true) ...
I die a little bit inside.
undefined
butwhy
logic
booleans