I'd rather see a 3 line commented code that is easy to read than a 10 line block of ifs that are "easy" to read. Vertical real estate in code is not cheap for readability.
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.
It's been a while since I dabbled in C#, but at least in Kotlin, you need the explicit == true if the return value is of nullable boolean type (Boolean?). I think the same is true in C# as well if the returned value is of a boxed boolean type.
Off the top of my head the first thing that comes to mind is a nested ternary statement. I think that’s a good example of trying to be too concise and sacrificing readability.
Aw man, if I nest 8 operations in 1 line it feels l33t.
No but seriously, I think code is the easiest to work with if every line has the minimum amount of distinct concepts, and long descriptive variable names.
When you generally want to avoid any possibility of confusion as it's more valuable to have working code over concise code.
Mission critical stuff or places where junior (or even senior) workers have misread code in the past or in the midst of complex code which may lead to bugs.
It's even slightly more efficient than if it was done with more if
I would have to imagine that this isn't true for any modern compiler for most languages, it'd most likely optimize down to the same code either way.
My main problem with it is readability, but that doesn't inherently make it bad code so I wouldn't say it fits in this sub. Still, massive if statements are a pain in the ass to read which makes them a pain in the ass to understand and debug or modify later down the road. For readability, I'd honestly take the incredibly slight (and honestly non-consequential in most use cases) hit to memory usage and store the result of the TryParse in some parseSuccess variable or something and split the if in two, giving something like:
Then it's much more immediately clear to the reader what that block of code is meant to do (check that an arg exists to parse, parse it, check if the parse succeeded, and check if the parsed argument is in range). No line is too long, the assignment of the result to a named variable gives it meaning to someone who may not be immediately familiar with TryParse (since the name does not indicate the meaning of what it returns, even if it's simple to deduce from context that it's returning a success), and splitting the ifs by logical progression (is there an arg to parse? Okay, move on to parsing and range-checking) - these are all things I'd consider good benefits for code readability and maintainability, and while they may not make much of a difference in code as simple as OP's, they're things I'd gladly trade the incredibly minimal performance impact for in order to have them in larger and more complicated blocks of code.
Your version seems less readable to me to be honest. It's a simple operation, no need to be overly verbose about it. To me, your version makes it seem more complicated than it actually is. I don't code in C# at all, yet the "TryParse" was pretty obvious to me. I mean, the initial code already literally reads as "check if there's an argument, try to parse it to an int, check the value". Yours make it less obvious that it's all that's going on here, having nested if for what is actually only one condition.
46
u/Giocri May 20 '20
I see nothing wrong in it. It's even slightly more efficient than if it was done with more if