Making WordPress.org

Opened 6 months ago

Closed 6 months ago

#5429 closed enhancement (reported-upstream)

PHP Coding Standards - Yoda Conditionals - Absolute Phrasing

Reported by: sabernhardt Owned by:
Milestone: Priority: normal
Component: Developer Hub Keywords:


originally reported by @theistand on https://core.trac.wordpress.org/ticket/50276

In the PHP coding standards page under the Yoda Conditionals it is stated
"When doing logical comparisons involving variables, always put the variable on the right side and put constants, literals, or function calls on the left side."
The concern being the use of the word "always" because at the bottom it is then stated "Yoda conditions for <, >, <=, or >= are significantly more difficult to read and are best avoided."

The issue with this is that <, >, <=, >= are in fact conditional operators and should apply to your standards with logical comparison. The emphasis "are significantly more difficult to read" implies that the decisions are on readability, however the entire purpose for the Yoda Conditionals has nothing to do with readability as it is the most illogical and unreadable implementation. The use of Yoda Conditionals should only be for ease in debugging.
Also quoting "Computer Science Terms" l-values stands for left-values and r-values stands for right-values. This is a logical approach and is a industry standard across the board which is the exact opposite as your call for use of Yoda Conditionals.

No argument on using them only pointing out some areas that can cause substantial confusion to newbies coming into the wordpress world.

PHP Coding Standards DevHub page

Change History (3)

#1 @sabernhardt
6 months ago

In addition to removing "always" from the first paragraph (both times), here are two suggestions for revising the section. If the information about the equal-sign operators is moved to the top, it would be unnecessary in the last paragraph.

First sentence:

When doing logical comparisons involving variables and either equality or identity operators (==, !=, ===, and !==), put the variable on the right side and put constants, literals, or function calls on the left side.

Last sentence/paragraph:

Avoid Yoda conditions for <, >, <= or >= because they are significantly more difficult to read.

This ticket was mentioned in Slack in #core-coding-standards by dd32. View the logs.

6 months ago

#3 @dd32
6 months ago

  • Resolution set to reported-upstream
  • Status changed from new to closed

Hi @sabernhardt & @theistand,

Meta isn't the right team to deal with this ticket, core is the better place IMHO.
However, I raised it directly with the team and it's been suggested https://github.com/WordPress/wpcs-docs is probably a better place.

You can also reach out to the team in #core-coding-standards on the Making WordPress slack (see https://chat.wordpress.org/)

I'm also going to copy @jrf's response here for reference for those who don't have a Slack account:

Thanks dd32 for the ping. This should actually be handled in this repo: https://github.com/WordPress/wpcs-docs

As for the arguments the op makes: I totally agree that Yoda conditions are redundant and the current CS check for them is laughable as it only checks for comparison operators, NOT assignment operators, which is what Yoda is supposed to be about (preventing assignments in condition).

I realize there is a lot of division in the opinions about Yoda, so it is on my list of future posts to put up on Make to open it up for a broader discussion.

Note: See TracTickets for help on using tickets.