[development] comment module: minor issue unreachable code

Doug Green douggreen at douggreenconsulting.com
Wed Aug 22 13:41:27 UTC 2007


Bèr Kessels wrote:
> Op woensdag 22 augustus 2007, schreef Earnie Boyd:
>   
>> As you can see I don't think break is always a good thing, it should
>> either be return or break and return should not be embodied in the
>> case.  
>>     
>
> having more then one return in a function is generally a (very) bad practice. 
> It makes the code hard to read, and far more complex then needed.
>
> Bèr
>   
I have followed this coding practice (one return) for a long time 
(almost 20 years) for the reason Ber mentions -- it makes it easier to 
maintain and debug.  Returns that are embedded 10 levels deep within 
loops and conditionals are harder to find while debugging. 

However, I do not support making this a coding standard -- maybe just a 
suggestion. 

Since we code in an interpretive language, fewer lines of code has it's 
benefit.  And it is certainly easier to code a return statement than to 
set a flag and/or return value, issue a break, and make sure that the 
code breaks properly to the final end. 

What I think is needed is some suggestions and some code samples.  If 
you make it a coding standard, then I'll probably add it to coder, and I 
think we'll be flagging lots of innocuous returns.

For example, I do prefer:

  function example() {
    if (variable_get('example', 0)) {
       // ... do something and return
       return 'something';
    }
    // ... falls off the end with an empty return
  }

to

  function example() {
    if (!variable_get('example', 0)) {
      return;
    }
     return 'something';
  }

Partially for the single return, but also because the logic is cleaner.

-- 
Doug Green
douggreen at douggreenconsulting.com
904-583-3342

Bringing Ideas to Life with Software Artistry and Invention...



More information about the development mailing list