Skip to content

Conversation

igorw
Copy link
Contributor

@igorw igorw commented May 30, 2013

The coding guidelines suggest using and and or over &&
and ||. Frankly I don't think this is a good idea. The
precedence rules for and and or are so weird, they will
cause lots of confusion and edge cases.

To illustrate this, look at the following example (from logical
operators
):

$x = false or true;

What do you expect the value of $x to be? It's false, because
or has lower precedence than =. So while you expected the
expression to be:

$x = (false or true);

It is in fact:

($x = false) or true;

Stop using and/or as logical operators. They were designed
for flow control.

The coding guidelines suggest using `and` and `or` over `&&`
and `||`. Frankly I don't think this is a good idea. The
precedence rules for `and` and `or` are so weird, they will
cause lots of confusion and edge cases.

To illustrate this, look at the following example ([from logical
operators][0]):

    $x = false or true;

What do you expect the value of `$x` to be? It's `false`, because
`or` has lower precedence than `=`. So while you expected the
expression to be:

    $x = (false or true);

It is in fact:

    ($x = false) or true;

Stop using `and`/`or` as logical operators. They were designed
for flow control.

[0]: http://php.net/manual/en/language.operators.logical.php
taylorotwell added a commit that referenced this pull request May 30, 2013
Stop recommending `and`/`or` over `&&`/`||`
@taylorotwell taylorotwell merged commit db8beca into laravel:master May 30, 2013
@CodeAngry
Copy link

I always use 'and' / 'or' as they improve readability. But I wrap in parenthesis things to make sure they play along nicely just like && / ||. This is a source for really freak bugs to someone who does not know they have a VERY different order or precedence compared to their non literal variants.

@jeremeamia
Copy link

Using and and or is pretty common practice among Code Ignitor, Kohana, and (I think) FuelPHP developers. As someone who did development with Kohana for a couple of years, I know that this practice can definitely cause issues if you don't include parentheses as appropriate. It is an easy mistake for junior devs to make and sometimes hard to debug when the conditions are wrong. I agree with @igorw that it should not be the recommended standard to use them. Glad to see this merged.

@taylorotwell
Copy link
Member

Yeah I agree it's an easy mistake. It's bitten me once, but thankfully a unit test caught it. In general, I prefer the "look" of just and and or. But, because of how easy to is to write not so obvious bugs, I don't think we can insist on it anymore.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants