Monday, March 7, 2016

Useful PHP Tips Revisited Link Day 4

4. Drop Those Brackets Link

Based on the content of this tip, we believe the author means “braces,” not brackets. “Curly brackets” may mean braces to some, but “brackets” universally means “square brackets.”
This tip should be unconditionally ignored. Without braces, readability and maintainability are damaged. Consider a simple example:
<?php
 
if (date('d M') == '21 May')
    $birthdays = array('Al Franken',
                       'Chris Shiflett',
                       'Chris Wallace',
                       'Lawrence Tureaud');
 
?>
If you’re good enough, smart enough, secure enough, notorious enough, or pitied enough, you might want to party on the 21st of May:
<?php
 
if (date('d M') == '21 May')
    $birthdays = array('Al Franken',
                       'Chris Shiflett',
                       'Chris Wallace',
                       'Lawrence Tureaud');
    party(TRUE);
 
?>
Without braces, this simple addition causes you to party every day. Perhaps you have the stamina for it, so the mistake is a welcome one. Hopefully, the silly example doesn’t detract from the point, which is that the excessive partying is an unintended side effect.
In order to promote the practice of dropping braces, the previous article uses short examples such as the following:
<?php
 
if ($gollum == 'halfling') $height --;  
else $height ++;
 
?>
Because each condition is constrained to a single line, such mistakes might be less likely, but this leads to another problem: inconsistencies are jarring and require more time to read and comprehend. Consistency is such a valued quality that developers often abide by a coding standard even if they dislike the coding standard itself.
We recommend always using braces:
<?php
 
if (date('d M') == '21 May') {
    $birthdays = array('Al Franken',
                       'Chris Shiflett',
                       'Chris Wallace',
                       'Lawrence Tureaud');
    party(TRUE);
}
 
?>
 
You’re welcome to party every day, but make sure it’s deliberate, and please be sure to invite us!

Further more details: 
  • PHP TRAINING IN KATHMANDU
  • PHP TRAINING IN LALITPUR
  • No comments:

    Post a Comment