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 valid[1]), 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 repetitive.

Author: Robert Watkins

My name is Robert Watkins. I am a software developer and have been for over 20 years now. I currently work for people, but my opinions here are in no way endorsed by them (which is cool; their opinions aren’t endorsed by me either). My main professional interests are in Java development, using Agile methods, with a historical focus on building web based applications. I’m also a Mac-fan and love my iPhone, which I’m currently learning how to code for. I live and work in Brisbane, Australia, but I grew up in the Northern Territory, and still find Brisbane too cold (after 22 years here). I’m married, with two children and one cat. My politics are socialist in tendency, my religious affiliation is atheist (aka “none of the above”), my attitude is condescending and my moral standing is lying down.

2 thoughts on “Never use a working template”

  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.

  2. Never use a working template

    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…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: