Sunday, November 20, 2011

Copy and Paste is evil

I consider copy and paste of code one of the most common sources of bugs and bad coding practices. First of all, let's talk about bugs. If you copy and paste code, it's likely you're doing it because you don't know what the code is doing; you only know that it works. You only know that it works, and copying and pasting is easier than re-typing. However, adding code without knowing what it does is a recipe for disaster. A couple of days ago I spent 5 hours troubleshooting code that another developer "copied and pasted". If you're going to copy and paste, at least make sure that you read the code you just copied and pasted.

Moving on bad coding practices. Well, this is an easy one. Duplicate code means that any change needs to happen in 2 different places, and you don't want that. You want classes with clear boundaries, otherwise your code will soon become a big ball of mud.

No comments: