[prev in list] [next in list] [prev in thread] [next in thread] 

List:       rxtx
Subject:    Re: [Rxtx] multiple exit points.
From:       Trent Jarvi <tjarvi () qbang ! org>
Date:       2006-01-18 18:22:18
Message-ID: Pine.LNX.4.61.0601181118550.12445 () www
[Download RAW message or body]

On Wed, 18 Jan 2006, Dr. Douglas Lyon wrote:

> Hi All,
> I did hear that some people don't like multiple exit points.
> The question remains hotly contested and is probably
> off-point for this list.
>
> Is there a name for the general rule that functions or methods shouldn't have 
> multiple exit points?
> Perhaps someone would say that this is structured programming and
> it dates from the 80's. However, beyond the "we always did it this way.."
> lets keep an open mind.
>
> Let's say I have a function which validates an input. I have half a dozen 
> simple tests. My choices are a lot of nested if statements, a status variable 
> or multiple exit points. In some cases multiple exit points leads to simpler 
> clearer code.
>
> So, single-return way to do it is
>
> int foo(int arg)
> {
>    int result;
>    if (x != 0) {
>        .
>        .    
>        .
>        .
>        .
>        /* calculating result */
>    }
>    return result;
> }
>
> The multiple-return approach is
>
> int foo(int arg)
> {
>    if (x == 0) return 0;
>    .
>    .
>    .
>    .
>    .
>    return /* calculated result */
> }
>
> I like the second code better, as it
> a) expresses the intent clearly;
> b) if the code takes more than a page, enclosing all of it in the 'if' scope 
> just doesn't look good. The second approach gives code that is easier to 
> read.
>

Don't look for gotos in the native code :)

The primary goal should always be readablility in open source because it 
is our goal to have others read the code and mybe contribute after that.

Thats my opinion.

--
Trent Jarvi
tjarvi@qbang.org

_______________________________________________
Rxtx mailing list
Rxtx@qbang.org
http://mailman.qbang.org/mailman/listinfo/rxtx


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic