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:
- You don't need to think much - just find each line of code for X and make an equivalent line of code for Y.
- 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.
0 Comments:
Post a Comment
<< Home