Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

(crazyinsomniac) Re: Coding superstitions

by crazyinsomniac (Prior)
on Jan 08, 2002 at 02:47 UTC ( #136985=note: print w/ replies, xml ) Need Help??


in reply to Coding superstitions

What do you do at the end of a sub?

sub Hello { "I am a sub"; }
I hate that. Nothing particularly wrong with it, it's valid perl, except that I hate it. The following is more to my liking
sub Hello { return "No! *I* am a sub."; }
Hashkeys are a little more forgiving, because how often are you really going to have s p a c e s =>in between, or some such nonsense like others have pointed out, but as with my example above, I'll take being explicit any time over being implicit. I also like to do the following (mainly for visual effect):
$fooBar->Fooz( 'abc'    => 1,
               'aa0ec'  => 221,
               'abae'   => 12,
               'aecadf' => 145,
             ,)->pack( 'ab' => 11,
                       'cd' => 22,
                     ,);

 
______crazyinsomniac_____________________________
Of all the things I've lost, I miss my mind the most.
perl -e "$q=$_;map({chr unpack qq;H*;,$_}split(q;;,q*H*));print;$q/$q;"


Comment on (crazyinsomniac) Re: Coding superstitions
Select or Download Code
Re: (crazyinsomniac) Re: Coding superstitions
by n3dst4 (Scribe) on Jan 08, 2002 at 21:21 UTC
    I know what you mean about returning things. I think it's a deep brainwash from all other languages that says unused expressions are useless. Then there's the case where you can't tell at a glance which line acually does the returning. I like returns.

    On a related (im|ex)plicit note, I argued once when I was at uni about this code:

    sub fred { if ($_[0] == 'ooyah') { return 1; } else { return 0; } }
    The lecturer insisted that I should take out the else and leave the last return dangling there. I still don't know which I prefer.
      Assuming you don't mind returning 1/undef instead of 1/0 I'd prefer this:
      sub fred { $_[0] eq 'ooyah' }
      Or, if you want to keep your '0', how about:
      sub fred { $_[0] eq 'ooyah' or 0 }

      -Blake

        Well caught; the class was actually in Java, but I transliterated the example.
      I would replace that entire routine with the ? thingie, as in
      sub fred { return ( ( $_[0] eq 'ooyah' ) ? 1 : 0 ); }
      Gee, I didn't realize until I was replying that you used == and I corrected that to eq. Not bad for some guy who wrote C code forever.

      And I disagree with the lecturer about leaving the last return to dangle -- what he/she suggested is obsfucation, and you're not doing yourself any favours with that approach.

      --t. alex

      "Excellent. Release the hounds." -- Monty Burns.

      FWIW, I prefer your version, simply because my brain effortlessly translates the else into $_[0] != 'ooyah', so it is immediately clear to me that the block gets executed if and only if $_[0] != 'ooyah'. I think I can understand the prof's desire for the "dangling return", but I also think it would require a few more clock cycles for my brain to look at that and be convinced exactly under which conditions 0 would be returned. (At the very least, I'd have to see the return 1 above to know that the return 0 would never get executed if the if was true -- because the if might just have done some work without actually returning...)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2015-07-07 03:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (87 votes), past polls