More on Cut and paste
Saturday, May 31st, 2003After my recent rant on the evils of cut and paste, and some of the feedback I received on this, I have been thinking some more about it, and monitoring my own coding habits.
It was an interesting activity, leading to a sense of needing to repent of my earlier rashness. Cut and paste belong in an IDE, but care needs to be taken in their use.
One particular task I had that required a bit of C+P was i18ning a section of the app. Cut the String out, paste into a properties file, and then whack the lookup code and key back into the file. This task was a big C+P one, that really was aided by some IntelliJ coolness (Clipboard history rocks — <ctrl> <shift> <V>).
As mentioned in one of the comments of my earlier entry, Cut and Paste is also be useful when using an API in a similar way to before, although this often can be a precursor to doing a refactoring. Maybe the automatic insertion of TODO comment would be useful… or maybe not :). (cutting and paste multiple lines to do something is a code smell in need of refactoring).
BUT…. The usefulness of C+P doesn’t negate the risks involved in doing C+P. To keep the codebase clean care needs to be taken… Which is where a tool like PMD is great (thanks Tom). PMD does a great job at finding sections of code that have repeated tokens. It also does a couple of other useful bits of analysis (suggests Integer.toString(int) instead of (new Integer(int)).toString() for instance.
Try running it across your project and see what you find ;).