{"id":177,"date":"2006-05-16T16:40:57","date_gmt":"2006-05-16T16:40:57","guid":{"rendered":"http:\/\/kubasek.com\/blog\/pragmatic_craftsman\/?p=177"},"modified":"2006-05-16T16:40:57","modified_gmt":"2006-05-16T16:40:57","slug":"never-duplicate","status":"publish","type":"post","link":"https:\/\/pragmaticcraftsman.kubasek.com\/2006\/05\/16\/never-duplicate\/","title":{"rendered":"Never Duplicate"},"content":{"rendered":"<p>How many times have you been thinking &#8220;let me just copy this here. It&#8217;s not going to change. And if it changes, I&#8217;m going to modify it in both places.&#8221;<\/p>\n<p>I think that&#8217;s a critical sin.<\/p>\n<p>You might remember, but the next maintainer might not.<\/p>\n<p>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.<\/p>\n<p>In my opinion, duplicating code is the single biggest sin that you can commit. Always try to refactor the code to avoid duplication.<\/p>\n<p>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&#8217;d rather copy it and modify it than refactor it. But I&#8217;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&#8217;m frustrated :-)).<\/p>\n<p>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&#8217;re duplicating; admit the sin :-)).<\/p>\n<p>Whenever I see a lot of duplication in a project, I know it&#8217;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.<\/p>\n<p>Good developers always strive to avoid duplication.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How many times have you been thinking &#8220;let me just copy this here. It&#8217;s not going to change. And if it changes, I&#8217;m going to modify it in both places.&#8221; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[3],"tags":[],"class_list":["post-177","post","type-post","status-publish","format-standard","hentry","category-better-coder"],"_links":{"self":[{"href":"https:\/\/pragmaticcraftsman.kubasek.com\/wp-json\/wp\/v2\/posts\/177","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pragmaticcraftsman.kubasek.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pragmaticcraftsman.kubasek.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pragmaticcraftsman.kubasek.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pragmaticcraftsman.kubasek.com\/wp-json\/wp\/v2\/comments?post=177"}],"version-history":[{"count":0,"href":"https:\/\/pragmaticcraftsman.kubasek.com\/wp-json\/wp\/v2\/posts\/177\/revisions"}],"wp:attachment":[{"href":"https:\/\/pragmaticcraftsman.kubasek.com\/wp-json\/wp\/v2\/media?parent=177"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pragmaticcraftsman.kubasek.com\/wp-json\/wp\/v2\/categories?post=177"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pragmaticcraftsman.kubasek.com\/wp-json\/wp\/v2\/tags?post=177"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}