Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Re: (crazyinsomniac) Re: Coding superstitions

by n3dst4 (Scribe)
on Jan 08, 2002 at 21:21 UTC ( #137185=note: print w/replies, xml ) Need Help??

in reply to (crazyinsomniac) Re: Coding superstitions
in thread Coding superstitions

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.

Replies are listed 'Best First'.
Re: Re: (crazyinsomniac) Re: Coding superstitions
by blakem (Monsignor) on Jan 09, 2002 at 00:24 UTC
    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 }


      Well caught; the class was actually in Java, but I transliterated the example.
Re: Re: (crazyinsomniac) Re: Coding superstitions
by talexb (Canon) on Jan 09, 2002 at 19:37 UTC
    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.

Re: Re: (crazyinsomniac) Re: Coding superstitions
by seattlejohn (Deacon) on Jan 15, 2002 at 12:24 UTC
    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?

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2018-02-25 22:03 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (315 votes). Check out past polls.