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
Closed
Conversation
Contributor
|
Contributor
|
Do not use Yoda:
|
Contributor
|
Do not use Yoda:
|
Contributor
|
The Clean Coder: |
Contributor
|
Even if I vote against, I thank you for your proposal :) |
Contributor
|
Great proposal! This thumbs vote format is really cool. For this proposal I vote against tho. |
|
I think this makes the code harder to read. |
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.)
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.