is" = = true " useful for something? It's better "!"or" = = false"?

Is putting

Useful in checks, or is it completely useless?

It is better to use ! or == false.

Example:

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

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

I did a survey and found nothing about it.

Author: Maniero, 2018-07-15

3 answers

Usually when you see a code in a if comparing to false or true, you are doing something superfluous, since the if always 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 magnitude is the same, different, major, minor, always returns a boolean, then also satisfies the need. Indeed, in most cases it turns out to be necessary because the type of the expression used is not boolean, and it is the simple way to generate a boolean.

What many people don't know is that if doesn't require any of that, it just needs a boolean result. This is valid:

if (true)

Albeit without practical sense. Other form:

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 some expressiveness just because they do not understand what a variable is for (I will not say that they fail in communication and expression too).

Then understand that people do things without them understanding why they are doing it. And I divide it into two categories:

  • Who doesn't know how if works

    And confirming that the only special thing is that it requires to receive as an" argument " a boolean, as if arriving at it does not matter, it has languages which accept until a cast is made or a normal number is promoted to the boolean automatically.

  • Who knows this and insists on using the redundant form because they believe it to be more readable

    However this belief comes because it has partial understanding of what it is doing. Of course she can say she likes it that way, and no one can question someone's taste, but if she claims it is universally readable, either she is being arrogant, or she is being ignorant, and just repeating what some people say.

These same people do not for example

atual = estoque.contagem + 0

It justifies that it uses == true to make sure that it is a boolean where one expects a boolean. Well, why doesn't it use a + 0 to indicate that it is an integer in a place that we don't have how to know if it is or not, just by looking at that code? It is much more necessary so to make readable what is the type of the expression in this case than in if and no one it does. It is a classic case of "two weights, two measures" that in general is not done by bad faith, it is that the person can not equalize what he is doing.

I've never seen anyone do this:

if (objeto instanceof Tipo == true)

At the very least the person should be consistent. Why does the person not do so and in that case he does not use the == true? Because she learned to use this operator that way, and people tend to do things by cake recipes and not because they understand what is 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 even and tell me which works best as a coherent text.

Only one do is missing, or then do right after the condition expression to become more fluid. Some languages more derived from Pascal even do so and this can even be spoken which is more readable in a certain respect.

Languages more derived from C prefer the short way as a way to make it more readable, it opts for the more mathematical form. If the person likes verbosity and chooses the language he avoids doing so he has already chosen the wrong tool.

Horse inside a passenger car

Has language, in general functional ones, where its users guarantee to be more readable than any other language because it is all shorter. And I agree, although it is harder to learn, and "scares the weakest" for being something different, something that needs to be dedicated to create a natural flow in the head. The best programmers I know use functional and low-level languages, where natural readability does not matter, but rather be short to read.

The same goes for preferring == false instead of !, the person may even have learned mathematics, after all he uses arithmetic in a natural way, when he arrives in Boolean algebra he can not be natural, therefore, forgive me, but she still does not know how to program.

Anyone can find different. I tried to substantiate how I came to this conclusion. Every time I write this and someone disagrees, it just stays on it, without justifying.

I suggest these people stop using 2x+1 and use dois vezes xis mais um, it's more their face. But you can write code as you see fit, it works, and it's right, it's just horrible for my taste.

 11
Author: Maniero, 2020-07-24 18:19:06

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

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

 7
Author: , 2018-07-15 14:43:03

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

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

Since you are dealing with a language where 0 and false are the same, you will have problems in these situations.

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

 1
Author: Inkeliz, 2018-07-24 15:52:31