跪拜 Guibai
← All articles
Backend · Java · Programmer

Write Methods That Read Like a Business Flowchart

By SamDeepThinking ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Codebases that mix abstraction levels force every maintainer to reverse-engineer business rules from raw implementation details, which is where regressions breed. Structuring code as a flat list of same-level steps cuts onboarding time and makes surgical changes safe.

Summary

A real-world order-submission interface shows how decomposing a process into small, same-level methods creates code that reads like a business narrative. The top-level method lists five steps — validate input, calculate push time, create the order, trigger OA approval for overtime orders, and publish a domain event — without burying the reader in detail. Each step drills down into its own same-style method, such as order creation splitting into creating the main order and then splitting it by supplier.

This uniform abstraction level means a developer adding a new validation only touches the check-params method; the rest of the flow stays untouched. The payoff is a lower defect rate during modifications because each method owns a single responsibility and sits at the same conceptual altitude.

The contrast is the all-in-one monster method that mixes validation, business rules, persistence, and side effects into an unreadable block. Code structured as a series of named steps maps directly to the use case, which is the definition of maintainable business logic.

Takeaways
Decompose a top-level business method into a sequence of named private methods, each representing one step in the use case.
Keep every method call inside a given method at the same level of abstraction; don't mix high-level orchestration with low-level data manipulation.
A reader should grasp the core business flow from the top-level method alone, without clicking into any implementation.
Adding a new validation or step becomes a single-method change because responsibilities are isolated.
Deeply understanding the business process is a prerequisite — the code structure simply mirrors that understanding.
Conclusions

The technique is less about reusability and more about cognitive load: a flat list of same-level steps lets a developer hold the entire flow in working memory.

Calling this 'beautiful code' is not an aesthetic judgement — it is a practical claim that defect rates drop when changes are confined to one method.

The discipline of uniform abstraction is harder than it looks; it requires the writer to know which details belong one level down and to resist inlining them.

Many teams skip this because extracting methods feels like ceremony under deadline pressure, but the cost is paid later in every debugging session and regression.

Concepts & terms
Same level of abstraction
A coding principle where every statement inside a method operates at the same conceptual distance from the business domain — you don't mix high-level orchestration calls with low-level string formatting or database row manipulation.
Domain event
A record of something that happened in the business domain (e.g., an order was created) that other parts of the system can react to, often published via an event bus to decouple side effects from the main flow.
OA approval process
Office Automation approval workflow, common in Chinese enterprises, where a business action like an overtime order triggers a chain of managerial sign-offs before it can proceed.
From the discussion

The sole contribution is a code snippet that chains list partitioning with a flat-mapped stream call, offered as a personal favorite. No debate or competing views emerge.

Batching a large ID list and flattening the results into a single collection can be expressed concisely with Guava's Lists.partition and Java streams.
Featured comments
tltwuyulw

I really like this code I wrote. var allResults =Lists.partition(idList, batchMax).stream().flatMap(s->getDataByIds(s).stream()).toList();

See top comments, translated →
Source: juejin.cn ↗ Google Translate ↗ Backup ↗