Usually when you see a code in a if
compared to false
or true
, is doing something superfluous, since the if
at all times expects a boolean, so it can only be those 2 values. Any expression that results in a boolean satisfies the need.
The relational operator, which establishes whether the greatness is the same, different, larger, smaller, always returns a Boolean, then also satisfies the need. In fact, in most cases, it ends up being necessary because the type of expression used is not boolean, and it is the simple way to generate a boolean.
What many people don’t know is that the if
does not require any of this, just needs a boolean result. This is valid:
if (true)
yet without practical sense. Another way:
valor = true
if (valor)
or
totalDescontandoAcimaLimite = valorUnitario * quantidade * 0.9 > 100
if (totalDescontandoAcimaLimite)
that it is totally unnecessary to have this variable, but its existence increases readability. Already
condicao = valorUnitario * quantidade * 0.9 > 100
if (condicao)
is no longer readable. People use variables without any expressiveness just because they don’t understand what a variable (I won’t say they fail in communication and expression too).
So understand that people do things without them understanding why they’re doing it. And I divide it into two categories:
who does not know how the if
works
and confirming that the only special thing is that it requires receiving as "argument" a boolean, how to get to it does not matter, has languages that accept until you make a cast or a normal number is automatically promoted to boolean.
who knows this and insists on using the redundant form because they believe it is more readable
However, this belief comes because she has a partial understanding of what she is doing. Of course she can say she likes it that way, and no one can question anyone’s taste, but if she claims it’s universally legible, or she’s being arrogant, or she’s being ignorant, and she’s just repeating what some people say.
These same people do not for example
atual = estoque.contagem + 0
She justifies using the == true
to make sure it is a boolean where one expects a boolean. Well, because it does not use a boolean + 0
to indicate that it is a whole in a place that we have no way of knowing if it is or not, just by looking at this code? It is therefore much more necessary to make legible what the type of expression is in this case than in if
and no one does. It is a classic case of "two weights, two measures" that in general is not done in bad faith, is that one cannot equalize what one is doing.
I’ve never seen anyone do this:
if (objeto instanceof Tipo == true)
I put in the Github for future reference.
At the very least the person should be consistent. Why the person does not do so and in this case he does not use the == true
? Because she learned to use this operator that way, and people tend to do things for cake recipes and not because they understand what’s right. If she believed so much in the greater readability of checking again what she already knows she would do in this case too. Her argument is pure bullshit.
I find it less readable to be explicit, and it’s the same as saying "climb up". Read the expression fluently in Portuguese and tell me which works best as coherent text.
Only one left do
, or then do
right after the expression of condition to get more fluid. Some languages more derived from Pascal even do so and this can be said to be more readable under a certain aspect.
Languages more derived from C prefer the short way as a way to make it more readable, she opts for the more mathematical form. If the person likes verbosity and chooses the language to avoid doing so already chose the wrong tool.
It has language, in general functional ones, where its users guarantee to be more readable than any other language for being everything shorter. And I agree, although it’s harder to learn, and it "scares the weakest" by being something different, something you need to dedicate yourself to creating a natural flow in your head. The best programmers I know use functional, low-level languages, where natural readability doesn’t matter, but being short to read.
The same goes for preferring == false
in place of !
, the person may even have learned mathematics, after all she uses arithmetic in a natural way, when she arrives at Boolean algebra she can’t be natural, so forgive me, but she still can’t program.
Anyone can find it different. I tried to substantiate how I came to this conclusion. Every time I write this and someone disagrees, it’s just that, without justifying.
I suggest these people stop using 2x+1
and use dois vezes xis mais um
, is more their face. But they can write code as they see fit, it’s the same, it works and it’s right, it’s just horrible for my taste.
Related: IF, ELSE IF, ELSE or IF IF IF. When to use, what is the difference?
– user28595
Please indicate the language you are using to get a more concrete answer
– Jorge Costa
I don’t know anything about better performance using one or the other, it goes more of the person. The only concrete advantage between them is that without
== true/false
saves space if it is something crucial in the project (which, in general, is not)– Costamilam
For the sake of readability I adopted the habit of doing
if (false == ...)
which makes it more explicit that the condition may be false than using the!
.– Piovezan
Jorge Costa, I believe this applies to most languages it is implicit on my part to specify a language.
– Eduardo Mior