Keep your brackets on dear!

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Email
  • RSS
  • Reddit

Lots of people use different names for these, I personally favour “curly brackets” though their correct term is apparently “braces”. These are usually found round conditional statements and the like in PHP. At least you HOPE they are! Quite often I come across places where they have been left out for some reason – usually lazy coding.

[code lang=”php”]

$value = “thing”;
if ($something === true) doStuff($value);

[/code]

Now alot of people would say: “It’s only one line so it’s ok to leave out the curly brackets this time and keep the code on one line to enhance performance”. Performance implications for some curly brackets is minuscule and means that the code does not flow when you read it. It makes it harder to find problems and also when you need to add some logic you can end up making a right royal mess such as:

[code lang=”php”]

if ($something === true)
$array = array(
“thing” => “apple”,

“action” => “eat”
);
$doStuff($array);

[/code]

By adding one more element to the if statement (the array) $doStuff is going to doStuff whatever the outcome of the if statement. A mistake like this becomes very very hard to spot and can be easily made when in a rush to add last minute changes or change logic in a tight deadline. By keeping the curly brackets in – coding standard is maintained and error margins reduced. Here’s how that last statement should have looked.

[code lang=”php”]

if ($something === true) {
$array = array(“thing” => “apple”, “action” => “eat”;
$doStuff($array);
}

[/code]

simples!

Leave a Reply

Your email address will not be published. Required fields are marked *

Email
Print
WP Socializer Aakash Web