Home > Computer Programming > Formatting Your Functions

Formatting Your Functions

I’ve noticed in a lot of code people tend to write entire functions within an if/else block, particularly when error-checking must be done first-and-foremost in the function (like verifying parameters). I write my code differently; let me explain why.

To show you what I’m talking about, I’ve created a basic example:

public double calculateMass(Object something)
{
     if(something != null)
     {
          // Perform calculations
     } else {
          // Error
     }
}

This is poorly formatted for several reasons:

  1. Someone reading this code must scroll past the entire if block to see the else block. It should be clear from the name of the function what the if block does, but it is not clear how the function reacts to errors, which are in the else block.  The reader must skip over the giant if block to see the else block, hidden at the bottom under a giant formula of physical calculations.
  2. This indentation is unnecessary and makes the code harder to read. Anyone who’s written code knows things get messier the more they’re nested in blocks.  In the example, the code is immediately nested, already making further blocks 1-level deep – off to a bad start.
  3. An if-else wrapping the entire function implies the function has 2 calculations. Depending on some condition the function will perform a different calculation; one calculation in the if block and the other in else block.  In the example, this is not the case; the else block contains nothing relevant to the calculation.  The calculateMass function only has 1 calculation: to calculate the mass of a given object – there is no ‘else.’

To better highlight that the function has only 1 purpose, this “main” code should begin in the 0-level indentation of the function, i.e. it should be flush with the left-most indentation of the function. For example,

public double calculateMass(Object something)
{
     // 0-level
          // 1-level
               // 2-level
                    // 3-level
}

To handle the error-checking and remove the else block, a simple if block with a negation should be used prior to the “main” code.  For example,

public double calculateMass(Object something)
{
     // This is the negation of the original condition,
     // 'something != null'
     if(something == null)
     {
          // Error
     }

     // Perform calculation
}

With this format, it is clear to anyone first viewing the code how the function handles errors. Since they can already assume that the code that follows will calculate mass, there is nothing left unknown to the reader at this point (aside from implementation details).

Got an objection? Got your own formatting you prefer? Share it in a comment.

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: