The Pragmatic Craftsman :: Simplicity from complexity ::

Never Duplicate

How many times have you been thinking “let me just copy this here. It’s not going to change. And if it changes, I’m going to modify it in both places.”

I think that’s a critical sin.

You might remember, but the next maintainer might not.

I have commited it many times, no doubt about it. But it was never a good thing. I have also experienced the benefit of having a single place to make changes. And it feels great.

In my opinion, duplicating code is the single biggest sin that you can commit. Always try to refactor the code to avoid duplication.

What invites duplication is code complexity, I think. If I find a code that is hard to understand, I am afraid to modify it. I’d rather copy it and modify it than refactor it. But I’m still commiting the sin. So you should try to make our code as simple as possible (it is a lot harder to do than writing complex code, believe me). Whenever I see code that is simple, easy to understand, I have high regard for the developer that wrote it (imagine what I feel when I’m frustrated :-) ).

Avoiding duplication is tough sometimes. Sometimes it cannot be avoided. But you should have a mindset of never duplicating, be it the last resort. And if you do, you should feel that you did something wrong (write a comment to indicate that you’re duplicating; admit the sin :-) ).

Whenever I see a lot of duplication in a project, I know it’s not a quality project (no matter what it does). Youl want to write high-quality projects, right? Youl want to become a better developer, right? Avoiding duplication is a required route to take in that direction.

Good developers always strive to avoid duplication.

Comments are closed.

Random Quote

Topics

Tags

Archive

Recent Entries

Currently Reading

Shelfari: Book reviews on your book blog

:: The Pragmatic Craftsman recommends

:: The Pragmatic Craftsman book reviews

Info

© 2001-2010 Stanley Kubasek About me :: Contact me

@PragCraftsman on Twitter

SOLID principles come in handy here. Especially, "Open of Extension, Closed for Modification." One of the best principles I've learned. - 14 hours agoI actually try to follow @unclebobmartin's rule all the time. Sort of. I try not to make it worse. But to make it better? Great challenge! - 14 hours agoAlways check a module in cleaner than when you checked it out (via @unclebobmartin in 97 Things Every Progr.). I love the idea! - 14 hours agoI put Code Complete #1 over Martin's book because it has "awaken" me as a programmer. But other than that, Martin's book is the best! - 1 day ago@mcory1 First of all, @unclebobmartin is my hero! Agile Software Development, Principles, Patterns, and Practices is my #2. SOLID rules! :) - 1 day ago