« Expressing Intent vs Duplication | Main | There's more to managing an economy than lowering taxes »

Never use a working template

A common feature of many IDE are templates, which greatly speed writing code. People often create their own "cut-and-paste" templates as well.

An important rule to remember here is that no template should ever "work" out of the box. This way, if (for whatever reason!) you don't fill it in, you know it will fail fast. This helps avoid bugs that can be quite subtle: template code which fails slowly. :)

In Java IDEs, for example, I configure the template used for new methods to throw a java.lang.UnsupportedOperationException.

When I use TDD-techniques to write code, the usual procedure is to fill out (part of) the test case, and fix the compile errors. "Fixing the compile errors" may sometimes involve creating several methods. By using the "broken" template, I can easily just run my test, and fill in the methods as I go.

Similarly, if you're using cut-and-paste (which is sometimes valid1), don't cut-and-paste something that works; cut and paste something that is blatantly wrong - make a parameter something like "CHANGE_ME", or introduce a deliberate syntax error. That way, when you customise the cut-and-paste template, you can fix the error as you go.

----

1 An example of "valid" cut-and-pasting occurs when doing TDD, particularly for testing validation rules. The tests in these scenarios are often fairly repeatitive.

TrackBack

Listed below are links to weblogs that reference Never use a working template:

» Never use a working template from Nerdherding for Beginners
Some good advice from a neat programming blog - Software is too expensive to build cheaply.... I have occasionally fallen into the cut-and-paste variant of this trap (mentioned in a comment to the post). Doing too many things at once... [Read More]

Comments (1)

Same "rule" applies to copy/paste of code. The best way to do it is to copy the code and paste it into a new page in your editor, and then change everything that needs to be changed so it will not compile, then copy/paste that code into the real destination.

Post a comment


About

This page contains a single entry from the blog posted on January 9, 2005 11:38 PM.

The previous post in this blog was Expressing Intent vs Duplication.

The next post in this blog is There's more to managing an economy than lowering taxes.

Many more can be found on the main index page or by looking through the archives.

Creative Commons License
This weblog is licensed under a Creative Commons License.
Powered by
Movable Type 3.35