Jon Aquino's Mental Garden

Engineering beautiful software jon aquino labs | personal blog

Sunday, May 18, 2014

Vine video as the sonnet of this generation

I have been wondering what is the attraction of Vine videos - those six-second looping videos.

Then today I was reading about the sonnet (16-line poems), and how popular they were in England in the 16th century. Maybe Vine is the sonnet of this generation?

Consider the following description of the sonnet - note that it has some similarity to a Vine video with its 6-second constraint:

Very often these love poems were addressed to a small circle of congenial spirits—fellow poets, wits, ladies of fashion—whom the poet wished to amuse. The way to impress this company was to exhibit special ingenuity in devising new variations on the old theme or in polishing one's work to an impressively high gloss. The reproach that such poetry is “insincere” would have astonished any Elizabethan. The rules were there with all the weighty authority of tradition behind them, and they were there to be followed. To depart from them would be as incomprehensible as if a chess player were to propose a change of rules to his opponent in order that he “might express himself better”.

—Adventures in English Literature

Tuesday, May 06, 2014

Programming by Equivalence

I would like to introduce a programming technique called “Programming by Equivalence”. You can't use it all the time, but when you can use it, it’s great.

Problem: You have an existing feature X, and you want to implement a similar feature Y.

Solution: For every line of code for X, create a line of code for Y.

The beauty of this technique is:

  1. You don't need to think much - just find each line of code for X and make an equivalent line of code for Y.
  2. You know when you are done: when every line of code for X has an equivalent line of code for Y.

Here is a script that can help you to do 1 and 2 above. Just tweak the variables in the script to match your own code. Suppose you have implemented Google +1 buttons in your codebase, and now you want to add LinkedIn buttons. Run the script, and you will get output like:

lib/XG_ConfigHelper.php::: return B()->getMainConfig('googlePlusOneEnabled') === '1';
lib/components/bundle/controllers/EntryController.php::: if (R('XG_ConfigHelper')->isGooglePlusOneEnabled()) {
lib/components/bundle/lib/EntryListService.php::: if (R('XG_ConfigHelper')->isGooglePlusOneEnabled()) {
lib/components/bundle/lib/MustacheTransformer.php::: ->socialButtons()->googlePlusOneButton($entry);
lib/mustache-templates/v1/bundle/article/list.mustache::: {{{googlePlusOneButtonHtml}}}
. . . . . . . . . .

Note that the script looks for the X string (in my case, googlePlusOne) and checks if a corresponding Y string exists. The script outputs any X lines that do not have corresponding Y lines.

Using this technique, the creation of feature Y becomes mechanical. You keep running the script, and keep implementing the lines that it finds. If there are any lines that are false-positives, you mark those lines in the script. When the script's output is empty, you're done.

Thus you can be quite confident that you have thoroughly implemented the new feature.