1

I have been referring my new company's code, where I found the code was not wrapped around IF and ELSE so was little bit confusing in flow. For example:

if(user_is_registered)
  {
      // You are already registered.
      //exit
  }
//New user's registration code goes here

While my last company was following the other way i.e. :

if(user_is_registered)
   {
      // You are already registered.
   }
else
   {
      //New user's registration code goes here
   }

Like every fresher I am confused which is the best practice to follow with legitimate reasons. Please enlighten me. I tried to find out answers for the same, but could not get. Some of answers are there in which some experts are supporting way 1 some are supporting way 2. Please also suggest me other refs if available. Thank you.

Imran Ahmad
  • 2,798
  • 3
  • 28
  • 49
  • Thanks @Bergi. It was really helpful. – Imran Ahmad Dec 15 '15 at 13:37
  • Maybe also see the [more extensive discussion](http://stackoverflow.com/q/355670/1048572) at [Programmers.SE] – Bergi Dec 15 '15 at 13:39
  • Some people are claiming nesting will increase if you use if else and return points will decrease from the code and vice versa. Still not clear. – Imran Ahmad Dec 15 '15 at 13:44
  • You might be also interested in general coding styles for if statement. Check this article https://timetocode.wordpress.com/2020/05/08/code-style-academy-how-to-spoil-if-statement/ – Mutex May 08 '20 at 20:47

3 Answers3

3

It may well have been a decision made in order to avoid nesting. In your example it's not immediately apparent, but if there are further IF, LOOP, etc. statements further down in the registration logic, then nesting start to occur.

As a general rule nesting should be avoided and refactored away as it hinders comprehension and is often indicative that the method is doing too much.

e.g.

if ( user_is_registered )
{
    // do something & return
}
else
{
   // do something else

   if ( some_other_condition )
   {
      // do another thing

      while (something_is_not_true)
      {
        // loopy things
      }
   }
}
Pseudonymous
  • 839
  • 5
  • 13
  • The code I presented is pretty simple, but the actual code is very lengthy and will grow with the growing business. – Imran Ahmad Dec 15 '15 at 13:35
  • But if we avoid nesting the number of return points will increase dramatically according to [this article](http://stackoverflow.com/questions/355670/is-returning-early-from-a-function-more-elegant-than-an-if-statement) – Imran Ahmad Dec 15 '15 at 13:40
  • For sure you will have more return statements with reduced nesting - personally I think it's a relatively small price to pay for increased readability and comprehension. – Pseudonymous Dec 15 '15 at 13:52
2

When you use an else you're building logic that is very explicit. By having separate if statements you can apply multiple blocks of rules on given conditions.

It could be that the object being compared meets multiple requirements, so Else would not be any good.

Take this example:

    var x = 10;

    if (x < 11){
        // do something
        // - this gets hit
    }else{
        // do something else
    }

    // perhaps i want to have multiple conditions that x meets..
    if (x < 11){
       // do something
        // - this gets hit
    }

    if {x == 10){
      // do something
      // - this gets hit - provided the if before didn't make changes to X
    }

    if (x != 10){
       // do something - this won't be hit, unless a previous if made changes to x
    }

Now - when you take your particular example, //New user's registration code goes here in your first block will ALWAYS fire if there wasn't a way to exit out of the method, as there is in your if. In your 2nd block it only fires if the if doesn't match.

Personally, I would wrap in the if/else, in this case and be explicit with the code and the intent.

Darren Wainwright
  • 30,247
  • 21
  • 76
  • 127
0

If you want a block of code to be executed only if the condition in if statement fails, then add the block as else part of the if statement. The statements are exclusive. Only the if block or else block is executed not both.

If you want the block of code to be always executed, include the block after if statement. The block is always executed.

aliasm2k
  • 883
  • 6
  • 12