The Pragmatic Craftsman :: Simplicity from complexity : by Stanley Kubasek ::

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.

Favorite Quote




Currently Reading


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

Me on Twitter

»see more

Recent Entries