Avoiding Hardcoded Business Logic in Enterprise Applications
Every enterprise codebase contains a line that encodes a policy someone will change. A percentage, a threshold, a list of statuses that skip a check, a branch for one region. Each was correct when written, and each becomes a support ticket the day the policy moves.
The standard advice is to externalize. That advice is right often enough to be dangerous, because it is usually given without the second half: externalized logic is still logic. It has simply moved somewhere that has no compiler, no tests, no code review, and frequently no history of who changed what.
A spectrum, not a binary
The choice is not hardcoded against configurable. There are several steps between, each buying changeability at a specific price.
| Level | Changed by | Price paid |
|---|---|---|
| Literal in a method | A developer, in a release | Invisible; found only by reading the code |
| Named constant | A developer, in a release | None worth mentioning - always do at least this |
| Configuration file or setting | An administrator, at deploy or runtime | Leaves the type system and the test suite |
| Database table with an admin screen | A business user, at runtime | Needs validation, audit, and a way to review changes |
| Rules engine | An analyst, with governance | New skill set, new deployment story, real setup cost |
| Custom expression language | Whoever learns it | You now own a language and its tooling |
The mistake is not choosing the wrong row. It is choosing a row without noticing that the price column exists, and then being surprised when a configurable system becomes unpredictable.
What externalizing actually removes
A value in code is protected by machinery that is easy to take for granted. Moving it out gives up all of it at once unless you deliberately rebuild each piece.
- Type checking: a percentage stored as text can become the string 'twenty five' and nothing objects until it is used.
- Tests: nothing exercises the configured value, so a wrong setting is discovered by its consequences.
- Review: a change that would have needed approval as a pull request now needs none.
- History: without deliberate auditing, there is no record of what the value was when a case was processed.
- Locality: the logic no longer reads as one thing, because half of it is somewhere else.
None of these are arguments against externalizing. They are the checklist for doing it properly, and skipping the checklist is what produces systems where the answer to why did this case compute differently is genuinely unknown.
Four questions that choose the level
- How often does it change? Yearly policy revisions justify configuration; a value that has never changed does not.
- Who must be able to change it? If a developer release is acceptable, stop at a named constant - it is the cheapest option that is not a literal.
- What is the blast radius of a wrong value? A rate that affects every payment needs validation and review regardless of where it lives.
- Must past cases be reproducible? If yes, the value must be versioned and recorded per case, not simply stored as current state.
The fourth question is the one most often missed, and it is the most expensive to retrofit. A system that stores only the current rate cannot explain a payment made under the previous one, and by the time anyone asks, the old value is gone.
When hardcoding is the right answer
Some logic should be in code, stated plainly and left there. Rules that derive from the structure of the domain rather than from policy - that a payment cannot exceed its invoice, that a date of completion follows a date of start - are not going to change with a circular from head office. Making them configurable adds a setting that must be validated, tested, and defended, in exchange for flexibility nobody will ever use.
A useful test: if changing the value would require changing code anyway - because the surrounding logic only makes sense for certain values - it is not configuration. It is a constant with extra steps, and pretending otherwise creates a setting that can be changed into a broken state.
The over-configuration failure mode
The opposite extreme is less discussed and just as damaging. Its symptoms are recognisable:
- Understanding behaviour requires reading code and several configuration sources together, and no environment matches another.
- Settings encode branching, so the configuration has become an untested program.
- Flags added for one rollout are still present years later, and nobody knows which combinations are exercised.
- Changing configuration still requires a developer, because only they know which combinations are safe.
- Bugs cannot be reproduced without an exact copy of production settings.
At that point the system is not flexible. It is a program written in a language with no syntax checking, deployed without review. The original goal - letting the business change policy safely - has been lost while paying the full cost of the mechanism.
Making externalized logic trustworthy
- Validate on write, not on use, so an invalid value is rejected by the person who entered it rather than discovered by a customer.
- Version every change, with who and when, and keep the old values rather than overwriting them.
- Record on each processed record which version of the policy applied to it.
- Test the configuration: a scenario suite that runs against the current settings catches a bad change before it ships.
- Make the effective values visible in the application, so support can see what the system believes without database access.
- Review changes to high-blast-radius settings with the same seriousness as code.
This is a different concern from hardcoded identifiers in queries and scripts, which is about portability between environments rather than about policy that changes over time. Both are called hardcoding, and they have different fixes - one wants lookup by stable business keys, the other wants versioned, governed configuration.
Conclusion
Do not ask whether logic should be hardcoded. Ask how often it changes, who changes it, what a wrong value costs, and whether history must be reproducible - then pick the cheapest level that satisfies those answers. Stop at a named constant far more often than instinct suggests, go all the way to governed rules when policy genuinely moves, and whenever you do externalize, rebuild the validation, tests, review and history that leaving the code gave up. The goal was never configurability. It was changing policy safely, which is also why the result should be able to explain what it did and why.