Click here to Skip to main content
15,887,267 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: if else Style Pin
Iacopo Vettori28-Jan-24 23:47
Iacopo Vettori28-Jan-24 23:47 
GeneralRe: if else Style Pin
Peter Kelley 202128-Jan-24 23:58
Peter Kelley 202128-Jan-24 23:58 
GeneralRe: if else Style Pin
John Wellbelove29-Jan-24 1:29
John Wellbelove29-Jan-24 1:29 
GeneralRe: if else Style Pin
MikeCO1029-Jan-24 1:42
MikeCO1029-Jan-24 1:42 
GeneralRe: if else Style Pin
Matt Bond29-Jan-24 2:49
Matt Bond29-Jan-24 2:49 
GeneralRe: if else Style Pin
jschell29-Jan-24 5:22
jschell29-Jan-24 5:22 
GeneralRe: if else Style Pin
Alister Morton30-Jan-24 23:46
Alister Morton30-Jan-24 23:46 
GeneralRe: if else Style Pin
Stacy Dudovitz29-Jan-24 13:56
professionalStacy Dudovitz29-Jan-24 13:56 
In reading all of the responses, I'm quite surprised that the only alternative mentioned was the tertiary operator (?: ). While it's simple to respond 'context dependent' as I saw in some of the replies, I think maybe it would help to spell out the concerns as well as offer up more choices.

Let's start with the elephant in the room... readability and maintainability. They both go hand in hand to answer the question "what are you trying to do or say in the code?"

In addition, some of the choices can lead to both bugs and code creep.

Let's start with if/else as a potential solution to express a/the logical path to traverse. A couple of simple rules would help (these are my rules, some may be obvious, but some are what I call 'Where's the curly brace' religious preferences. Take everything I write here are my own style rather than engage in religiosity).
if (condition 1)
{
   // do something 1
}
else if (condition 2)
{
   // do something 2
}
else
{
   // do something 3
}

I always include the curly braces to ensure that scope is honored i.e. what code is executed based on which condition is met.

Too often I've see this:
if (condition 1)
   // do something 1
else if (condition 2)
   // do something 2
else
   // do something 3

The problem here is if another author comes in to add/maintain the code, and adds a second line of execution to one of the conditions. If that author forgets to now add the curly braces to enclose scope, bad things happen.
if (condition 1)
   // do something 1
else if (condition 2)
   // do something 2A
   // do something 2B      <-- we've exited the scope of the if/else clause. Ooops!
else
   // do something 3

In addition, the whole Nickleback induced hatred the tertiary operator hails back to a late 70's bug introduced in a version (or couple of versions) of the Whitesmiths C compiler, and has since become lore. If we are striving for readability, then:
if (condition) ? "do one thing" : "do the other thing";

If you are vehemently opposed to this, chances are you also avoid the null coalescing as well as other similar constructs:
char *s = null;
char *t = null;
...

t = s ? "some string" : "some default";

But really, is this the only way to proceed?

How about:
switch (operand)
{
   case 1:
      // do something case 1
      break;
   case 2:
      // do something case 2
      break;
   case 3:
      // do something case 3
      break;
   default:
      // do default option
      break;
}

Once your application starts to grow in complexity, then you start to contend with nesting:
if (condition 1)
{
   if (condition 2)
   {
   }
   else if (condition 3)
   {
      if (condition 4)
      {
      }
   }
}

This quickly becomes a nightmare to test and maintain. Testing requires the exercising of all logical branches, which this format complicates.

It quickly becomes evident that a better abstraction and/or implementation would be better suited. In the case of states, where a change of state triggers an execution of code and a transition to a new state, then a state machine would be better suited here to replace the if/else scaffolding (beyond the scope of this response).

And so it goes...

BTW, there are valid cases where the second format form of if/else would be better suited. For the case of nullable types i.e. bool? var, sometimes explicitly spelling out the true and false cases makes for better reading and comprehension:
bool? state;
...
if (state == true)
{
   // do some action if true
}
else if (state == false)
{
   // do some action if false
}
else // state is null
{
   // do some action (default action?) if null
}

There's more than one way to skin and if/else clause! Wink | ;) Wink | ;)
GeneralRe: if else Style Pin
jschell30-Jan-24 4:40
jschell30-Jan-24 4:40 
GeneralRe: if else Style Pin
Stacy Dudovitz30-Jan-24 10:38
professionalStacy Dudovitz30-Jan-24 10:38 
GeneralRe: if else Style Pin
jschell31-Jan-24 4:53
jschell31-Jan-24 4:53 
GeneralRe: if else Style Pin
englebart2-Feb-24 13:43
professionalenglebart2-Feb-24 13:43 
GeneralRe: if else Style Pin
englebart30-Jan-24 14:42
professionalenglebart30-Jan-24 14:42 
GeneralRe: if else Style Pin
Stacy Dudovitz30-Jan-24 16:22
professionalStacy Dudovitz30-Jan-24 16:22 
GeneralRe: if else Style Pin
englebart2-Feb-24 13:40
professionalenglebart2-Feb-24 13:40 
GeneralRe: if else Style Pin
hpcoder25-Feb-24 11:49
hpcoder25-Feb-24 11:49 
GeneralWordle 951 Pin
Sandeep Mewara25-Jan-24 15:53
mveSandeep Mewara25-Jan-24 15:53 
GeneralRe: Wordle 951 Pin
OriginalGriff25-Jan-24 20:27
mveOriginalGriff25-Jan-24 20:27 
GeneralRe: Wordle 951 Pin
Sander Rossel25-Jan-24 20:39
professionalSander Rossel25-Jan-24 20:39 
GeneralRe: Wordle 951 - 4 4 me Pin
pkfox25-Jan-24 21:03
professionalpkfox25-Jan-24 21:03 
GeneralRe: Wordle 951 Pin
GKP199225-Jan-24 21:03
professionalGKP199225-Jan-24 21:03 
GeneralRe: Wordle 951 Pin
ChandraRam25-Jan-24 21:47
ChandraRam25-Jan-24 21:47 
GeneralRe: Wordle 951 Pin
StarNamer@work26-Jan-24 0:06
professionalStarNamer@work26-Jan-24 0:06 
GeneralRe: Wordle 951 Pin
Cp-Coder26-Jan-24 2:46
Cp-Coder26-Jan-24 2:46 
GeneralWindows 11 venting Pin
cegarman25-Jan-24 11:01
cegarman25-Jan-24 11:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.