Skip to main content
Published in Engineering Insights

Building for Flexibility

Building for Flexibility
Table of contents
  1. What is test-driven development, and how does it handle changing requirements?
  2. What should happen to the tests when requirements change?
  3. How do teams find and remove dead code?
  4. How do feature flags turn into dead code?
  5. Does technical debt matter if the product might change direction again?

The best hires aren't the ones you bring on because they have a specific skill set.

They're the ones you bring on because they're hungry to learn and eager to solve problems.

The first job they have in your organization might be to fill an acute gap. But when things change, and they always change, these hires happily shift to the next gap and fill it well.

If you track the best ones over the years, you'll see their value grow and grow. Not because they retain everything they've learned to do, but because they've figured out exactly what they should remember and what just isn't important.

A lot of things should work like that.

And if you're intentional about it, they can.

In software development, we build products with specific uses in mind. There's really no other way to do it but, generally, most engineering and product leaders recognize that the final piece of software might not look or behave exactly as the initial roadmap dictated.

We're also very familiar with examples of software that started as one thing and became something entirely different. Slack is a classic story of a technology that started as an internal communication tool for a video game.

But the lesson we often learn here is not, "I should build a solid product foundation that's flexible to changes". Instead the lesson is, "I should constantly iterate and not be afraid to make sweeping changes based on market feedback." And so, what we're left with are products and features that have finally found product market fit but are built on spaghetti code, unused code, and code that's been commented out.

We never learn exactly what's important to keep and what can be discarded.

If you want to build a flexible product that can withstand iterations and market changes, you need to have discipline in the process. That starts with test-driven development. We talk to new teams (and some established ones) who forgo testing, saying "we haven't been testing because things are changing so quickly." But that's exactly why they should be testing, to make sure they're next iteration isn't carrying the dead weight of a previous version.

Building for flexibility can also be a matter of de-risking the project. Any new product or feature comes with significant risk, and there are a lot of variables at play, not just technical. There's a very good chance it's not worth using your internal team to build it out, and keeping them focused on the core product. You can buy down that risk with an external team (with a time & materials model) that can build a strong, flexible foundation but be nimble enough to make changes as they go.

What might be most important to keep in mind may be this: your goal shouldn't be to build a certain product, it should be to solve a certain problem. And balancing flexibility with discipline is the best way to do that.  

What is test-driven development, and how does it handle changing requirements?#

Test-driven development (TDD) handles a changed requirement like any new behavior. The programmer writes a failing test for the new behavior first, then changes the code until it passes. Kent Beck calls it "a programming workflow" in Canon TDD. His starting point: "A programmer needs to change the behavior of a system (which may be empty just now)."

This post makes TDD the starting point for a flexible product: "That starts with test-driven development."

Martin Fowler's summary says Beck developed TDD "as part of Extreme Programming." Fowler sums up the loop in three steps. "Write a test for the next bit of functionality you want to add." Then "Write the functional code until the test passes." Then "Refactor both new and old code to make it well structured." Fowler also calls a list of test cases written first "a vital initial step." Beck set out the steps again in his December 2023 post. Each step below carries his own words.

  1. Test List. "Write a list of the test scenarios you want to cover"
  2. Write a Test. "Turn exactly one item on the list into an actual, concrete, runnable test"
  3. Make it Pass. "Change the code to make the test (& all previous tests) pass (adding items to the list as you discover them)"
  4. Optionally Refactor. "Optionally refactor to improve the implementation design"
  5. Until the Test List is Empty. "Until the list is empty, go back to #2"

Beck separates two kinds of design. One is "How a particular piece of behavior is invoked." The other is "How the system implements that behavior." Implementation design waits for the refactor step: "Now you get to make implementation design decisions."

Beck also names the state each change should leave behind. Two of his four points are "Everything that used to work still works." and "The system is ready for the next change." The second point is the one this post cares about when it warns about dead weight.

Fowler adds a warning about the refactor step, the third of his three. "The most common way that I hear to screw up TDD is neglecting the third step." Without it, he writes, "we just end up with a messy aggregation of code fragments."

A Gnar engineering post on its development process describes the same habit. "In test-driven development, each new feature includes writing a test (in code) to assert the behavior of the feature." It adds that including a test "ensures that the developer stays committed to the requirements of the feature."

What should happen to the tests when requirements change?#

When a requirement changes, someone decides what the new behavior is, and the tests that check the old behavior change to say so. In Beck's method, a new scenario goes on the test list and gets its own test.

One shortcut is to paste the code's new output into the test so it passes. Beck lists that shortcut as a mistake: "copying actual, computed values & pasting them into the expected values of the test." He writes that it "defeats double checking." He also lists "delete assertions so the test pretends to pass."

A Gnar engineering post in defense of unit tests makes a related point about fixing tests to match new code. Doing it automatically "might work in simple cases, but in complex applications, it amounts to changing a specification by fiat and at will." The same post says a feature branch that breaks a unit test "implies a potential broken contract somewhere else in the codebase." It also says "if there is no spec, the unit tests become the de facto spec."

New cases also show up mid change. Beck says to add them to the list as they appear. If a new test shows the work so far cannot handle a case, "you need to decide whether to push on or start over."

What should a test describe? Fowler says writing the test first "forces us to think about the interface to the code first." The Gnar post draws its line there too: "anything you'd call an "interface" counts, and anything you'd call "implementation" doesn't."

How do teams find and remove dead code?#

Teams find dead code with more than one signal, then delete it. Meta combines static and dynamic analysis of programs. It also falls back on a search for textual references. Understand Legacy Code adds production logs for code that may still run. Once a team is confident the code is unused, that source says to get rid of it, and git keeps the history.

Why remove it at all? Understand Legacy Code puts it plainly: "unused code adds cruft to the codebase, making it harder to read and reason about." It adds that "Code usually becomes dead after the software has been changed or fixed."

This post names what iteration leaves behind: "spaghetti code, unused code, and code that's been commented out." It also says, "We never learn exactly what's important to keep and what can be discarded." The checklist below shows how teams answer that for unused code.

  • Analyze the code. Meta's engineering blog says its Systematic Code and Asset Removal Framework (SCARF) "combines static and dynamic analysis of programs to detect dead code."
  • Check production for code you are unsure about. Understand Legacy Code's advice: "find a way to monitor when the code is called in production, wait, then get your answer from the logs."
  • Search for names used as text. Meta also searches for textual references, because some languages "can call code through string references or use eval."
  • Delete the code. The same Understand Legacy Code post says: "With git, you won't lose that piece of code."
  • Know how to get it back. The git log option -G will "Look for differences whose patch text contains added/removed lines that match <regex>."
  • Review each deletion. SCARF "automatically creates change requests that delete the dead code." Meta's 2023 post says "False-positives caught by engineers during code review are triaged."

How do feature flags turn into dead code?#

A feature flag turns into dead code when the decision behind it is made but the flag check stays in the code. PostHog's documentation says disabling a flag "stops it from evaluating, but the flag check stays in your codebase as dead code." Removing it means choosing, per flag, which code path to keep.

This post traces unused code to a lesson teams draw from products that changed course. The lesson: "I should constantly iterate and not be afraid to make sweeping changes based on market feedback." Feature flags are one way teams run those changes. Pete Hodgson's feature toggles article on Martin Fowler's site calls them "a powerful technique, allowing teams to modify system behavior without changing code."

The same article says toggles "require you to introduce new abstractions or conditional logic into your code." It says savvy teams view them "as inventory which comes with a carrying cost." Release toggles "should generally not stick around much longer than a week or two." Hodgson adds "although product-centric toggles may need to remain in place for a longer period."

PostHog's cleanup guide gives the rule for which path to keep.

Flag stateCode path to keepPostHog's words
Fully rolled out, at 100% with no targeting conditionsThe enabled path"The enabled code path is the one to keep." For multivariate flags, "keep the winning variant's code path and remove the rest." It also warns that a flag at 100% might mean "about to roll back, keep the old path."
Not rolled out, at 0% or no release conditionsThe else or fallback path"Keep the else/fallback path."
Partially rolled out, with some targetingDecided from the flag's intent"You'll need to check the flag's intent to decide which code path to keep. These need manual review."

PostHog counts a flag as stale in two cases, though disabled flags "are not considered stale since they were intentionally turned off." One case is "Not evaluated in 30+ days." The other case is "100% rolled out for 30+ days with no property filters." In that case, "The flag check is equivalent to a hardcoded value."

Order matters in PostHog: "Remove the flag from your code first." Its safe sequence then says "Deploy the code changes" before you "Disable the flag in PostHog." A flag at 100% may still be checked by code, and disabling it "turns the feature off for everyone." PostHog adds: "The API can tell you the rollout state, not the team's intent."

Hodgson's article lists ways teams keep flag counts down. Some add "a toggle removal task onto the team's backlog" when a release toggle is created. Others set "expiration dates," and some build "time bombs" that fail a test after that date. Some set "a limit on the number of feature flags a system is allowed to have at any one time."

Does technical debt matter if the product might change direction again?#

Yes, it matters most in the code a team keeps changing and working in. Martin Fowler says "crufty but stable areas of code can be left alone." He describes technical debt as cruft that makes a system "harder than it would ideally be to modify and extend." He calls the extra effort it takes to add something new "the interest paid on the debt." He adds that the interest is triggered only when you work with that part of the software.

He draws the contrast too: "In contrast, areas of high activity need a zero-tolerance attitude to cruft." He suggests paying it down gradually, which means "we spend more time on removing cruft in those areas that we modify frequently."

That fits this post's closing line: "balancing flexibility with discipline is the best way to do that." Gnar's code migrations and refactors page describes the work: "Refactoring means cleaning up code without changing what it does. We do it when a part still does its job, and write tests as we go."

Gnar's November 2026 post on replatforming, refactoring and rebuilding gives a sign to look for. When "The software works, but each change breaks something else," the post's table lists what it usually points to as "Refactor." The first thing it says to check: "Whether tests exist, so the cleanup can be checked."

Frequently asked questions

Why is iteration alone not enough?

A product can find product market fit while still being built on tangled, unused and commented-out code. A flexible foundation also requires deciding what to keep and what to discard.

Should a team skip testing when requirements change quickly?

No. That is exactly when the team should be testing, so the next iteration does not carry dead weight from a previous version.

How can an external team reduce project risk?

An external team working on a time & materials model can build a flexible foundation and adapt as the work changes. That can keep the internal team focused on the core product.

What are the steps of test-driven development?

Kent Beck's Canon TDD lists five. Write a list of test scenarios. Turn exactly one into a runnable test. Change the code until it and all earlier tests pass. Optionally refactor. Repeat until the list is empty. New scenarios found along the way go on the list.

How do teams find dead code safely?

With more than one signal. Meta's dead code system combines static and dynamic analysis, and also searches for names in text, because some code is called by string. For code that may still run, Understand Legacy Code suggests watching production logs before deleting.

When should a feature flag be removed?

Once the decision behind it is made. PostHog counts a flag as stale after 30 days at a 100% rollout with no filters, or 30 days without being checked, unless the flag is disabled, and says to remove it from code before disabling it. Pete Hodgson says release toggles "should generally not stick around much longer than a week or two," though product-centric ones may need longer.

Is it safe to delete unused code instead of commenting it out?

Yes, once you are confident it is unused and the code is committed to version control. Understand Legacy Code notes that with git you will not lose that code. The git log search option can find the commits that removed a name. Commented-out code stays in the file for every reader.

Stay In The Loop

Never miss an update. Get the latest blogs, webinars, and expert insights straight to your inbox from The Gnar.