Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re: Perverse Unreadable Code

by Chady (Priest)
on Apr 25, 2001 at 23:27 UTC ( #75574=note: print w/replies, xml ) Need Help??

in reply to Perverse Unreadable Code

I couldn't help not noticing the :

"I have also seen people doing horrors like putting if statements after the code to be excuted "
where's the horror in that???

Code is more aesthetical like this :

if ($var) { &doSomthing; }
has the (same) effect as:
&doSomething if ($var);
both are readable enough, and the second is closer to spoken language. and is less written code.
He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

Chady |

Replies are listed 'Best First'.
(Ovid - 'if' after the statement) Re(2): Perverse Unreadable Code
by Ovid (Cardinal) on Apr 25, 2001 at 23:41 UTC
    I've also heard this complain and I agree with you: it stems from people so used to other languages that it looks foreign.

    Personally, I tend not to put the if after the statement unless the statement will be executed most of the time. Then when it comes to maintenance and someone is scanning the code, they will often see that and understand that it's the norm rather than the exception.


    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: Re: Perverse Unreadable Code
by boo_radley (Parson) on Apr 25, 2001 at 23:46 UTC
    &doSomething if ($var);
    This was the construct that tripped me up the most, along with its brother unless, when being brainwashed coming over to perl.
    the typical
    if ($foo) { ... ... }
    sets the reader up -- he knows something might happen, but if he's unfamiliar with the alternate synax, that fact might pass him by. The end result is that I read code a little more thoroughly, and I've gotten used to it, so I guess it's not a total loss.
Re: Re: Perverse Unreadable Code
by snafu (Chaplain) on Jul 06, 2001 at 23:07 UTC
    I couldn't agree more with you. For instance, take the following function I wrote:

    for ( @status ) { my $pass = 0; chomp; # eliminate that nasty terminator! :) $_ =~ s/\0//; # Ignore the stuff that comes from this # command for now. Maybe later I will # use this information for something but # I will wait until a more mature release # of this code. if ( $watch ) { # I know this next section looks obfuscated # but actually, it was the easiest way for me # to accomplish my task here. # This block takes data that looks like this: # # player name number number # IPaddr number number <- one record # player name number number # IPaddr number number <- another record # # and parses out just the name per record # so that the array is now propagated with # player name <- from reocrd 1 # player name <- from record 2 # etc. # NOTE to self: You will probably need to get # the IP address at some point here! $pass = 1 if $pass == 0; $pass = 2, $_ =~ s/( .*).*// if $pass; $pass = 1 # place regex to extract IP here if $pass == 2; push( @player_info, $_ ) if $_; } # start counting after we see the "-"'s $watch = 1 if ( /-/ ); } if ( $#player_info >= 0 ) { return($#player_info + 1); } else { return(0); } }
    Notice the lil block of if statements?

    $pass = 1 if $pass == 0; $pass = 2, $_ =~ s/( .*).*// if $pass; $pass = 1 # place regex to extract IP here if $pass == 2; push( @player_info, $_ ) if $_;

    To me this seemed to be a much simpler block of code to write while at the same time not too difficult to understand what it was doing. Consider the alternative:

    A rough estimation of the translation of the above code (untested)...

    if ( $pass == 0 ) { $pass = 1; } elsif ( $pass ) { $pass = 2; $_ =~ s/( .*).*//; } elsif ( $pass == 2 ) { $pass = 1; } if ( $_ ) { push( @player_info, $_ ); }

    I'd be curious to know what others think.
    I wonder if I could have used the flip-flop operator here.

    - Jim

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://75574]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (2)
As of 2018-05-28 04:07 GMT
Find Nodes?
    Voting Booth?