On Wednesday 14 Apr 2010 02:35:50 Mimi Cafe wrote:
> I think this will work, but is it elegant.?
>
>
>
> If (condition){
>
> if (nexted_condition){
>
> do this.
>
> }
>
> Elsif (nexted_condition){
>
> Do that...
>
> }
>
> else{
>
> Do something else.
>
> }
>
>
>
> }
>
> else{
>
> Do something else..
>
> }
>
As other people noted, it will work - you can nest if/elsif/else's (and other
flow-control constructs) arbitrarily. However, as Martin Fowler notes in his
book "Refactoring" ( http://www.refactoring.com/ ) long functions or methods
are a code smell which indicates that one should extract one-or-more functions
out of them. So if you have an inner conditional, consider extracting it into
a function. Often after you have such a function, you can use
<< return COND() ? TRUE_VAL() : FALSE_VAL() ; >> which can avoid further
clutter. Or you can consider using a dispatch table like Uri suggested.
I admit I often write quick-and-dirty code that has some levels of nested
constructs (primarily in mostly standalone scripts or programs) but it's
better to refactor them into smaller subroutines for more serious stuff.
Regards,
Shlomi Fish
--
-----------------------------------------------------------------
Shlomi Fish http://www.shlomifish.org/
Why I Love Perl - http://shlom.in/joy-of-perl
Deletionists delete Wikipedia articles that they consider lame.
Chuck Norris deletes deletionists whom he considers lame.
Please reply to list if it's a mailing list post - http://shlom.in/reply .
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/