My Coding Style Today I Change

17 April, 2024

I admit, I've always been more of a Trekkie than a Star Wars fan. (did I just hear some light sabers firing up?) I mean, there's not even a collective term for Star Wars fans. Calling them "Star Wars fans" seems forced (no pun intended). That said, Yoda has given good advice, on occasion, and I have to think that both he and Spock would approve of the notation named for him.

For those who never took notice or have forgotten, Yoda's sentence structure was more akin to that of Japanese, with the elements somewhat reversed. For example:

You must go to Tatooine

would be expressed as

Go to Tatooine, you must.

With that foundation, let's move on to some simple programming logic, like testing the return of a function call. As far back as I can remember, if I need to explicitly test the return, I've been writing it like this:

if ( my_cool_function() == FALSE )

but as of today, I'm switching to writing it as what is referred to as "Yoda notation" or a "Yoda condition":

if ( FALSE == my_cool_function() )

So, it works exactly the same, but looks weird. Why do it? Let's say I do what all of us do from time to time, though we think of it as a noob error:

if ( my_cool_function() = FALSE)

and later spend a couple hours debugging until I get to this line and <doh> palm my forehead. However, using Yoda notation:

if ( FALSE = my_cool_function() )

will throw a syntax error, because it's attempting to assign a value to a constant.

I'd say give it a try, but of course, there is no try, there is only do, or do not.

Login or Register to Comment!