Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

The “literal/constant first” form can be used defensively to avoid mistakenly using the wrong operator for languages that have assignment operators that are similar to equality-test operators. For example, this will cause a syntax error:

    if (true = some_boolean)
While this will silently result in an assignment of true to the some_boolean variable, not intended the equality test

    if (some_boolean = true)
Editor hints and linting can help catch this as well, but those aren’t always available (or weren’t available in the past). I think I first saw this in Code Complete 2.

Here’s some more explanation

https://softwareengineering.stackexchange.com/questions/7408...

Looks like the pattern is sometimes referred to as a Yoda expression.



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: