Nested if and elsif and else

Nested if and elsif and else

am 14.04.2010 01:35:50 von Mimi Cafe

------=_NextPart_000_0027_01CADB6A.6F583AF0
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: 7bit

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..

}





Mimi


------=_NextPart_000_0027_01CADB6A.6F583AF0--

Re: Nested if and elsif and else

am 14.04.2010 02:36:29 von Jim Gibson

On 4/13/10 Tue Apr 13, 2010 4:35 PM, "Mimi Cafe"
scribbled:

> I think this will work, but is it elegant.?

Yes, it will work, and yes, it is elegant, as long as it encapsulates the
logic that is required by your program.

Be sure and watch your indenting, so you can mentally group the correct
branches together (you are a little off in your first 'else'). Also be sure
and put some comments for each conditional that explains what is happening
if it is not obvious (but don't just repeat what is in the condition).

Make sure the 'do this', 'do that', and 'do something else' are not too
long. If they are more than a few statements, use subroutines (with good
names).

> If (condition){
>
> if (nexted_condition){
>
> do this.
>
> }
>
> Elsif (nexted_condition){
>
> Do that...
>
> }
>
> else{
>
> Do something else.
>
> }
>
>
>
> }
>
> else{
>
> Do something else..
>
> }



--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Nested if and elsif and else

am 14.04.2010 04:54:42 von Uri Guttman

>>>>> "JG" == Jim Gibson writes:

JG> On 4/13/10 Tue Apr 13, 2010 4:35 PM, "Mimi Cafe"
JG> scribbled:

>> I think this will work, but is it elegant.?

JG> Yes, it will work, and yes, it is elegant, as long as it encapsulates the
JG> logic that is required by your program.

i disagree that it is elegant. too often if/else lists are not
needed. many can be replaced by dispatch tables. if one of the clauses
does just a return or next/last that can be replaced with a modifier or
shorter statement. without ANY serious work, i have over 10k lines of
perl code in one system with about 10 else's and maybe 3 elsif's. it
just is a matter of knowing how to manage flow control well and you
rarely need else's.

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Nested if and elsif and else

am 14.04.2010 05:17:49 von Kenneth Wolcott

Hi;

On Tue, Apr 13, 2010 at 19:54, Uri Guttman wrote:
>>>>>> "JG" == Jim Gibson writes:
>
> =A0JG> On 4/13/10 Tue =A0Apr 13, 2010 =A04:35 PM, "Mimi Cafe" ooglemail.com>
> =A0JG> scribbled:
>
> =A0>> I think this will work, but is it elegant.?
>
> =A0JG> Yes, it will work, and yes, it is elegant, as long as it encapsula=
tes the
> =A0JG> logic that is required by your program.
>
> i disagree that it is elegant. too often if/else lists are not
> needed. many can be replaced by dispatch tables. if one of the clauses
> does just a return or next/last that can be replaced with a modifier or
> shorter statement. without ANY serious work, i have over 10k lines of
> perl code in one system with about 10 else's and maybe 3 elsif's. it
> just is a matter of knowing how to manage flow control well and you
> rarely need else's.
>
> uri

I really like the switch statement (native in Perl v5.10) over
anything more complicated than one if/else clause.

Ken Wolcott

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Nested if and elsif and else

am 14.04.2010 09:13:19 von Shlomi Fish

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: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

RE: Nested if and elsif and else

am 14.04.2010 13:49:05 von Mimi Cafe

Yes, the nested if and elsif and else makes the code difficult to read and I
often get stuck trying to make sense of it all. For now, I will look to see
if I can move some bit and pieces to subroutines to improve readability.

Thanks guys
Mimi

-----Original Message-----
From: Jim Gibson [mailto:jimsgibson@gmail.com]
Sent: 14 April 2010 01:36
To: beginners@perl.org
Subject: Re: Nested if and elsif and else

On 4/13/10 Tue Apr 13, 2010 4:35 PM, "Mimi Cafe"
scribbled:

> I think this will work, but is it elegant.?

Yes, it will work, and yes, it is elegant, as long as it encapsulates the
logic that is required by your program.

Be sure and watch your indenting, so you can mentally group the correct
branches together (you are a little off in your first 'else'). Also be sure
and put some comments for each conditional that explains what is happening
if it is not obvious (but don't just repeat what is in the condition).

Make sure the 'do this', 'do that', and 'do something else' are not too
long. If they are more than a few statements, use subroutines (with good
names).

> If (condition){
>
> if (nexted_condition){
>
> do this.
>
> }
>
> Elsif (nexted_condition){
>
> Do that...
>
> }
>
> else{
>
> Do something else.
>
> }
>
>
>
> }
>
> else{
>
> Do something else..
>
> }



--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/




--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Nested if and elsif and else

am 14.04.2010 17:41:59 von Steve Bertrand

On 2010.04.13 23:17, Kenneth Wolcott wrote:
> Hi;
>
> On Tue, Apr 13, 2010 at 19:54, Uri Guttman wrote:
>>>>>>> "JG" == Jim Gibson writes:
>>
>> JG> On 4/13/10 Tue Apr 13, 2010 4:35 PM, "Mimi Cafe"
>> JG> scribbled:
>>
>> >> I think this will work, but is it elegant.?
>>
>> JG> Yes, it will work, and yes, it is elegant, as long as it encapsulates the
>> JG> logic that is required by your program.
>>
>> i disagree that it is elegant. too often if/else lists are not
>> needed. many can be replaced by dispatch tables. if one of the clauses
>> does just a return or next/last that can be replaced with a modifier or
>> shorter statement. without ANY serious work, i have over 10k lines of
>> perl code in one system with about 10 else's and maybe 3 elsif's. it
>> just is a matter of knowing how to manage flow control well and you
>> rarely need else's.
>>
>> uri
>
> I really like the switch statement (native in Perl v5.10) over
> anything more complicated than one if/else clause.

I agree with Uri. Even switch statements can be cumbersome depending on
how many cases you have. I believe that (in the majority of cases)
dispatch tables are far more effective, easy to read (like a table of
contents) and maintainable (ie. very easy to add to without having to
worry about placement).

#!/usr/bin/perl

use warnings;
use strict;

my $dt = {
simple => sub { print "Simple, anon sub inline\n" },
easy => sub { my $num = 1; print $num*2 ."\n";},
complex => \&complex, # coderef to external sub,
};

sub complex {
my $num = shift;
# ... do a bunch of stuff
print "$num\n";
}

# call them

$dt->{ simple }();
$dt->{ easy }();
$dt->{ complex }( 5 );

Steve


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Nested if and elsif and else

am 14.04.2010 19:01:43 von Raymond Wan

Hi Mimi,


Mimi Cafe wrote:
> I think this will work, but is it elegant.?
>
> If (condition){
>
> if (nexted_condition){


As others have noted, it will work. But as for elegance, this is a very subjective opinion and
contrary to what recent comments have said, my take on it is that it depends on who intends to look
at this code and what their background is. Is it just you? Or publicly available open source for
potentially everyone? Or just the members of your software engineering team? Are they people with
good Perl backgrounds or are their backgrounds varied and maybe might prefer if...else and switch,
constructs that are available in other languages.

I personally prefer switch and if..else (in that order) since they are based on keywords which my
syntax highlighter picks up easily and are even shown when I print the code out. Also, I can grep
for, if I need to.

Regardless of what you choose, you should also use comments before the block of code to explain what
it is you're doing...

Ray


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Nested if and elsif and else

am 15.04.2010 17:49:20 von Harry Putnam

"Uri Guttman" writes:

> i disagree that it is elegant. too often if/else lists are not
> needed. many can be replaced by dispatch tables. if one of the clauses
> does just a return or next/last that can be replaced with a modifier or
> shorter statement. without ANY serious work, i have over 10k lines of
> perl code in one system with about 10 else's and maybe 3 elsif's. it
> just is a matter of knowing how to manage flow control well and you
> rarely need else's.

Can someone show an example of an if/elsif/else nested construct being
replaced by a dispatch table?


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Nested if and elsif and else

am 15.04.2010 18:02:39 von Ron Bergin

Harry Putnam wrote:
> "Uri Guttman" writes:
>
>> i disagree that it is elegant. too often if/else lists
>> are not
>> needed. many can be replaced by dispatch tables. if one
>> of the clauses
>> does just a return or next/last that can be replaced
>> with a modifier or
>> shorter statement. without ANY serious work, i have over
>> 10k lines of
>> perl code in one system with about 10 else's and maybe 3
>> elsif's. it
>> just is a matter of knowing how to manage flow control
>> well and you
>> rarely need else's.
>
> Can someone show an example of an if/elsif/else nested
> construct being
> replaced by a dispatch table?
>
> --

Here's an example I gave in a similar question in another
forum.

my %dispatch = (
1 => \&getcpuinfo,
2 => \&osversion,
3 => \&loadaverages,
4 => \&systemload_uptime,
5 => \&netinterfaceinfo,
6 => \&diskusage,
7 => \&ipaddress,
q => sub { print "Goodbye\n" and exit; },
error => sub { print "invalid selection\n" },
);

while(1)
{
print "press 1 to get CPU Info \n",
"press 2 to get OS version \n",
"press 3 to get CPU Load averages\n",
"press 4 to get System Load & Uptime\n",
"press 5 to get Net Interface info\n",
"press 6 to get system disk usage info \n",
"press 7 to get IP address info \n";
"press q to Exit\n"

chomp(my $selection) = ;

my $code = $dispatch{$selection} || $dispatch{'error'} ;
$code->();
}


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Nested if and elsif and else

am 15.04.2010 18:21:16 von Harry Putnam

rkb@i.frys.com writes:

> Here's an example I gave in a similar question in another
> forum.

Thanks...

I'm sorry to ask more but if someone asked to be shown an
if/elsif/else construct being replaced by a dispatch table, I don't
really see how that answered there question. It didn't for me.

Where is the comparable if/elsif/else construct that is being replaced
by the dispatch table?

Visualizing how it would go is a little beyond my grasp I guess.


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Nested if and elsif and else

am 15.04.2010 18:43:52 von Jim Gibson

On 4/15/10 Thu Apr 15, 2010 9:21 AM, "Harry Putnam"
scribbled:

> rkb@i.frys.com writes:
>
>> Here's an example I gave in a similar question in another
>> forum.
>
> Thanks...
>
> I'm sorry to ask more but if someone asked to be shown an
> if/elsif/else construct being replaced by a dispatch table, I don't
> really see how that answered there question. It didn't for me.
>
> Where is the comparable if/elsif/else construct that is being replaced
> by the dispatch table?
>
> Visualizing how it would go is a little beyond my grasp I guess.


Something like this:

print "press 1 to get CPU Info \n",
"press 2 to get OS version \n",
"press 3 to get CPU Load averages\n",
"press 4 to get System Load & Uptime\n",
"press 5 to get Net Interface info\n",
"press 6 to get system disk usage info \n",
"press 7 to get IP address info \n";
"press q to Exit\n"

chomp(my $selection) = ;
if( $selection eq '1' ) {
getcpuinfo();
}elsif( $selection eq '2' ) {
osversion();
}elsif{ $selection eq '3' ) {
loadaverages();
}elseif( $selection eq '4' ) {
systemload_uptime();
]elsif(
...
}else{
print "Dispatch error\n";
);





--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Nested if and elsif and else

am 15.04.2010 19:17:07 von Ron Bergin

On Apr 15, 9:21=A0am, rea...@newsguy.com (Harry Putnam) wrote:
> r...@i.frys.com writes:
> > Here's an example I gave in a similar question in another
> > forum.
>
> Thanks...
>
> I'm sorry to ask more but if someone asked to be shown an
> if/elsif/else construct being replaced by a dispatch table, I don't
> really see how that answered there question. =A0It didn't for me.
>
> Where is the comparable if/elsif/else construct that is being replaced
> by the dispatch table?
>
> Visualizing how it would go is a little beyond my grasp I guess.

Sorry for not posting the if/elsif/else block, but to me that part
appeared to be obvious, but I guess it wasn't.

I see that Jim has posted the if/elsif/else part, so I won't duplicate
it.


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Nested if and elsif and else

am 15.04.2010 20:25:57 von Harry Putnam

Ron Bergin writes:

> Sorry for not posting the if/elsif/else block, but to me that part
> appeared to be obvious, but I guess it wasn't.

Probably would have been for all but the densist I guess. Not the
first time I've been guilty of that.

> I see that Jim has posted the if/elsif/else part, so I won't duplicate
> it.

Yes, and thanks to both of you for making it clear even to me.

You fella's have lots of patience.


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/