Code Smell 195 – Yoda Conditions

Best is to put the expected value last, if conditions you want to write.

TL;DR: In a natural way, write your conditions.

Problems

Readability
The least surprise principle violation

Solutions

Write your conditions with the …


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Maxi Contieri ⭐⭐⭐

Best is to put the expected value last, if conditions you want to write.

TL;DR: In a natural way, write your conditions.

Problems

  • Readability

  • The least surprise principle violation

Solutions

  1. Write your conditions with the expected value as the second.

  2. Name the variables accordingly.

Context

Most programmers write the variable or condition first and the test value second.

In fact, this is the correct order for assertions.

In some languages, this style is used to avoid accidental assignment instead of equality comparison, which can result in a logic error in the code.

Sample Code

Wrong

if (42 == answerToLifeMeaning) {
  // 
}

Right

if (answerToLifeMeaning == 42) {
  // might be mistaken with answerToLifeMeaning = 42
}

Detection

[X] Semi-Automatic

We can check for constant values on the first side of the comparison.

Tags

  • Readability

Conclusion

Reliable, direct, and clear be when conditions your writing.

Relations

Disclaimer

Code Smells are just my opinion.

Credits

Photo by Lia on Unsplash

Any man can make mistakes, but only an idiot persists in his error.

Marcus Cicero

This article is part of the CodeSmell Series.


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Maxi Contieri ⭐⭐⭐


Print Share Comment Cite Upload Translate Updates
APA

Maxi Contieri ⭐⭐⭐ | Sciencx (2023-02-07T23:23:53+00:00) Code Smell 195 – Yoda Conditions. Retrieved from https://www.scien.cx/2023/02/07/code-smell-195-yoda-conditions/

MLA
" » Code Smell 195 – Yoda Conditions." Maxi Contieri ⭐⭐⭐ | Sciencx - Tuesday February 7, 2023, https://www.scien.cx/2023/02/07/code-smell-195-yoda-conditions/
HARVARD
Maxi Contieri ⭐⭐⭐ | Sciencx Tuesday February 7, 2023 » Code Smell 195 – Yoda Conditions., viewed ,<https://www.scien.cx/2023/02/07/code-smell-195-yoda-conditions/>
VANCOUVER
Maxi Contieri ⭐⭐⭐ | Sciencx - » Code Smell 195 – Yoda Conditions. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/02/07/code-smell-195-yoda-conditions/
CHICAGO
" » Code Smell 195 – Yoda Conditions." Maxi Contieri ⭐⭐⭐ | Sciencx - Accessed . https://www.scien.cx/2023/02/07/code-smell-195-yoda-conditions/
IEEE
" » Code Smell 195 – Yoda Conditions." Maxi Contieri ⭐⭐⭐ | Sciencx [Online]. Available: https://www.scien.cx/2023/02/07/code-smell-195-yoda-conditions/. [Accessed: ]
rf:citation
» Code Smell 195 – Yoda Conditions | Maxi Contieri ⭐⭐⭐ | Sciencx | https://www.scien.cx/2023/02/07/code-smell-195-yoda-conditions/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.