"== true" is it useful for anything? Is it better "!" Or "== false"?

8

Is == true useful for scans, or is it completely useless?

It is best to use ! or == false .

Example:

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

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

I have researched and found nothing about it.

    
asked by anonymous 15.07.2018 / 16:37

3 answers

12

Generally when you see a code in if comparing with false or true , you're doing something superfluous, since if always expects a boolean, so only can be these 2 values. Any expression that results in a Boolean satisfies the need.

The relational operator, which establishes whether the quantity is the same, different, major, minor, always returns a boolean, then also satisfies the need. In fact, in most cases, it becomes necessary because the type of expression used is not Boolean, and it is the simple way to generate a Boolean.

What many people do not know is that if does not require any of this, it just needs a Boolean result. This is valid:

if (true)

Although without practical sense. Another way:

valor = true
if (valor)

or

totalDescontandoAcimaLimite = valorUnitario * quantidade * 0.9 > 100
if (totalDescontandoAcimaLimite)

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 do not understand what a variable is for ( variable (I will not say they fail in communication and expression as well.)

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

  • Who does not know how if works

    and confirming that the only special thing is that it requires a Boolean argument, as if it does not matter, it has languages that accept until a cast or a normal number be promoted to 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 says it's universally readable, or she's being arrogant, or she's being ignorant, and she's just repeating what some people say. p>

These same people do not do for example

atual = estoque.contagem + 0

It justifies using == true to make sure it is a boolean where a boolean is expected. Well, why does not it use + 0 to indicate that it is an integer in a place that we do not know how to know if it is or not, just looking at this code? It is much more necessary thus to make readable what type of expression in this case than in if and nobody does. It is a classic case of "two weights, two measures" that is not usually done in bad faith, but the person can not equalize what he is doing.

I find it less legible to be explicit, and it's the same as saying "climb up." Read the expression fluently in Portuguese and tell me what works best as a coherent text.

Only one do , or then do is left shortly after the condition expression to get more fluid. Some more Pascal languages even do so and this can even be said to be more readable under a certain aspect.

More C-derived languages prefer the short way as 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.

Ithaslanguage,usuallyfunctional,whereitsusersguaranteetobemorereadablethananyotherlanguagebecauseeverythingisshorter.AndIagree,althoughit'shardertolearn,and"scares the weaker" because it's something different, something that needs to be dedicated to creating 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, so forgive me , but it still does not know how to program.

Anyone can find it different. I tried to explain how I came to this conclusion. Every time I write this and someone disagrees, it's all about it, without justification.

I suggest these people stop using 2x+1 and use dois vezes xis mais um , it's more their face. But they can write code as they see fit, work it out, work, and that's right, it's just awful to my liking.

    
15.07.2018 / 19:43
8

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

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

    
15.07.2018 / 16:43
2

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

For just one 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, however, the searched value is found at the beginning of the string it returns 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 much more sense, since possibly variavel might be only true or false .

    
24.07.2018 / 17:52