0

Given the following:

bool isCorrect = theAnswer == 42;

Which is the preferred way of testing false boolean logic in C# (programming in general)?

if (!isCorrect)
  // throw exception

or

if (isCorrect == false)
  // throw exception

The reason I ask is because one of our senior developers at work suggests we should always use the latter approach as it enhances readability and ensures other developers can clearly see the false check; an exclamation mark is easy to miss. I much prefer the former as it's more concise and readable enough to me.

I understand that this may be a subjective issue so was wondering if there was a concrete preference mentioned in any coding style.

Appulus
  • 18,630
  • 11
  • 38
  • 46
  • I don't think there is a definitive style. Just follow the lead dev. – The_Cthulhu_Kid Dec 07 '12 at 12:48
  • Saying that I prefer (!isCorrect) but the latter is easier to read – The_Cthulhu_Kid Dec 07 '12 at 12:49
  • Each company will have there own preferred coding style. You should follow this. – Ash Burlaczenko Dec 07 '12 at 12:53
  • Possible duplicate of http://stackoverflow.com/questions/356217/should-i-use-isgood-or-isgood-false – Rui Jarimba Dec 07 '12 at 12:53
  • One annoyance is with negative boolean names: `if(!isNotEnabled)` that can cause a confusion. And `!` is easy to miss : `if(!inCache)`. But still, every one prefers their own style. – S.D. Dec 07 '12 at 12:53
  • 1
    Follow the local style, but personally I find (!isCorrect) is easier to read than (isCorrect==false), which I have to mentally translate to (!isCorrect) anyway.... I have no problem seeing "!" characters (nor do I have difficulty distinguishing "+" and "-" and so on....). There are some who suggest introducing a property called "isNotCorrect" (or possibly in this case, "isWrong") and therefore you get the even more readable if (isWrong). Thinking about it, who on earth finds an "!" easy to miss? Do they also confuse "!" and "!="? – Matthew Watson Dec 07 '12 at 12:58
  • 1
    Personaly I prefer !isCorrect. But the most important is to be consistent along your project. That's where the most readability comes from ;) – CuccoChaser Dec 07 '12 at 13:21
  • @RuiJarimba, good find though that question did not come up with any search (probably because of the title). – Appulus Dec 07 '12 at 13:50

4 Answers4

3

I have seen production code from senior developers containing such code:

if (isCorrect.ToString().Length == 5)

But I'm still using:

if (!isCorrect)

Use what you think is more readable for you, there is no statistics among all developers))

SergeyS
  • 3,515
  • 18
  • 27
1

The preferred way (I'm not sure if it actually is a best practice, but it definitely should be) is to not test false

// First question: "Is the answer correct ?"
bool isCorrect = theAnswer == 42;

// Second question: "What if it is ?"
if (isCorrect) 
{

}
else //Third question: "What if it isn't ?"
{
}

It's not only more logical, but saves you from scrolling around to skip error handling if you need to follow the actual flow of your code.

Also, it's worth pointing out for completeness that boolean names should always be positive: think isCorrect VS isNotWrong ... isPositive VS isNotNegative ... much easier not only to read but to understand too.

Alex
  • 23,004
  • 4
  • 39
  • 73
  • 1
    Yes, this avoids most of the pitfalls, except you can often end up with weird looking code where you have a lot of empty "if" blocks and the actual code ends up in the "else" blocks. But you can usually avoid this my making the sense of the boolean the "right" way around - so, similar to what you say, use "isCorrect" rather than "isWrong". – Matthew Watson Dec 07 '12 at 13:13
  • I completely agree (and forgot about it): a boolean which would be read "this is true, hence the condition I checked to fill its value was false" should be avoided (of course, this is not set in stone: it's just a principle. The whole point is, keep the code logic easy to follow) – Alex Dec 07 '12 at 13:29
  • @Alex, although I appreciate your answer and logic of it (no pun intended), I don't think I agree with it. My example was a very trivial one but sometimes you simply cannot avoid the false check, e.g. `!someList.Any()`. – Appulus Dec 07 '12 at 13:55
  • I stand by my words: false-checking is easy to miss `if (someList.Any()) {} else {}` no way you can misread that. – Alex Dec 07 '12 at 14:31
0

You are right that (!condition) is more concise.
The senior dev is right that (condition == false) is more 'visible' (or readable if you like).

Since it just boils down to preference, you should just do what the senior dev suggests and keep everything consistent whether you like it or not. When you are the senior dev you can go back and change everything.

A.R.
  • 15,405
  • 19
  • 77
  • 123
  • 1
    I don't think there's any objective answer to which is more readable. I definitely find "if (!isCorrect)" more readable than "if (isCorrect == false)" since I read it directly as "if it's not correct" whereas with "if (isCorrect == false)" I have to mentally change the order. But that is just my PERSONAL opinion. I'm just saying, you can't objectively say which is best. – Matthew Watson Dec 07 '12 at 13:08
  • @MatthewWatson: Agreed, you can't say which is best, and so that's why I said do whatever your (boss) wants. Meanwhile, I guess that 'visible' is a better term than 'readable'. Moving along, I will say that I also prefer (!condition) – A.R. Dec 07 '12 at 14:07
0

Given the following:

bool isCorrect = theAnswer == 42;

The clearest way to check the inverse is (IMHO!):

bool isWrong = !isCorrect;

...

if (isWrong)
// throw exception
Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
  • Than another question, maybe we need: bool isWrong = (isCorrect == false); here?)) – SergeyS Dec 07 '12 at 13:05
  • Surely not... Would you write "x = 0 - y" instead of "x = -y"? No? So why would one do the equivalent with a boolean? Well, clearly it's just a personal preference! :) – Matthew Watson Dec 07 '12 at 13:10