0

I want to know which method of boolean syntax do you guys prefer to use, I have some Boolean syntax when I was learning programming and up-until now.

Here are some cases:

Declared Variable

protected bool someBoolean = false;

Case 1:

if (someBoolean  == true)
{
   // Do something, it is being triggered
}

else if (someBoolean == false)
{
   // The Boolean is not being triggered
}

Case 2:

if (someBoolean)
{
   // Do something, it is being triggered
}

else if (someBoolean == false)
{
   // The Boolean is not being triggered
}

Case 3:

if (someBoolean == true)
{
   // Do something, it is being triggered
}

else if (!someBoolean) // Same with someBoolean == false
{
  // The Boolean is not being triggered
}

Case 4:

if (someBoolean)
{
   // Do something, it is being triggered
}

else
{
  // The Boolean is not being triggered
}

Case 5:

if (someBoolean)
{
   // Do something, it is being triggered
}

else if (!someBoolean) // Same with someBoolean == false
{
  // The Boolean is not being triggered
}

Which one (or more) of the cases above do you guys prefer and mostly use for your programming method when dealing with boolean? Please provide and gives some reason (or more) why did you choose that instead of others? :)

For me, I am more confident use the Case 5, because it is making my code looking beautiful (for what I am thinking it is)

Kaoru
  • 2,853
  • 14
  • 34
  • 68
  • 2
    I always prefer Case 4, because it is the cleanest to me. I highly doubt there's a non-trivial performance difference between these, so I'd just go with whatever looks better to you. – ryrich Mar 29 '14 at 04:17
  • 3
    I'm a C++ programmer so it's possible conventions are different, but I prefer case 4. The format is shorter and simpler (no need to rewrite the variable's name or a second conditional test), which seems more elegant to me. There's also less possibility of accidentally making the `if` and `else` conditionals identical if I'm working with a complicated expression. – cf- Mar 29 '14 at 04:18
  • @ryrich: They all compile to the same code. There is no performance difference among them. – Jim Mischel Mar 29 '14 at 05:06

1 Answers1

0

I will prefer to avoid if { } else { }

condition ? first_expression : second_expression;  

in your case

someboolean ? expression 1 : expression2;     

Otherwise will go with case 4. Where in if someboolean is false the code will hit else no need to check here whether it is false or not. In this case you reduce some extra code to check value which is not at all needed if someboolen is not true that means it is false.

Dnyanesh
  • 2,265
  • 3
  • 20
  • 17
  • You can't just write `someBoolean ? DoSomething() : DoSomethingElse();` The [conditional operator ](http://msdn.microsoft.com/en-us/library/ty67wk28.aspx) requires that the two expressions evaluate to the same type. In addition, you have to assign the result to something. See, for example, the answers to http://stackoverflow.com/q/5490095/56778 – Jim Mischel Mar 29 '14 at 05:13
  • Yes sir, Thanks for correcting me. Ternary operator comes with some constraints like you need to assign result and two expressions evaluate same type, but what I wanted to mention is Ternary comes handy to avoid if else. That's why I mentioned Otherwise will go with case 4. – Dnyanesh Mar 29 '14 at 05:29