Skip to content
This repository was archived by the owner on Jan 1, 2019. It is now read-only.

Add Yoda Conditions style - #6

Closed
mrDlef wants to merge 2 commits into
masterfrom
daidais-patch-1
Closed

Add Yoda Conditions style#6
mrDlef wants to merge 2 commits into
masterfrom
daidais-patch-1

Conversation

@mrDlef

@mrDlef mrDlef commented Feb 12, 2018

Copy link
Copy Markdown
Member

Yoda Conditions

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. If neither side is a variable, the order is not important. (In computer science terms, in comparisons always try to put l-values on the right and r-values on the left.)

if (true == $theForce) {
    $victorious = youWill($be);
}

In the above example, if you omit an equals sign (admit it, it happens even to the most seasoned of us), you’ll get a parse error, because you can’t assign to a constant like true. If the statement were the other way around ($theTorce = true), the assignment would be perfectly valid, returning 1, causing the if statement to evaluate to true, and you could be chasing that bug for a while.

A little bizarre, it is, to read. Get used to it, you will.

This applies to ==, !=, ===, and !==. Yoda conditions for <, >, <= or >= are significantly more difficult to read and are best avoided.

@geoffroy-aubry

Copy link
Copy Markdown
Contributor
  • Why do you say that this apply to === and !== ?
  • From my point of view the lack of readability outweighs the benefits described above

@geoffroy-aubry

Copy link
Copy Markdown
Contributor

Do not use Yoda:

  • The Programatic Programmer: From Journeyman to Master
  • Clean Code: A Handbook of Agile Software Craftsmanship
  • Design Patterns: Elements of Reusable Object-Oriented Software
  • Code Complete: A Practical Handbook of Software Construction, Second Edition

@geoffroy-aubry

Copy link
Copy Markdown
Contributor

Do not use Yoda:

  • jQuery
  • Zend
  • Laravel
  • React
  • Underscore
  • AngularJS
  • Backbone.js
  • React
  • Closure
  • Ruby on Rails (RoR)
  • CakePHP

@geoffroy-aubry

Copy link
Copy Markdown
Contributor

The Clean Coder:
« I dislike any statement in code that causes the reader to do a double-take. Code that protects the author at the expense of the reader is flawed code. »

@geoffroy-aubry

Copy link
Copy Markdown
Contributor

Even if I vote against, I thank you for your proposal :)

@nunomaduro

Copy link
Copy Markdown
Contributor

Great proposal! This thumbs vote format is really cool. For this proposal I vote against tho.

@wodzy

wodzy commented Feb 12, 2018

Copy link
Copy Markdown

I think this makes the code harder to read.
I vote against too.

@mrDlef

mrDlef commented Feb 13, 2018

Copy link
Copy Markdown
Member Author

Here are some good resources about that:

As we don't have enough unit tests and linters for our legacy code I thought it would be a good way to be sure of the behavior of a condition.
Cool to discuss on that anyway.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Development

Successfully merging this pull request may close these issues.

4 participants