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

(tye)Re: Logical expression style

by tye (Sage)
on Jul 12, 2002 at 17:04 UTC ( #181345=note: print w/replies, xml ) Need Help??

in reply to Logical expression style

I don't like any of them (I've been trying (off and on) and failing to find a good standard for this type of thing for a long time). But I really don't like hiding the operators on the end. And I don't like adding extra parens just to "show precendence" (my eye can't match nested parens nearly as well as it can notice variations in spacing).

So now I try to line things up by precendence with the operator in front. I always put two spaces around logical expressions so I can put single spaces within them.

if( defined $settings && ! $setttings->{hidecode} and $codtype{ $node->{nodetype} } or $node->{code} || containscode( $node->{doctext} ) # ^1 ^2 ^3 ) {
So the 'arguments' to 'and' line up above "^1", the arguments to 'or' above "^2" and the arguments to '||' above "^3". I think I've finally found a standard for this that I like. (:

        - tye (but my friends call me "Tye")

Replies are listed 'Best First'.
Re: (tye)Re: Logical expression style
by VSarkiss (Monsignor) on Jul 12, 2002 at 17:58 UTC

    I think I do about the same thing, but isn't this slightly misleading? Since the or binds least tightly, if I were to show precedence, I'd do:

    if ( defined $settings && ! $settings->{hidecode} and $codtype{ $node->{nodetype} } or $node->{code} || containscode( $node->{doctext} ) ) { # ...
    In other words, I'd line up the horns of the or at the outermost level, and then the and, etc.

    Or did I miss the point you were trying to make? (No pun intended ;-)

    This is similar to how I indent SQL where clauses:

    where EXISTS (select 1 from users where users.user_id = acct.user_id or (acct_id is null and user_id = 0)

      Yes, it was a contrived example and I did it wrong. ):

              - tye (but my friends call me "Tye")
Re: (tye)Re: Logical expression style
by Sifmole (Chaplain) on Jul 12, 2002 at 17:11 UTC

    I think I would go cross-eyed trying to read that! Just looking at it gives me that "all over the place" feeling.

    I tend to use the operator at the end method.

Re: (tye)Re: Logical expression style
by Anonymous Monk on Jul 12, 2002 at 17:52 UTC
    Omigod! My eyes! I looked directly at the code!
    if ( ( defined $settings && ! $settings->{hidecode} ) && ( $codetype{ $node->{nodetype} } ) || ( $node->{code} || containscode( $node->{doctext} ) ) ) {

    A few extra parens makes it much more readable in my opinion.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2022-08-08 19:17 GMT
Find Nodes?
    Voting Booth?

    No recent polls found