Make Contact

Close
Invalid Input
Please type your name. Please type your company Invalid email address.
Invalid Input

Make Contact

608.294.5460
634 W Main St., Ste 201
Madison, WI  53703

Map
[email protected]

Feb 02 2015
Dan Greig

Dan Greig

Team Lead / Developer

Preferences: Brackets

There are many ways to “successfully” write code if your only goal is to communicate with a computer. But computers aren’t your only audience, you are also communicating with other programmers that will need to understand the code you’ve written. Some coding styles are easier to read, making it quicker and less complicated to maintain or make changes. Here, I talk about brackets and the most straightforward way to use them.

I contend that in all languages that support it, brackets should always be placed on the next line at the same indentation as the conditional that they go with; e.g. for a simple ‘if’ statement the bracket placement would look like this:

 
if( $var1 > 0 ) 
{
	$var1  = 1;
}

This is very easy to read as your eye can simply scan down and see where the matching bracket is. Let’s make this a little more complicated and nest a few statements inside of the if statement.

Now here are some other ways the above code could be written:

if( $var1 > 0 ) 
{
     $var1 = 1;
     if( $var2 > 0 )
     {
          $var1  = 2;
          if( $var2 > 0 )
          {
               $var1  = $var2;
          }else
          {
               $var2 = $var1;
          }
          if( $var3 > 0 )
          {
               $var2  = $var1;
          }
     }
}

A

if( $var1 > 0 ) {
     $var1 = 1;
     if( $var2 > 0 ) {
          $var1  = 2;
          if( $var2 > 0 ) {
               $var1  = $var2;
          }else {
               $var2 = $var1;
          }
          if( $var3 > 0 ) {
               $var2  = $var1;
          }
     }
}

B

if( $var1 > 0 ):
     $var1 = 1;
     if( $var2 > 0 ):
          $var1  = 2;
          if( $var2 > 0 ):
               $var1  = $var2;
          else:
               $var2 = $var1;
          endif;
          if( $var3 > 0 ):
               $var2  = $var1;
          endif;
     endif;
endif;

C

All three of those cases do the exact same thing when the computer runs them. But the example above is easiest to read as it has space between lines. 

Some people prefer the last style(C) because it replaces the brackets with a description of which piece of code is ending. That can be useful if you have multiple types of bracketed conditionals nested. Something like this:

	foreach($items as $item):
     if($item > 0):
          echo $item;
     endif;
endforeach;

If we were to write that with brackets it would look like this:

	foreach($items as $item)
{
     if($item > 0)
     {
          echo $item;
     }
}

Recognizing the value of this approach, especially when there is a lot of code in the foreach, it could be difficult to figure out what an end bracket meant if the previous one was off your screen. However, there is an easy solution to this problem and it’s something you should be doing... Comments!

With comments my code would look like this:

foreach($items as $item)
{
     if($item > 0)
     {
          echo $item;
     } //end if
} //end foreach $items

This offers the quick matching of brackets and if I need more information, it’s right there.

Throughout this post you will also notice the stringent use of proper indentation on the code, I will leave the discussion of whether to use tabs or spaces to someone else ☺ Without indentation the computer would also run this just fine and it would do exactly what the above code does:

if( $var1 > 0 ) { $var1 = 1; if( $var2 > 0 ) { $var1  = 2; if( $var2 > 0 ) { $var1  = $var2; } else { $var2 = $var1; } if( $var3 > 0 ) { $var2  = $var1; } } }

But who wants to look at that code and try and figure out when $var2 is set to $var1?

Code quality matters, let’s make the internet a better place and use good code formatting habits. You will thank yourself in the future and so will your co-workers.

Make Contact

Looking for a team to help your idea take flight?
Get in touch and we'll talk it out.

Phone or Email

(608) 294-5460

Address

Earthling Interactive
634 W Main St., Ste 201
Madison, WI 53703