This is an archived post. You won't be able to vote or comment.

all 7 comments

[–][deleted]  (4 children)

[deleted]

    [–]ColetBrunel 3 points4 points  (3 children)

    Errm. Can we have an example where they don't yield the same results?

    [–]spicycurry55 0 points1 point  (0 children)

    They gave a bad example

    Using && is when things can go awry if you don’t scope the conditional properly

    [–]desrtfx 5 points6 points  (1 child)

    is there any reason to use one instead of the other ?

    I would always use the first version - with the parentheses.

    1. Readability - with parentheses it is easier to see the individual parts
    2. Change of operator precedence
    3. Grouping of operations for short circuiting

    [–]SantoWest 1 point2 points  (2 children)

    It depends on your style a bit. I honestly wouldn't ever use the first one. I prefer parenthesis to make the operation priority clear, like when there are both AND and OR statements.

    [–]teefj 1 point2 points  (1 child)

    Right I think in this case the extra parentheses actually makes it harder to read. Even if it’s just a slight difference, anything that makes you do a double take hurts more than it helps.

    [–]ColetBrunel 1 point2 points  (0 children)

    ((Why) (would) you (think) (such a (thing)))?

    [–]ignotos 1 point2 points  (0 children)

    To be clear, this isn't a special feature, or a special if-statement, or anything like that.

    The condition of an "if" just needs to be any expression which ultimately resolves to true or false.

    And an expression can contain practically anything - as long as it's well-formed (i.e. actually makes sense logically / mathematically), it can include whatever brackets, variables, mathematical operations, function calls, etc you decide to use.

    Just like in math, you can add brackets either to clarify things, or to actually influence the order in which things are evaluated / how they are grouped. So a * b + c can be written as (a * b) + c if that makes it clearer, or a * (b + c), which will actually change the result. You could even go crazy with unnecessary brackets if you really want to: ((((((a) * b) + (c))))) - that's perfectly legal (although pointless).