Skip to content

[New Rule] Use Yoda Conditions #137

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
matthiashamacher opened this issue Aug 30, 2019 · 2 comments
Closed

[New Rule] Use Yoda Conditions #137

matthiashamacher opened this issue Aug 30, 2019 · 2 comments
Labels
proposal New rule proposal

Comments

@matthiashamacher
Copy link

Rule

Use Yoda Conditions to avoid assignments inside a condition statement.

Reason

With Yoda Conditions it is not possible to assign a variable inside a condition statement, which improves the code quality because it prevents possible assignment errors.

Implementation

Some other Frameworks, such as Symfony, are using Yoda Conditions and have checks for them.

@matthiashamacher matthiashamacher added the proposal New rule proposal label Aug 30, 2019
@matthiashamacher matthiashamacher changed the title Use Yoda Conditions [New Rule] Use Yoda Conditions Aug 30, 2019
@Vinai
Copy link

Vinai commented Aug 30, 2019

Even though I personally favor yoda style comparison, I think a rule for this would be too restrictive.
The reasons for this are the following.
Many developers find yoda style equality checks harder to reason about because in their mind the comparison is done the other way around.
In my experience, such accidental assignments where extremely rare and never got past a code review. Nowadays they would be caught by tests, too.
Making coding guidelines too restrictive can discourage contribution. I think allowing for personal coding style preferences in the small is more encouraging.

@lenaorobei
Copy link
Contributor

I agree with @Vinai here. It is more about personal preferences not about the code style. I do understand you intention, but we don't want to enforce developers to use yoda conditions and limit somehow their personal preferences.

Sorry, going to close this PR.

magento-devops-reposync-svc pushed a commit that referenced this issue Dec 16, 2021
* Fix DirectThrowSniff false positive

* fix custom exception gives false positive with DirectThrowSniff

* fix coding standard

* Apply suggestions from code review

Co-authored-by: Sergio Vera <[email protected]>

* Fix error

Co-authored-by: MeenakshiSundaram <[email protected]>
Co-authored-by: Sergio Vera <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
proposal New rule proposal
Projects
None yet
Development

No branches or pull requests

3 participants