Thread: Curly brackets!
View Single Post
Old 05-06-2009, 12:45 PM   #6 (permalink)
Salathe
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,239
Thanks: 3
Salathe is on a distinguished road
Default

Whilst removing curly braces where they're not strictly necessary might seem like a good idea, in practice it's all too easy run into problems as a result. I guess my most practical advice would be to use good indentation in your code.

For example:
PHP Code:
if (condition) {
if (
condition)
// do something
else
// do something else
} else {
// do something else entirely

Compared to:
PHP Code:
if (condition)
{
  if (
condition)
  {
    
// do something
  
}
  else
  {
    
// do something else
  
}
}
else
{
    
// do something else entirely

Also, don't try to nest things too deeply. If there are lots of nested blocks it can quickly get confusing as to what the current block does and when. Code can often be rewritten to keep the nesting fairly shallow and clear as to what is going on. If there are 200 lines (just an arbitrary number) between opening and closing braces of an if statement, perhaps it's time to rethink the structure.
__________________
salathe@php.net
Salathe is offline  
Reply With Quote