Re: Nested if and elsif and else
Uri Guttman u...@stemsystems.com 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-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Nested if and elsif and else
Harry Putnam wrote: Uri Guttman u...@stemsystems.com 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) = STDIN; my $code = $dispatch{$selection} || $dispatch{'error'} ; $code-(); } -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Nested if and elsif and else
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. 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-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Nested if and elsif and else
On 4/15/10 Thu Apr 15, 2010 9:21 AM, Harry Putnam rea...@newsguy.com scribbled: 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. 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) = STDIN; 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-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Nested if and elsif and else
On Apr 15, 9:21 am, 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. 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. 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-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Nested if and elsif and else
Ron Bergin r...@i.frys.com 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-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Nested if and elsif and else
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-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
RE: Nested if and elsif and else
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:jimsgib...@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 mimic...@googlemail.com 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-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/ -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Nested if and elsif and else
On 2010.04.13 23:17, Kenneth Wolcott wrote: Hi; On Tue, Apr 13, 2010 at 19:54, Uri Guttman u...@stemsystems.com wrote: JG == Jim Gibson jimsgib...@gmail.com writes: JG On 4/13/10 Tue Apr 13, 2010 4:35 PM, Mimi Cafe mimic...@googlemail.com 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-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Nested if and elsif and else
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-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Nested if and elsif and else
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
Re: Nested if and elsif and else
On 4/13/10 Tue Apr 13, 2010 4:35 PM, Mimi Cafe mimic...@googlemail.com 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-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Nested if and elsif and else
JG == Jim Gibson jimsgib...@gmail.com writes: JG On 4/13/10 Tue Apr 13, 2010 4:35 PM, Mimi Cafe mimic...@googlemail.com 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 -- u...@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-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Nested if and elsif and else
Hi; On Tue, Apr 13, 2010 at 19:54, Uri Guttman u...@stemsystems.com wrote: JG == Jim Gibson jimsgib...@gmail.com writes: JG On 4/13/10 Tue Apr 13, 2010 4:35 PM, Mimi Cafe mimic...@googlemail.com 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. Ken Wolcott -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/