Flag This Hub

An Alternative to the Nested IF and the Dreaded goto

By


Introduction

An important part of coding is to make it easy to support. This means, at a minimum, the code is logical and easy to follow. One area that can make this difficult is a large nested if. Here is a simple C++ example of a function that adds a user to a database.

int addUser(String userName, int flags)
{
   int  result = 0;
   
   if (isAdministrator() == true)
   {
      if (isValid(userName) == true)
      {
         UserRecord *record = new UserRecord();
         record->name = userName;
         record->flags = flags;
         
         if (insertUserInDatabase(record) == true)
         {
            Log("User Added Successfully");
         }
         else
         {
            delete record;
            
            Log("Unable to add User to Database");
            result = -1;
         }
      }
      else
      {
         Log("User Name is Invalid");
         result = -2;
      }
   }
   else
   {
      Log("You are NOT Logged In as Administrator")
      result = -3;
   }
   
   return result;
}

In this example we are adding a user to a database. We must first pass some validations checks, and then return the appropriate results. While the level of nesting in this example is small, you can see how things can get out of hand as the level increases.

Using goto

Now, if we have a function where the nesting gets to be too much, we can make it easier to follow using the goto statement. Here is the same function, modified to use the goto.

int addUser(String userName, int flags)
{
   int  result = 0;
   
   if (isAdministrator() == false)
   {
      Log("You are NOT Logged In as Administrator")
      result = -3;
      goto exit;
   }
   
   if (isValid(userName) == false)
   {
      Log("User Name is Invalid");
      result = -2;
      goto exit;
   }
   
   UserRecord *record = new UserRecord();
   record->name = userName;
   record->flags = flags;

   if (insertUserInDatabase(record) == false)
   {
      delete record;
      
      Log("Unable to add User to Database");
      result = -1;
      goto exit;
   }
   
   Log("User Added Successfully");

exit:
   return result;
}

Now that I have shown you how to do this using goto - don't. While I strongly discourage the use of the goto statement, this article is not meant to be a philosophical discussion. There is plenty of information available on the subject if you are interested. If you still choose to use the goto statement then be prepared for pitchforks and torches.

Using do-while

The do-while statement is one way we can achieve a more logical flow without using the dreaded goto. Here is the same code written using the do-while:

int addUser(String userName, int flags)
{
   int  result = 0;
   
   do
   {
      if (isAdministrator() == false)
      {
         Log("You are NOT Logged In as Administrator")
         result = -3;
         break;
      }
   
      if (isValid(userName) == false)
      {
         Log("User Name is Invalid");
         result = -2;
         break;
      }
   
      UserRecord *record = new UserRecord();
      record->name = userName;
      record->flags = flags;
   
      if (insertUserInDatabase(record) == false)
      {
         delete record;
      
         Log("Unable to add User to Database");
         result = -1;
         break;
      }
      
      Log("User Added Successfully");
   } while (false);
   
   return result;
}

By using a do-while where the condition is false, we have created an "execute once" while loop that we can break out of as needed.

Summary

Programming styles are very subjective. What one sees as elegant code, another sees as a mess. The goal of this article was not to stress a particular style of coding, but rather to show an alternative to the nested if for C++ and Java. For me, the do-while method has a more logical flow. Whatever your choice, make it readable for the next guy.

Comments

IntimatEvolution 3 months ago

I don't know much about coding, frankly I know very little..., but Fred you have yourself an extremely well written hub here! Bravo big guy!!!

Submit a Comment
Members and Guests

Sign in or sign up and post using a hubpages account.



    Like this Hub?
    Please wait working