Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Re: Operator Precedence

by tomazos (Deacon)
on Jul 22, 2001 at 18:18 UTC ( [id://98819]=note: print w/replies, xml ) Need Help??


in reply to Re: Operator Precedence
in thread Operator Precedence

The solution to *interesting* precedence bugs is not to throw in parenthesis for no reason. A prevention is always better than a cure. The examples you have given are a demonstration of my point. Why do you think the ternary operator has higher precedence than assignment?

Because:

$is_good ? $white : $black  += 1;

Yes, to someone who doesn't know the precedence table this is confusing. To someone that doesn't know any Perl I'm sure that the Howdy World program is confusing too. The solution is not to document every line in painful detail as to what exactly is going on so that people that don't know Perl can read your program.

The solution is that programmers learn the precedence table and soon what seemed like a cryptic and convoluted expression will magically transform into an obvious sequence of calculations.

Replies are listed 'Best First'.
Re3: Operator Precedence
by pmas (Hermit) on Jul 22, 2001 at 20:47 UTC
    I have to slightly disagree with you, tomazos. I feel slight tendention toward elitism, like in: "if you cannot learn to perfection precedence table, you are not good enough to understand my code, so damn you...". Sure I exagerated a lot to show my point, but just think about it.
    My situation is very different from yours. In our team, about half team memebers have biology or medicine background, not computer sciences. Some (not all!) are struggling with programming, but for every one time I wish they know some rare trick, there are three times I am amazed what they know about problem domain, how they can understand what customer is saying, what I might understand wrong without their help. Parts of old system are in ASP, simple "database proof of concept" is done in MS Access, some people just know PHP and prefer it (not me!), but htey may ask me to look at their code (to solve database isuues). So this is not only issue of "learning perl precedence by heart", real life is not this simple.

    So my point is: when thinking about parantheses, err on side "more () for better understanding", and avoid "for expert of my level this is obvious". Generations maintaining your code will thank you for that.

    pmas
    To make errors is human. But to make million errors per second, you need a computer.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-03-29 06:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found