"== true" is useful for something? Is it better "!" or "== false"?

Asked

Viewed 374 times

8

Place == true is useful in checking, or is completely useless?

You better use ! or == false.

Example:

if (pessoa.estaMorta() == false) ....
if (!pessoa.estaMorta()) ....

if (pessoa.estaViva() == true) ....
if (pessoa.estaViva()) ....

I did some research and found nothing about it.

  • 1
  • Please indicate the language you are using to get a more concrete answer

  • 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)

  • 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 !.

  • Jorge Costa, I believe this applies to most languages it is implicit on my part to specify a language.

3 answers

12


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.

Cavalo dentro de um carro de passeio

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.

  • I believe that your reasoning succeeds in overturning any argument from the supporters of == true/false. Very well explained and argued the answer, I think that on the question of readability of the code will depend very much also on the level of experience of the person... new people in programming will probably be able to "read" better codes that have == true/false but that will depend on the teacher as well. Now I can "upgrade" my codes.

  • 1

    While the guy does it in the first exercises, blz, he’s past the first ones, he won’t go forward until he changes that attitude.

8

If the methods estaViva() and estaMorta() return boolean values, it is completely redundant to match true or false. Not that it means you can’t do it, but it’s unnecessary.

If both methods already return a boolean value, just use their return as a condition for the if with if (!pessoa.estaMorta()) and if (pessoa.estaViva()), This improves the readability of the code.

1

I believe this is quite useful in dynamic typing languages, where a 0 and false are also treated.

Just for an example effect, PHP, there is a function of strpos. It returns the position where the searched text has been found or will return false if the value searched for in the string is not found. This seems normal, but the value searched is found at the beginning of the string it will return 0.

Since you’re dealing with a language where 0 and false are equal, you will have problems in these situations.

Personally, I prefer to always use the if($variavel === false), so ensure that it will not have an undesirable effect in any case. However, in other languages, simply use if(variavel) makes much more sense, since possibly variavel might just be true or false.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.