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.
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.
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…