DEV Community

Cover image for Engineering Intuition: Knowing When to Refactor
Doogal Simpson
Doogal Simpson

Posted on • Originally published at doogal.dev

Engineering Intuition: Knowing When to Refactor

Quick Answer: That sudden feeling of dread when opening a code file is a highly valuable engineering signal from your subconscious "lizard brain." Before your logical mind processes the mess, your gut recognizes technical debt. Use this instinct to identify areas needing refactoring, but balance it with pragmatic "good enough engineering" to avoid endless rewrites.

We have all been there. You click on a file in your editor, the syntax highlighting kicks in, the scrollbar shrinks to a microscopic sliver, and an immediate pit forms in your stomach.

You haven't even read a single function yet, but you already know you don't want to be here.

Sometimes, I think you need to follow that lizard brain when you are reading code. It is a primitive, instinctual reaction, and in software engineering, it is one of the most reliable signals you have. Before your higher brain can logically parse the variables, dependencies, and control flows, your subconscious has already pattern-matched the file as a danger zone. Instead of ignoring that dread, you can use it to guide your technical decisions.

Why do developers get a feeling of dread when reading code?

Developers feel dread because their subconscious pattern-matching engine recognizes high-risk, overly complex code before their conscious mind can process it. It is a rapid, biological warning system alerting you to massive technical debt or fragile architecture.

Imagine your team is taking over an older e-commerce platform. You open the main checkout module and instantly feel a wave of anxiety. This happens because your brain is reacting to visual cues—deep nesting, endless blocks of text without whitespace, or a massive import list—that historically correlate with painful debugging sessions.

When you get this feeling, pause. Instead of forcing yourself to immediately build your new feature on top of a terrible foundation, sit there and let your higher brain catch up. Analyze the file. Usually, you will quickly spot exactly what triggered your lizard brain: a god object, tangled state management, or tightly coupled logic. Recognizing this early gives you a chance to clean up the workspace before you accidentally break it.

When should I refactor bad code versus leaving it alone?

You should refactor when the bad code actively blocks your current task, makes testing impossible, or introduces high risk to an upcoming change. If the code is a mess but is rarely touched and functions perfectly in production, it is usually better to leave it alone to prioritize delivering actual value.

Constantly refactoring everything just because a file gives you a bad vibe is a trap. If you rewrite every ugly file you encounter, you will never ship anything. This is especially true if you are a newer engineer. When you are just starting out, almost every complex file looks intimidating and induces dread. You have to learn the difference between code that is genuinely broken and code that is just unfamiliar.

To help decide when to act on that feeling of dread, use this decision framework:

  • Active Modification: Are you required to add new logic to this specific file today? (If yes, consider refactoring).
  • Blast Radius: Will touching this code likely break unrelated features? (If yes, add test coverage and refactor first).
  • Frequency of Change: Is this a file the team touches weekly, or hasn't it been modified in three years? (If rarely touched, leave it alone).
  • Deliverable Timeline: Do you have the buffer to clean this up without missing a hard product deadline? (If no, log it as technical debt and move on).

How do I balance code quality with fast delivery?

Balancing quality and delivery requires treating refactoring as a targeted, incremental cost rather than a separate, massive project. You achieve this through "good enough engineering," where you intentionally balance the strict pursuit of clean code against the immediate need to ship deliverables.

Refactoring takes time, and time is a tangible cost to your business. You cannot fix everything. Good enough engineering means accepting that some files will remain ugly.

Let's say you have a legacy analytics service that processes background events. It is messy, but it works flawlessly and no one needs to update its core logic. Spending three days making it beautiful provides zero value to the end user. However, if you are actively working inside a tangled billing module to add a new payment gateway, spending a few hours untangling the specific functions you need to interact with is a highly pragmatic investment.

Listen to your lizard brain when it warns you of danger, but let your logical brain decide if fighting that danger is actually worth the time.

FAQ

How do you know if code is actually bad or if you are just inexperienced?

Inexperience often makes standard, complex patterns feel overwhelming. The best way to check is to consult a senior engineer or run a static analysis tool. If the complexity is necessary for the domain, it is just a learning curve; if the logic is genuinely tangled and untested, it is bad code.

What is "good enough engineering"?

Good enough engineering is a pragmatic approach to software development where you write code that is secure, functional, and maintainable without obsessing over perfect architecture. It is the active balancing of technical quality against business deliverables and deadlines.

How do I justify refactoring time to product managers?

Frame refactoring in terms of business value and risk mitigation, not code aesthetics. Explain that cleaning up a specific module will reduce bugs in the upcoming release and significantly speed up the time it takes to add the next feature.

Top comments (0)