How Mapping Business Logic to a Code Tree Improves Readability and Reduces Bugs
The article explains why treating business requirements as a logical tree and directly mapping that structure to code—using clear comments, proper function extraction, and avoiding premature design patterns—leads to more maintainable, bug‑free software.
Business Logic and Code Representation
Requirement documents and source code are two representations of the same business logic. If the underlying logic is incorrect, any representation will be wrong. Therefore developers should clarify the logic before writing code.
Code Reviews Reveal Logic Problems
Many chaotic codebases stem from unclear or misunderstood business logic rather than a lack of coding skill. Clarifying the logic first provides a solid foundation for clean code.
Example Business Requirement
Function doSomething must perform two main steps, A and B, each with ordered sub‑steps:
Step A: a1 → a2 → a3
Step B: b1 → b2
The logical structure can be visualized as a tree with a root, branches, and leaves.
Correct Code Implementation
Start with a single function that follows the logical order, then refactor into smaller functions that mirror the tree hierarchy.
void doSomething()
{
// A
a1; // a1 logic
a2; // a2 logic
a3; // a3 logic
// B
b1; // b1 logic
b2; // b2 logic
}Refactored version that maps the logical tree to a code tree:
void doSomething()
{
doA();
doB();
}
void doA()
{
a1; // a1 logic
a2; // a2 logic
a3; // a3 logic
}
void doB()
{
b1; // b1 logic
b2; // b2 logic
}Common Pitfalls
Unequal Extraction – extracting only part of the logic (e.g., extracting B but leaving A inline) creates mismatched responsibilities.
void doSomething()
{
a1; a2; a3; // A inline
doB(); // B extracted
}
void doB()
{
b1; b2;
}Partial Extraction – extracting a subset of a logical block makes naming ambiguous and harms readability.
void doSomething()
{
doA(); // extracts a1 and a2 only
a3; // a3 remains inline
doB();
}
void doA()
{
a1; a2;
}Wrong Extraction – mixing unrelated logic into a single extracted function breaks the correspondence between code and business logic.
void doSomething()
{
doMixed(); // contains parts of A and B
}
void doMixed()
{
a1; b1; // unrelated steps combined
a2; b2;
}Conclusion
Before extracting small functions or applying design patterns, write straightforward code that directly follows a well‑defined logical tree. Once the foundation is solid, more advanced techniques can be introduced to address specific pain points.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
