Throughout my (short) career so far in programming (merely a student working on internship) I have noticed that when it comes to IF statements, there is two different ways of doing it.
If we take foo as a boolean value:
if(foo)
{
//do stuff
}
This is my preferred way of doing things when dealing with IF statements, if I’m looking for false I use:
if(!foo)
{
//do more stuff
}
However, when some people see this they raise an eyebrow, suggesting that I may be stuck in a bad habit. But I wanted to know, is there any difference between this way or the “typical” way?
if(foo == true)
{
//do a bit more stuff
}
Am I falling into a common trap for new programmers? Or is there no difference (at least a noticeable one)
I never write == true or == false. It goes against the point of an if-sentence, in my opinion.
an if is basicly: If a Boolean expression is true, do something. a Boolean is a boolean expression in and of itself, so why wrap it?
So in my opinion, the ones using == true are the ones with a bad habit. Becuase they display ignorance of how the language works.
think of these “allowed” ways to write if (Foo) and if (!Foo):
using == and != with booleans actually introduce new ways to make mistakes both when programming and reading code.
Boolean and !Boolean is hard to misread.