<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Coder Legion - Recent posts tagged dry</title>
<link>https://coderlegion.com/tag/dry</link>
<description>Powered by Question2Answer</description>
<item>
<title>Don’t Repeat Yourself (DRY) the Right Way: How to Reuse Code Without Creating a Mess</title>
<link>https://coderlegion.com/7810/dont-repeat-yourself-dry-the-right-way-how-to-reuse-code-without-creating-a-mess</link>
<description>&lt;p&gt;Every developer has heard “Don’t Repeat Yourself” (DRY)&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;em&gt;How much code reuse is too much?&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Am I over-abstracting?&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Should this be a helper? A util? A service? A hook?&lt;/em&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The truth:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Bad DRY creates more problems than it solves.&lt;/li&gt;
&lt;li&gt;Good DRY makes your code predictable, clean, and future-proof.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;DRY is not just removing duplication&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;it’s learning what deserves reuse and what deserves separation.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This post walks you through how to write reusable code the right way&lt;/p&gt;
&lt;h3 id=&quot;why-dry-matters&quot;&gt;&lt;em&gt;Why DRY Matters&lt;/em&gt;&lt;/h3&gt;
&lt;p&gt;Good reuse helps you:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Fix bugs once instead of 5 times&lt;/li&gt;
&lt;li&gt;Scale faster&lt;/li&gt;
&lt;li&gt;Reduce cognitive load&lt;/li&gt;
&lt;li&gt;Keep your project consistent&lt;/li&gt;
&lt;li&gt;Avoid “copy-paste debt”&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;But bad reuse leads to:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Unnecessary abstractions&lt;/li&gt;
&lt;li&gt;Confusing utilities&lt;/li&gt;
&lt;li&gt;One giant file everyone is scared to touch&lt;/li&gt;
&lt;li&gt;“Helper” folders full of black magic&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;em&gt;Let’s fix that.&lt;/em&gt;&lt;/p&gt;
&lt;h3 id=&quot;step-1-dry-only-when-the-behavior-repeats-not-the-code-&quot;&gt;&lt;strong&gt;Step 1: DRY Only When the Behavior Repeats (Not the Code)&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Most developers refactor too early.&lt;/p&gt;
&lt;p&gt;Look at these two functions:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;function createUser() {
  console.log(&quot;Creating user…&quot;);
  // some logic
}

function createProduct() {
  console.log(&quot;Creating product…&quot;);
  // some logic
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;These look similar.&lt;br&gt;
But should they be merged?&lt;/p&gt;
&lt;p&gt;❌ No.&lt;br&gt;
The only repeated line is a console log the behavior is different.&lt;/p&gt;
&lt;p&gt;DRY is not about removing repeated lines.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;It’s about removing repeated logic.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&quot;step-2-start-with-local-reuse-before-global-reuse-&quot;&gt;&lt;strong&gt;Step 2: Start With “Local Reuse” Before “Global Reuse”&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Most developers jump straight to a /utils folder.&lt;/p&gt;
&lt;p&gt;Instead, follow this rules&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;If only one feature uses it, keep it inside that feature.&lt;/li&gt;
&lt;li&gt;If two features use it, promote it to a shared place.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;strong&gt;Example React structure:&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;/features
  /auth
    login.js
    useLogin.js
  /dashboard
    useDashboard.js
/shared
  /hooks
    useFetch.js   ← used by multiple features
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You “graduate” code only when needed.&lt;/p&gt;
&lt;h3 id=&quot;step-3-extract-reusable-code-the-smart-way&quot;&gt;&lt;strong&gt;Step 3: Extract Reusable Code the SMART Way&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Before extracting shared code, ask:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Will this logic change in different ways depending on the feature?&lt;/p&gt;
&lt;p&gt;   If yes → keep it local.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Does this logic repeat AND behave the same?&lt;/p&gt;
&lt;p&gt;If yes → extract it.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Does extraction make the code clearer or more confusing?&lt;/p&gt;
&lt;p&gt;If confusing → stop. &lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&quot;step-4-real-examples-of-good-reuse&quot;&gt;&lt;strong&gt;Step 4: Real Examples of Good Reuse&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Example: Reusable API Client&lt;/p&gt;
&lt;p&gt;Bad:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;fetch(&quot;/api/users&quot;)
fetch(&quot;/api/products&quot;)
fetch(&quot;/api/orders&quot;)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Good:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;export async function api(url, options = {}) {
  return fetch(url, options).then(res =&amp;gt; res.json());
}
&lt;/code&gt;&lt;/pre&gt;
&lt;h3 id=&quot;step-5-the-dry-traps-avoid-these-at-all-costs-&quot;&gt;&lt;strong&gt;Step 5: The DRY Traps (Avoid These at All Costs)&lt;/strong&gt;&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Over-abstracting early&lt;/p&gt;
&lt;p&gt;Don’t create files “just in case.”&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Giant utility folders&lt;/p&gt;
&lt;p&gt; If you have a helpers.js with 37 functions, it’s time to rethink.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Mixing unrelated logic&lt;/p&gt;
&lt;p&gt;utils/index.js containing API logic, string formatting, date parsing, and a random debounce function.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Creating shared code for one-off logic&lt;/p&gt;
&lt;p&gt; Not everything needs reuse.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&quot;step-6-when-to-refactor-for-reuse&quot;&gt;&lt;strong&gt;Step 6: When to Refactor for Reuse&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;Refactor when:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Two modules copy-paste the same logic&lt;/li&gt;
&lt;li&gt;Fixing a bug means changing multiple files&lt;/li&gt;
&lt;li&gt;Code feels “the same but slightly different”&lt;/li&gt;
&lt;li&gt;You keep writing similar boilerplate&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;em&gt;If you catch yourself repeating work, that’s your signal.&lt;/em&gt;&lt;/p&gt;
&lt;h3 id=&quot;final-thought&quot;&gt;&lt;strong&gt;Final Thought&lt;/strong&gt;&lt;/h3&gt;
&lt;p&gt;DRY isn’t about having zero duplication, it’s about smart duplication and smarter reuse.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Good code is:&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;clear&lt;/li&gt;
&lt;li&gt;predictable&lt;/li&gt;
&lt;li&gt;discoverable&lt;/li&gt;
&lt;li&gt;reusable when needed&lt;/li&gt;
&lt;li&gt;duplicated when necessary&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Yes, duplication is sometimes the right choice.&lt;/p&gt;
&lt;p&gt;Your goal isn’t to write fancy code.&lt;br&gt;
Your goal is to write code that future you, your teammates, and your project can trust.&lt;/p&gt;
</description>
<category>Java</category>
<guid isPermaLink="true">https://coderlegion.com/7810/dont-repeat-yourself-dry-the-right-way-how-to-reuse-code-without-creating-a-mess</guid>
<pubDate>Tue, 25 Nov 2025 16:28:41 +0000</pubDate>
</item>
</channel>
</rss>