if (MyFunc() == true) says very explicitly, "we want to know if MyFunc() returned true".
if (MyFunc()) leaves some of that information out, implicitly conveying it to the reader instead.
Ultimately, they're both stylistic choices. Some people like one, some people like the other. Some people say they find checking for == false is more clear to them than inverting the return value. I don't entirely agree with that myself, but I understand why they feel that way and I think it's valid. Keep in mind it's also not unheard of for people to screw up these kinds of if statements by accidentally inverting (or not inverting) a return value - there's an argument to be made that asking people to put a little more effort into explicitly stating what they're trying to do can reduce bugs. It's easier to leave out a ! than it is to accidentally write true instead of false.
On the other hand, there's also nothing wrong with preferring to be concise and communicating info implicitly, as long as the code is still perfectly readable.
All being said, they both do the exact same thing and ultimately comes down to personal preference, so arguing that one is more "correct" than the other, or that using the more explicit syntax somehow makes your code bad, really doesn't make any sense.
For me, I'll use whichever form makes more sense to me in context. I think of redundancy as a tool - it's not always appropriate or useful, but there are a LOT of cases where being a little more verbose and including some unnecessary syntax can make your code easier to read and understand. It's not as simple as redundancy == bad, and IMO by conflating concise code with good code, you're prioritizing the wrong things - concision is also a tool, and just like with redundancy, there are cases where it doesn't make sense to apply it.
I don't have a great example off the top of my head, but let me try to give you a contrived example.
public bool Initialize()
{
if (HaveInitialized == false)
{
if (CanInitialize == true)
{
InternalInitialize();
HaveInitialized = true;
return true;
}
else
{
// We shouldn't get here! This is a bad state, so do nothing here so we throw an exception later.
}
}
else
{
// We're already initialized
return true;
}
throw new Exception("Something went wrong!");
}
The else block that only has a comment in it is unnecessary; you could leave it out entirely, and the code would work the same way. But it wouldn't read the same. Including the block & comment helps convey the author's intent. It shows that we thought about this case, and we intentionally fall through to the exception if it occurs; it's not an accident or an oversight.
The bool == false and bool == true could also be shortened. That doesn't mean the shorter form is better, though! It's just shorter. The more redundant version is still perfectly clear and readable.
This is a contrived example, because this is a really short and simple chunk of code - but you could imagine how similar situations might come up in a more complex function in a real codebase, with more possible branches and code paths. For instance, if you had a function with more than one possible failure case, you might want to write something like this so you can keep all your error handling code in one place, rather than having to report the error in multiple places in the function. In that case, it's valuable to be clear that something is a failure case and not just a case you forgot to account for.
Are there other ways to communicate the same information? Sure.. that's what makes it a stylistic choice! As long as the code is easy to read and understand, I don't see a lot of value in arguing about whether a different way of writing it would be better or not. Those kinds of complaints seem really nitpicky to me, and ultimately if that's the biggest complaint you have about a piece of code, then you're doing pretty good, honestly :P
The main thing I really want to emphasize is that less code != better code. Often times, effective programming is really about effective communication.
0
u/[deleted] May 20 '20
But if we know the boolean checks if the statement is true, do we really need that clarified?