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

Re^2: Wrong result for "length keys %$hashref"?

by saberworks (Curate)
on Mar 14, 2011 at 21:56 UTC ( #893177=note: print w/ replies, xml ) Need Help??


in reply to Re: Wrong result for "length keys %$hashref"?
in thread Wrong result for "length keys %$hashref"?

That's only more self-documenting if you already understand list vs. scalar context. If a junior programmer saw that they would say WTF and have no idea where to look next. If you used scalar, they might not know exactly what it did but they'd have a direct pointer to documentation that would explain exactly what the intent of the code was.


Comment on Re^2: Wrong result for "length keys %$hashref"?
Re^3: Wrong result for "length keys %$hashref"?
by chromatic (Archbishop) on Mar 14, 2011 at 22:02 UTC
    If you used scalar, they might not know exactly what it did...

    A Perl programmer who doesn't understand scalar context has no business programming alone.

    With that said, this is one of the few places where using the explicit scalar operator makes sense.

Re^3: Wrong result for "length keys %$hashref"?
by ikegami (Pope) on Mar 14, 2011 at 22:53 UTC

    You're assuming code exists in isolation. I can't think of any code that needs the number of elements in a hash that would be unclear no matter how you spelled

    0+keys(%h)

    But that doesn't mean they're all equal.

    • The 0+ points out that something special is happening, whereas scalar() just looks like a function call. A function call that doesn't exist. There's not even an op for it.

    • The 0+ points out that a number is going to be returned, confirming what one might think keys is doing.

    If you used scalar, they might not know exactly what it did but they'd have a direct pointer to documentation that would explain exactly what the intent of the code was.

    I think they'd be better off reading the docs for keys, not scalar. The former lists the two possible return values for keys. The understanding of context will follow.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (10)
As of 2014-12-19 09:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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





    Results (75 votes), past polls