Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it acceptable to only use the 'else' portion of an 'if-else' statement?

Tags:

Sometimes, I feel like it is easier to check if all of the conditions are true, but then only handle the "other" situation.

I guess I sometimes feel that it is easier to know that something is valid, and assume all other cases are not valid.

For example, let's say that we only really care about when there is something wrong:

object value = GetValueFromSomeAPIOrOtherMethod();  if((value != null) && (!string.IsNullOrEmpty(value.Prop)) && (possibleValues.Contains(value.prop))) {     // All the conditions passed, but we don't actually do anything } else {     // Do my stuff here, like error handling } 

Or should I just change that to be:

object value = GetValueFromSomeAPIOrOtherMethod();  if((value == null) || (string.IsNullOrEmpty(value.Prop)) || (!possibleValues.Contains(value.prop))) {     // Do my stuff here, like error handling } 

Or (which I find ugly):

object value = GetValueFromSomeAPIOrOtherMethod();  if(!((value != null) && (!string.IsNullOrEmpty(value.Prop)) && (possibleValues.Contains(value.prop)))) {     // Do my stuff here, like error handling } 
like image 690
John B Avatar asked Sep 24 '09 18:09

John B


People also ask

Do you need an else in an if else statement?

An if statement looks at any and every thing in the parentheses and if true, executes block of code that follows. If you require code to run only when the statement returns true (and do nothing else if false) then an else statement is not needed.

What is the use of ELSE part in if else block?

The if-else statement is used to execute both the true part and the false part of a given condition. If the condition is true, the if block code is executed and if the condition is false, the else block code is executed.

Can we use if inside the else part?

Yes, placing an if inside an else is perfectly acceptable practice but in most cases use of else if is clearer and cleaner.

When would you use an ELSE IF statement?

Use if to specify a block of code to be executed, if a specified condition is true. Use else to specify a block of code to be executed, if the same condition is false. Use else if to specify a new condition to test, if the first condition is false. Use switch to select one of many blocks of code to be executed.


2 Answers

Though rare for me, I sometimes feel that writing in this form leads to the clearest code in some cases. Go for the form that provides the most clarity. The compiler won't care, and should generate essentially (probably exactly) the same code.

It may be clearer, though, to define a boolean variable that is assigned the condition in the if () statement, then write your code as a negation of that variable:

bool myCondition = (....); if (!myCondition) {     ... } 
like image 64
Eric J. Avatar answered Sep 17 '22 15:09

Eric J.


Having an empty if block with statements in the else is ... just bad style. Sorry, this is one of my pet peeves. There is nothing functionally wrong with it, it just makes my eyes bleed.

Simply ! out the if statement and put your code there. IMHO it reduces the noise and makes the code more readable.

like image 41
JaredPar Avatar answered Sep 17 '22 15:09

JaredPar