Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Two simple code style advice questions

by blue_cowdawg (Monsignor)
on Jan 16, 2013 at 14:33 UTC ( #1013581=note: print w/ replies, xml ) Need Help??


in reply to Two simple code style advice questions

I like code that I can see exactly what is going on without going back and forth in the lines of code. Especially when a script becomes gargantuan. In your first case:

my %tests = map { $_ => 0 ) qw/ tfred tjock tfortytwo / ;
would be my preferred style. I could also go with:
my $tests ={}; map { $tests -> { $_ } = 0 } qw/ tfred tjock tfortytwo /;
as long as those two lines are next to each other in the code. Passing a reference to a hash to subs later on is more readable to my eyes that something like:
callToSub(\%tests);
but that is a personal preference.

In your second case I am a big fan of

my $errstr = ( $case != OK ? $msg[$case] : "" );
kinds of things. (Line right out of one of my projects). The term OK is the result of a stack of use constant OK =>0; sorts of things where I use constants to aid in my code's readability.


Peter L. Berghold -- Unix Professional
Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg


Comment on Re: Two simple code style advice questions
Select or Download Code
Re^2: Two simple code style advice questions
by BrowserUk (Pope) on Jan 16, 2013 at 14:43 UTC

    OK defined as 0? Yuck!

    That's as bad as defining TRUE as 0.

    It would be far clearer as:

    use constant NOERROR => 0; my $errstr = ( $case != NOERROR ? $msg[$case] : "" );

    Though I'd skip that conditional statement completely and embed the logic in the data:

    $msg[ 0 ] = ''; ... my $errstr = $msg[ $case ];

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
          OK defined as 0? Yuck!

      It would seem to me that most Unix commands return 0 when things are "OK." So.. if you want to accuse me of showing my C programming roots, I plead guily.


      Peter L. Berghold -- Unix Professional
      Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
        Why even bother defining a constant ;)
        no warnings; if (0 == OK) { print 'Everything is ok here!'; }
        It would seem to me that most Unix commands return 0 when things are "OK." So.. if you want to accuse me of showing my C programming roots, I plead guily.

        I understand the derivation, but still, the bland token OK has always suggested TRUE to me, and always will.

        Hence my suggested alternative of use constant NOERROR => 0; which seems less likely to cause confusion.

        That said, I wouldn't define a constant for 0 at all. It simply isn't necessary to compare against zero in Perl. The clearest idiom is simply:

        my $errstr = $case ? $msg[ $case ] : '';

        But I'd still prefer setting $msg[ 0 ] == ''; and simply:

        my $errstr = $msg[ $case ];

        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
        It would seem to me that most Unix commands return 0 when things are "OK."

        BUT, (and I'm still trying to get the developers here to overcome this), in shell programming, zero IS true (e.g. consider && and || in things like cmd1 && cmd2 or cmd1 || cmd2, and Perl is not shell.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1013581]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (7)
As of 2014-12-25 14:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (160 votes), past polls