Details
-
Improvement
-
Status: Closed
-
Minor
-
Resolution: Fixed
-
4.1
-
None
-
None
-
MOODLE_401_STABLE
Description
The following binary operators must not be used:
- and
- or
The following standard binary operators may be used instead:
- &&
- ||
To put it bluntly, these operators are nasty, and have counter-intuitive behaviours.
Whilst they have a use-case, their use leads to unclear code and bugs, and they do not behave as 90% of people would expect without RTFMing.
To demonstrate:
$example = false or true or true or true; // $example = false
|
$example = (false or true or true or true); // $example = true
|
if (false or true or true or true) {
|
// Truthy result
|
}
|
This has the potential to cause issues where developers are not 100% familiar with the operators, for example during a refactor.
Given the following code:
if (some_long_named_function() or some_other_long_named_function()) {
|
// Do something.
|
}
|
If it is later refactored, for example to add a third parameter, and the author continues with the use of the or operator, it could easily be modifed to:
$dosomething = some_long_named_function() or some_other_long_named_function() or something_else();
|
|
if ($dosomething) {
|
// Do something.
|
}
|
However the behaviour here is different and, if the first test returns false, then the conditional will no longer be entered because $dosomething takes the first value, which is now false.
I would strongly suggest that we ban the use of both the and, and or operators to prevent mistakes of this kind.
Voting options
The following options are suggested for voting:
Option A
Ban the use of the and and or keywords in favour of && and || and apply the following change to the coding style:
-
- A new Operators section
### Operators
The following binary operators **must not** be used:
- `and`
- `or`
The following standard binary operators _may_ be used instead:
- `&&`
- `||`
-
- A reference to this should be added to the Control statements section:
:::important
Please note that the the `and`, and `or` operators *must not* be used. The `&&` and `||` operators should be used in their place.
See [Operators](#operators) for more information.
:::
Option B
Do not ban them.
Attachments
Issue Links
- Discovered while testing
-
MDL-74287 Early (web) installation steps lead to table does not exist errors
-
- Closed
-