@Nicolas_Harraudeau
I think you understand my main point.
It is true that booleanMethod() == false
is easy to read alone.
Thank you for taking the time to understand me.
I understand your points regarding Code Symmetry and Code consistency.
I’m not attempting to say that all cases of !myvar
should be replaced with myvar == false
but I would argue readability over Code Symmetry and Code consistency
and if we can do all three at the same time let’s do it.
Now I need to pick at your arguments just a bit
Code symmetry luke pick apart gently.
I have to stop for a second and see if myvar can have a different value than true and false, such as null for a nullable boolean.
No, you don’t. Nullable bool is not able to be used in that way.
bool? nullBool = true;
if (nullBool)
{ }// this code will not even complie
same for the && statements
this also will not compile
bool? nullBool = true;
var result = nullBool && (1 == 2);// this code will not even complie
Code consistency luke pick apart gently.
Right so I could say for my code or my teams’ code ‘== false’ is the right way, and don’t use ‘!’
I know this is going to be a stretch with this example but something like this bit me in the past.
if (!ion){ // feed the lion}
my point? some times ‘!’ look like ‘l’ or ‘I’ especially for us without perfect eyesight
but if written like this it would prevent the same error
if (ion == false){ // skip getting the electron micoScope}
Also, a common best practice is to name bool variables with the word Is e.g
if (!IsValid)
vs if (IsValid == false)
I have been doing quite a bit of research related to this and the community is split on which is better.
the community seems to be split but in general, preferring !
to == false
.
however, the fact remains booleanMethod() == false
is not redundant.
Saying it’s redundant suggests that it can be removed without changing its meaning.
booleanMethod() == false
is NOT the same as booleanMethod()
.
My argument now is while there are goods reason to prefer !booleanMethod()
over booleanMethod() == false
What I’m looking for
I want the option to write in the cases where myvar == false
is more readable. Without code reporting that my code sucks.
There are at least some reasons to prefer booleanMethod() == false
and we should not punish developers that make that design decision.
I’m ok if this triggers a warning or info. with some other meaningful message.
but the statement “that == false
is redundant”
is a lie as it can’t be removed without changing the meaning.
It’s not just me saying this at least 53 other people find this destination useful.
quote from link above
q == false
is NOT redundant, just more verbose than !
. OTOH, == true
is redundant. – dan04 Feb 25 '12 at 23:44
Luke now understanding where we are at as a sonar could community
Now with all this said. I’ve been heard out I understand that while I’m not alone. I am in the minority opinion.
I’m glad I had the conversation here first. This is better than having done a pull request to have it fixed and have my work rejected at that time.