<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Raviraj Jariwala</title>
    <description>The latest articles on DEV Community by Raviraj Jariwala (@raviraj_jariwala).</description>
    <link>https://dev.to/raviraj_jariwala</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F912161%2Fdcc2e50c-b5ff-4429-9183-1504f00a5d4f.jpg</url>
      <title>DEV Community: Raviraj Jariwala</title>
      <link>https://dev.to/raviraj_jariwala</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/raviraj_jariwala"/>
    <language>en</language>
    <item>
      <title>What I learned refactoring a codebase with a 7,558-line constants file"</title>
      <dc:creator>Raviraj Jariwala</dc:creator>
      <pubDate>Sun, 21 Jun 2026 14:37:24 +0000</pubDate>
      <link>https://dev.to/raviraj_jariwala/what-i-learned-refactoring-a-codebase-with-a-7558-line-constants-file-2abg</link>
      <guid>https://dev.to/raviraj_jariwala/what-i-learned-refactoring-a-codebase-with-a-7558-line-constants-file-2abg</guid>
      <description>&lt;p&gt;If you've ever opened a file to fix a bug and spent forty minutes just figuring out where the bug lives, this post is for you.&lt;/p&gt;

&lt;p&gt;Not because the bug was hard. Because the code had no home.&lt;/p&gt;

&lt;p&gt;I've been the sole frontend developer on a sports data platform serving 5+ professional sports clubs. I've worked on this codebase for a few years now, across everything on the frontend — auth flows, build configuration, product features, UI design implementation. When I joined, there were other developers on the team. By the time this refactor started, it was just me.&lt;/p&gt;

&lt;p&gt;One of the things I was working with was a file called &lt;code&gt;constant.js&lt;/code&gt; with &lt;strong&gt;7,558 lines&lt;/strong&gt; in it. That number tells you everything you need to know about the state of things.&lt;/p&gt;

&lt;p&gt;This is what I did about it, the decisions I made along the way, and what I'd share with anyone facing something similar.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR:&lt;/strong&gt; I restructured a 6-product React/Gatsby codebase — 1,100+ files, a 7,558-line constants file, 30-minute builds — into a clean module architecture, fixed two auth bugs affecting real users, and shipped the whole thing without pausing feature delivery.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  How codebases get this way
&lt;/h2&gt;

&lt;p&gt;It's worth starting here because I think developers are too quick to blame the people who came before them.&lt;/p&gt;

&lt;p&gt;Nobody writes a 7,558-line constants file on purpose.&lt;/p&gt;

&lt;p&gt;It starts as a place to put a few shared values. Then one developer adds a constant. Another adds five more. A deadline hits, someone adds twenty at the bottom and ships. Nobody sounds an alarm when a constants file hits 500 lines. Or 1,000. Over years, across multiple developers with different styles, that file becomes something nobody fully understands and everyone is afraid to touch.&lt;/p&gt;

&lt;p&gt;That's exactly what happened here.&lt;/p&gt;

&lt;p&gt;The platform is a React + Gatsby SaaS app for professional sports organisations. 6 products in one codebase: broadcast analytics, social media tracking, news sentiment, customer intelligence, deal management, revenue forecasting.&lt;/p&gt;

&lt;p&gt;When the project started, the team was moving fast on an early-stage product. That's the right call at that stage. You ship, you figure out architecture later.&lt;/p&gt;

&lt;p&gt;The problem is "later" kept getting pushed back. By the time I joined, there were a few developers' worth of different coding styles layered on top of each other with no consistent structure.&lt;/p&gt;

&lt;p&gt;Everything lived in &lt;code&gt;src/components/&lt;/code&gt; and &lt;code&gt;src/components/pages/&lt;/code&gt;. No grouping by product, no grouping by feature. Just files, alphabetically, forever.&lt;/p&gt;

&lt;p&gt;And honestly? I followed the same pattern when I joined. You do. You're onboarding into an existing codebase, you match what's already there.&lt;/p&gt;

&lt;p&gt;Then the product matured, the client base grew, and what used to be manageable became painful to work in every single day.&lt;/p&gt;




&lt;h2&gt;
  
  
  What had actually accumulated
&lt;/h2&gt;

&lt;p&gt;Here's the concrete picture. Before any refactoring, the codebase had:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;constant.js&lt;/code&gt; with &lt;strong&gt;7,558 lines&lt;/strong&gt;, a single file holding every constant the app had ever needed&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;utils.js&lt;/code&gt; with &lt;strong&gt;1,115 lines&lt;/strong&gt;, every helper function all in one place&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;src/components/&lt;/code&gt; with &lt;strong&gt;1,100+ files&lt;/strong&gt;, no grouping by product or feature&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;3 unused npm packages&lt;/strong&gt; still being compiled on every build (&lt;code&gt;@material-ui/core&lt;/code&gt;, &lt;code&gt;@material-ui/pickers&lt;/code&gt;, &lt;code&gt;apollo-boost&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;2 login bugs&lt;/strong&gt; affecting real users&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;30+ minute build times&lt;/strong&gt; on every deploy&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Those two login bugs are worth explaining because they were the most user-facing pain.&lt;/p&gt;

&lt;p&gt;The first: after your first login, changing the URL from the address bar logged you out. The post-login redirect logic was conflicting with the router state, and navigating away triggered it.&lt;/p&gt;

&lt;p&gt;The second: random mid-session logouts. Picture a club analyst mid-presentation, pulling up a live report, and suddenly staring at the login screen. The cause was the idle timer living in component state. Every re-render was resetting it, creating a race condition that eventually fired the logout.&lt;/p&gt;

&lt;p&gt;And on top of all this, there was the UI situation.&lt;/p&gt;

&lt;p&gt;Some clients were still on the old version. Others had been migrated to the new one. So at any point in time there were three things running:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The legacy UI (still live for some clients)&lt;/li&gt;
&lt;li&gt;The new UI (live for others, actively getting features)&lt;/li&gt;
&lt;li&gt;A refactoring branch sitting on top of the new UI&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Three states of the same codebase. Simultaneously. Picture trying to hold all three clearly in your head every morning before writing a single line of code. New features couldn't pause. Clubs were using this every day.&lt;/p&gt;




&lt;h2&gt;
  
  
  The call that started the refactor
&lt;/h2&gt;

&lt;p&gt;I had a conversation with the client and laid it out plainly.&lt;/p&gt;

&lt;p&gt;The codebase was slowing everything down. Debugging was slow, builds were slow, adding features was getting harder because nothing had a clear home. The ask was simple: let me restructure while still delivering features. No freeze, no "we'll pause and clean up."&lt;/p&gt;

&lt;p&gt;They agreed. And then came the harder part: figuring out how to actually do it.&lt;/p&gt;




&lt;h2&gt;
  
  
  The approach
&lt;/h2&gt;

&lt;p&gt;The temptation with a refactor like this is to fix everything in one big branch. I considered it. It would have been a mistake. It ends in a merge conflict nightmare that never ships.&lt;/p&gt;

&lt;p&gt;The constraint here, that features couldn't pause, was actually a gift. It forced the refactor into pieces small enough to be independently merged. One product per PR.&lt;/p&gt;

&lt;p&gt;Discover first, then Social, News, Broadcast, Deal Management, Media Hub. Six PRs, each self-contained, each mergeable without the others being finished.&lt;/p&gt;

&lt;p&gt;But before moving a single file, the target structure needed to be clear.&lt;/p&gt;

&lt;p&gt;Picture opening your editor on the old codebase and seeing this:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Before:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;src/
  components/          (1,100+ files, no product grouping)
  components/pages/    (same problem)
  utils/
    constant.js        (7,558 lines)
    utils.js           (1,115 lines)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;No labels. No ownership. Every file looks the same regardless of which of the 6 products it belongs to.&lt;/p&gt;

&lt;p&gt;Here's what the same codebase looks like after:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;After:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;src/
  core/                shared across 2+ products
    ui/                reusable UI components
    utils/             split by domain (dates, numbers, charts)
    constants/         split by domain
    hooks/
    services/
    auth/
    features/          global features with their own API calls
  modules/
    airwave/           broadcast analytics
    reach/             social media tracking
    headlines/         news sentiment
    compass/           customer intelligence
    pipeline/          deal management
    studio/            media hub
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can see at a glance which product a file belongs to. You can see what's shared and what isn't.&lt;/p&gt;

&lt;p&gt;The rule for &lt;code&gt;src/core/&lt;/code&gt; is simple: if more than one product needs it, it lives here. Nothing product-specific.&lt;/p&gt;

&lt;p&gt;The rule for &lt;code&gt;src/modules/{name}/&lt;/code&gt; is visual: the folder structure mirrors the URL. If the route is &lt;code&gt;/app/airwave/insights/overview&lt;/code&gt;, the file lives at &lt;code&gt;src/modules/airwave/pages/insights/Overview/&lt;/code&gt;. You read the URL, you know exactly where to look.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;src/core/features/&lt;/code&gt; is for global features that span products or don't belong to any one of them: things with their own API calls, side effects, and state.&lt;/p&gt;

&lt;p&gt;The last piece before starting was adding path aliases to &lt;code&gt;jsconfig.json&lt;/code&gt;: &lt;code&gt;@core/*&lt;/code&gt;, &lt;code&gt;@modules/*&lt;/code&gt;, and product-specific aliases for the most complex ones.&lt;/p&gt;

&lt;p&gt;This sounds small. It's probably the highest-leverage change in the whole refactor. No more &lt;code&gt;../../../components/something&lt;/code&gt;. Every import tells you where the file lives.&lt;/p&gt;




&lt;h2&gt;
  
  
  The mechanics of moving 1,100+ files
&lt;/h2&gt;

&lt;p&gt;This is where things actually go right or wrong, so it's worth being specific.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step one: import conversion.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I wrote a script to convert relative imports to absolute imports using the new path aliases. The important thing was running it one feature folder at a time, not across the whole codebase in a single pass.&lt;/p&gt;

&lt;p&gt;If something broke, the blast radius was one feature. Fix it, verify, move to the next folder. Simple but it saved a lot of pain.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step two: file moves.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;VS Code's built-in file move handles most import updates automatically. For everything else, &lt;code&gt;git mv&lt;/code&gt; in the terminal.&lt;/p&gt;

&lt;p&gt;This matters more than it sounds. &lt;code&gt;git mv&lt;/code&gt; tells git the file moved rather than treating it as a deletion and a new file. After the migration, &lt;code&gt;git blame&lt;/code&gt; and &lt;code&gt;git log --follow&lt;/code&gt; still work. You can still trace why a line was written two years ago by someone no longer on the team.&lt;/p&gt;

&lt;p&gt;In a codebase without a test suite, that history is one of the few tools you have for understanding why something was done a certain way. Worth protecting.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What each PR looked like:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Every product PR ended the same way. The last few commits were always: organise product-specific constants into the module, pull anything shared into &lt;code&gt;src/core/&lt;/code&gt;, and break down any file over 500 lines into smaller, readable pieces.&lt;/p&gt;

&lt;p&gt;Not perfectly small. Just below 500 and coherent enough to open without getting lost.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The hardest part:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The deal management module was the most difficult. It felt like rearranging a room while someone was still living in it. That product was being actively changed based on client feedback at the same time I was restructuring its files. Every day there was a new conflict to resolve between the refactoring branch and what was landing on the main branch.&lt;/p&gt;

&lt;p&gt;Honestly, I thought about skipping the deal management module entirely and coming back to it later. Instead I skipped individual files within it when the conflict risk wasn't worth it, noted them, and moved on. A partial migration that ships cleanly beats a complete one that breaks something in production.&lt;/p&gt;




&lt;h2&gt;
  
  
  Fixing the bugs
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Login bug one (URL change logging users out):&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The post-login redirect logic was conflicting with the router state. Navigating via the address bar after first login triggered the logout flow.&lt;/p&gt;

&lt;p&gt;Fixed by simplifying: always redirect to the home route on login, then let the router handle navigation from there. One clear responsibility, no conflicts.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Login bug two (mid-session logouts):&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The idle timer moved from component state to &lt;code&gt;useRef&lt;/code&gt;. No more re-renders, no more race condition.&lt;/p&gt;

&lt;p&gt;User data fetching changed from sequential to concurrent. And localStorage cleanup on logout was scoped to only the specific keys that needed clearing, instead of wiping everything.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The build time:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Removing &lt;code&gt;@material-ui/core&lt;/code&gt;, &lt;code&gt;@material-ui/pickers&lt;/code&gt;, and &lt;code&gt;apollo-boost&lt;/code&gt; from &lt;code&gt;package.json&lt;/code&gt; was the bulk of the improvement. They were being compiled on every build and hadn't been used in a long time.&lt;/p&gt;




&lt;h2&gt;
  
  
  What changed
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;3,900+ files touched&lt;/strong&gt; across 74 commits&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Around 18,000 net lines removed&lt;/strong&gt; (284k deleted, 265k added)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Build time: 30+ minutes → 17-18 minutes&lt;/strong&gt;, just from removing dead weight&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Both login bugs resolved&lt;/strong&gt;, no reports since&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;All 6 products&lt;/strong&gt; with their own clearly bounded home in the codebase&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No client disruption&lt;/strong&gt; throughout, legacy and new UI both stayed live&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  What I'd do differently
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Set up the structure before anyone writes code against the old one.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The hardest part of this refactor wasn't the architecture decisions. It was migrating files while new code was still landing in the old locations. If the module structure and path aliases exist from the start, people move their own code naturally. Running a large migration alongside active development adds friction that didn't need to be there.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Add ESLint rules early.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Right now the conventions live in documentation. That works while I'm the only developer. But the moment anyone else touches the codebase under pressure, the first thing they'll do is import from the familiar old path.&lt;/p&gt;

&lt;p&gt;A linting rule that fails the build enforces the architecture without relying on anyone's memory. You set it once, it quietly holds the line for you after that. Two lines of config, permanent guardrails.&lt;/p&gt;




&lt;h2&gt;
  
  
  The thing worth remembering
&lt;/h2&gt;

&lt;p&gt;Messy codebases aren't created by bad developers.&lt;/p&gt;

&lt;p&gt;They're created by good developers solving the right problems at the right time, without the bandwidth to also clean up behind themselves. The mess accumulates quietly, one reasonable decision at a time, until one day it feels heavy enough to slow everything down.&lt;/p&gt;

&lt;p&gt;The lesson isn't to plan everything perfectly from day one. That's not realistic when a product is still finding its shape.&lt;/p&gt;

&lt;p&gt;The lesson is to recognise when the codebase needs to grow up along with the product, and to make that investment before the cost gets too high.&lt;/p&gt;

&lt;p&gt;Looking back, the hardest part wasn't technical. It was doing this alongside active feature delivery without letting either side slip. Almost every win came from removing things, not adding them. The faster build came from deleting dead packages. The auth reliability came from moving a variable to the right place. The debuggability came from splitting nearly 9,000 lines of monolithic files into files that know what they are.&lt;/p&gt;

&lt;p&gt;If your codebase feels like this right now, you don't need the perfect plan before you start. Pick one product, one module, one folder. Clean it up. Ship it. Then do the next one.&lt;/p&gt;

&lt;p&gt;That's really the whole approach.&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
