Skip to content
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

Add section about Yoda conditions #241

Open
camsaul opened this issue Jul 14, 2022 · 1 comment
Open

Add section about Yoda conditions #241

camsaul opened this issue Jul 14, 2022 · 1 comment

Comments

@camsaul
Copy link

camsaul commented Jul 14, 2022

Consider two ways to check what sort of bird we have on hand:

  1. (= bird-type :toucan) -- one reads this code as "bird type equals toucan"
  2. (= :toucan bird-type) -- you'd read this as "toucan equals bird type"

Now while they both do exactly the same thing, the former is the "normal" way to write things and the latter is a Yoda condition

@seancorfield
Copy link
Collaborator

seancorfield commented Jul 14, 2022

I don't find anything wrong with 2. and it has specific benefits in many languages because having the constant first avoids this error:

if ( x = 2 ) { ... } // assignment

if ( 2 = x ) { ... } // compiler will catch this as an error!

if ( 2 == x ) { ... } // what was actually intended

So you'll likely find a lot of people who've had that drummed into them for years and it'll be natural for them to write:

(if (= 2 x) ...)

(and I do this myself, very deliberately)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants