Yoda Conditional Statements

Here’s a little gem on conditional statements that I saw for the first time in the WordPress Coding Standards a few months ago. When writing out a conditional logic, it is recommended to place the variable on the right side of the comparison operator and the constants or literals on the left. Appropriately named “Yoda Conditions”, as the statement then reads backwards to how we would typically speak or think. I’ve never really thought about this before but it really is a handy little tip.

Take the following example (from the handbook):

[code language=”php”]
if ( true == $the_force ) {
$victorious = you_will( $be );

Despite that it reads backwards, this makes sense programatically. If you were to accidentally omit a part of the comparison operator (an equals sign, for example) when writing out your statement, your code would fail because you can’t assign to a constant. Writing your conditional the other way around (non-Yoda) would evaluate to true via an accidental assignment operator. This would result in perfectly valid code and a bug that is much more difficult to track down.

Note that this only applies to ==, !=, ===, and !== comparisons. As stated in the handbook, “Yoda conditions for <, >, <= or >= are significantly more difficult to read and are best avoided.”

Sass Basics (Slides)

Last week I had the pleasure of doing a lightning talk at the San Diego JavaScript Meetup. It was my first public presentation. Even thought it was short and informal, I was very excited for the opportunity and had a lot of fun.

SDJS is one of my favorite meetups here in San Diego. The organizers always do a great job with everything and the talks are always interesting. This was the first time they did the lightning talks where about 5 people presented on short, 5 minute topics. I loved this idea because I found it the perfect opportunity to give a talk and not feel a ton of pressure. There was a nice turnout that evening and everyone did a great job with their presentations.

I did my talk on Sass (my favorite CSS preprocessor). Since this was only a 5 minute talk, I just covered the very basics of getting started. Here is a link to my slides.

JavaScript and Media Queries

When developing a responsively designed web site, it is likely that you will come across a situation where you will want to selectively execute a JavaScript callback based on a media query comparison. For example, you may want to alter your menu layout for small-screen devices. Or you may wish to load in specific content only on large-screen devices. Luckily, these capabilities are currently available. In this article I will cover a couple methods and show you that this can be done without much trouble at all!

Continue reading “JavaScript and Media Queries”