Problem/Motivation
I'd like to propose that we suggest a preference for && and || for logical operators instead of and and or for consistency and clarity. If precedence is needed for assignment then it would be clearer to use parenthesis.
See http://php.net/manual/en/language.operators.logical.php
Benefits
If we adopted this change, the Drupal Project would benefit from consistency and clarity in code.
Three supporters required
- https://www.drupal.org/u/borisson_ (2017-09-17 they added support)
- https://www.drupal.org/u/{userid} (yyyy-mm-dd they added support)
- https://www.drupal.org/u/{userid} (yyyy-mm-dd they added support)
Proposed changes
Provide all proposed changes to the Drupal Coding standards. Give a link to each section that will be changed, and show the current text and proposed text as in the following layout:
1.https://www.drupal.org/docs/develop/standards/php/php-coding-standards#o...
Current text
There isn't any current text.
Add current text in blockquotes
Proposed text
Logical operators
Use
||and&&.
2. Repeat the above for each page or sub-page that needs to be changed.
Remaining tasks
Create this issue in the Coding Standards queue, using the defined template- Add supporters
- Create a Change Record
- Review by the Coding Standards Committee
- Coding Standards Committee takes action as required
- Discussed by the Core Committer Committee, if it impacts Drupal Core
- Final review by Coding Standards Committee
- Documentation updates
- Edit all pages
- Publish change record
- Remove 'Needs documentation edits' tag
- If applicable, create follow-up issues for PHPCS rules/sniffs changes
For a full explanation of these steps see the Coding Standards project page
Comments
Comment #2
drunken monkeyThanks for the suggestion! I completely support this. Seems weird we don't already specify this, really.
The only real use for
orandand, as you say, is checking assignments, and I personally find that pretty bad style which we should discourage (in my opinion).However, do you by any chance have any data on how often those are actually used in Core right now?
Comment #3
joelpittetIn D8 I couldn't find any cases by scanning with this regex.
(or|and) [^\w@<%\.#/\* {]And didn't see anything in D7 core but some sparse ones in ctools, features, eck and a few others.
Comment #4
tizzo commentedPersonally, I support this and give it a +1.
The TWG is soliciting input in our next final discussion announcement. Feedback will be reviewed in our meeting on 9/19.
Comment #5
giorgio79 commentedUsing natural language is much more readable and understandable, so -1. Lets not go back to assembly days please.
Comment #6
pfrenssenI am personally in favor of this.
andandorhave the lowest operator precedence in PHP which makes them difficult to use in typical code. For example these lines of code are NOT equivalent:The operator precedence is very carefully designed in PHP to make it as intuitive as possible and to make parentheses unnecessary in the most common use cases.
We already have coding standards in place to disallow coding standards that require the use of parentheses to make them work, for example we disallow inline variable assignments.
Also a very big point in favor is that core is not using
andandorat all as mentioned in comment #3. If we are already avoiding its use it is a de-facto standard and it makes sense to formalize this.In case people are wondering why these operators actually exist in PHP, they are intended to be used when doing an assignment and a comparison in a single step, so that parentheses can be avoided in this case. For example these two lines are equivalent:
So this is actually useful in this case, but ONLY in this case, and it can be very confusing to people that are not aware of this difference and they use them _without_ doing an inline assignment.
In Drupal we already disallow inline assigments, so there is absolutely no use at all for
andandor.Comment #7
JvE commentedSupported.
Comment #8
pslcbs commented+1 for || and && as logical operators.
Comment #9
james.williamsThe vast majority of core code already uses && and ||. So this isn't about choosing to use 'and' and 'or' instead. Instead, as far as I see it, it's about maintainability & consistency. So In my opinion, its better in that respect to just use one or the other, and as the majority's code already uses && and ||, I support this suggestion.
Comment #10
perignon commented+1 I never think to type 'and' or 'or'. I have been using && and || for too long. Java (day job) uses the same operators as PHP.
Comment #11
giorgio79 commentedStill -1. Should be raised upstream with php to move towards natural language instead of random doodles. :D Lots of confusion in php land https://stackoverflow.com/questions/2803321/and-vs-as-operator
Comment #12
loopduplicate commented+1 from me for sure. I have never needed to use "or" or "and".
Side note... isn't there some better way to actually do a +1?
Comment #13
pivica commented+1 from me.
Comment #14
andrej galuf commented+1 from me.
Comment #15
jgloverattronedotcom commented+1 for || && instead of "or" and "and"
Comment #16
progga commented-1
I find
if ($cheap and $nasty)more readable thanif ($cheap && $nasty)Comment #17
benjifisherFrom Comment #6 above:
I was not aware of that rule, and I cannot find it on https://www.drupal.org/docs/develop/standards/coding-standards. Can you give a reference?
Personally, I like to use the lower status of
andto avoid parentheses, as in the last code snippet from Comment #6. If that snippet already violates Drupal standards because of the inline assignment, then I will add my +1 in favor of disallowingandandor.@progga (Comment #16): Which matters more, personal preference or consistency? We already have a codebase full of
&&and||... I see I am repeating Comment #9.Comment #18
benjifisherAccording to the discussion in #2372543: [policy, no patch] Explicitly disallow yoda conditions, inline assignments are disallowed in JS code, and #2907332: Disallow assignments in conditions was just added to discuss whether to adopt the same standard in PHP code.
That sort of answers my own question.
Comment #19
gogowitsch commented+1. I am in favor of excluding 'and' and 'or' from Drupal code.
Comment #20
steinmb commented+1 Let us formalize this.
Comment #21
tizzo commentedThis was discussed on today's Coding Standards Committee meeting. Most of the support here appears to be in formally disallowing the english word forms rather than preferring the other form. As such, we have decided to retitle the issue and leave it open for another two weeks of discussion but barring major dissent we are like to ratify this in two weeks.
Comment #22
borisson_+1 to this, I learned about the operator precedence from this issue - I didn't even know that. Readability is important but I feel that this so pushed into our brains what these mean that this doesn't hurt readability.
Comment #23
tizzo commentedIn todays TWG meeting we felt that this issue has sufficient support. Marking postponed and tagging approved pending a coder rule. As soon as we get a patch in the coder queue to enforce this rule we will formally approve, announce, and update documentation.
Comment #24
quietone commentedI can't find an issue in Coder for this. Is there one?
Comment #25
jonathan1055 commentedThis has been revived in the Slack thread https://drupal.slack.com/archives/CJ19MV96J/p1738103687296019
From #23
I have updated the issue summary to include the new template, but not filled in any of the details. Anyone else on this thread is welcome to take that on, I'm just giving the template.
Comment #26
borisson_Added as supporting.
Comment #27
quietone commentedSince this was approved 7 years, I have gone ahead and made a coder issue, #3503286: Add a sniff to prohibit "or" or "and" as logical operators
Added proposed text for discussion.