When we’re not coming up with new technologies at Rubicon, we’re usually figuring out ways to optimize our code to make it faster and more stable. And when we’re not optimizing code, we’re optimizing ourselves as programmers—finding ways to be better communicators, collaborators, and teachers to one another. One of the methods we use to ensure code readability among all team members is syntactic sugar.
Syntactic sugar doesn’t make code run any faster. Rather, it makes us as programmers faster. It’s a philosophy of creating self-documenting, human-readable code that:
- simplifies usage
- lends itself to easy testing
- makes it easy to tell what a piece of code is supposed to do
- makes it easy to find code
- gives code context and meaning
- has a structure that mirrors the results we want to achieve
Consider this example. We’re all familiar with these different ways of incrementing a variable:
$i = $i + 1;
$i += 1;
$i++;
Sure, all three lines of code do the same thing, but which is easier to understand at first glance? Try reading them aloud, from left to right. The first example reads like, “Make $i become equal to itself plus one.” Not very intuitive. The second example is similarly obscure—I’m not even sure how that would translate to English.
But read the last example, and you’ll find it comes closest to expressing the concept of incrementing in a way that’s more familiar to how people naturally think: “Take $i and add one to it.” Notice also how its focus is on the desired end result: incrementing $i.
Here’s another example. Let’s say we’d like a function that could search for movie titles based on different criteria. We could write something like:
getMovies($filter_by, $sort_by, $sort_order, $start_date, $end_date, $num_results)
And we’d use it like so, to search for the 10 most popular movies from January to December 2010:
getMovies('popularity', 'alpha', 'desc', '1/1/2010', '12/31/2010, 10);
It does the trick—but is it readable? Again, read it aloud and you’ll wind up with “Get movies filtered by popularity, sorted alphabetically and by description, from January 1, 2010 to December 31, 2010, and get 10 results.” Sounds like instructions you’d hear from a robot.
This is where your intuition as a real live human programmer comes into play. When searching for movies, do you search for all popular movies, sorted backwards by alpha, from 100 years ago? Of course not. Just like you wouldn’t search for movies created in the last ten seconds, or for the top 2 movies of the last 66 years. Our getMovie function, while well-written, doesn’t indicate its own practical purpose—all cases, no matter how edge-y, are treated equally.
Back in the human world, we already know how most people search for movies. They look for what’s popular, or what’s new. Or maybe what’s classic, or what titles just became available to rent. With these common tasks in mind, we can write syntactic sugar functions that perform the same thing as our original getMovies function, but in a much more intelligible way. That way, the aforementioned function…
getMovies('popularity', 'alpha', 'desc', '1/1/2010', '12/31/2010, 10);
…becomes this:
getPopularMovies();
We’re not changing the functionality of getMovies() at all, and getPopularMovies() isn’t technically necessary since we could do the same thing with getMovies alone. But practically speaking it makes code much easier to scan, decode, and debug. Eventually, you can imagine writing other convenient bits of syntactic sugar, like…
getTopRentedMovies();
getRecentMovies();
getClassicMovies();
getWeeklyTop40();
…the function of each being super clear right from the get-go, making it easier for others in your team to review code and take their discussions to a higher problem-solving level—not to mention mitigating potential bus factors. Here’s to writing sweeter code!