Fundamentals 13 min read

Writing Well‑Designed Code: Ensuring Logical Correctness, Reducing Complexity, and Decoupling Business Logic

The article shares practical experiences on writing high‑quality code by emphasizing logical correctness, the necessity of handling the else branch, techniques for lowering logical complexity, and strategies for decoupling business logic from implementation, all illustrated with clear Java examples.

Java Captain
Java Captain
Java Captain
Writing Well‑Designed Code: Ensuring Logical Correctness, Reducing Complexity, and Decoupling Business Logic

Over the past few years the author has written less code but finds that each piece requires more thoughtful design; good code is rare, and many developers overlook the deeper logical considerations behind simple constructs like if and while.

Logical correctness is more than just using the right keywords; it demands that developers ask what should happen when a condition fails, whether the missing case can be recovered, and how to avoid silently ignoring errors. The author illustrates this with a simple coupon‑issuing snippet that only checks userInfo != null and proceeds, ignoring the scenario where userInfo is null.

In a more robust version, the code first attempts to fetch missing user information, then throws a RuntimeException if the user still does not exist, demonstrating the importance of handling the else path and not hiding failures.

Key advice: Be brave enough to let the program throw errors; hiding them only multiplies problems.

The author stresses that every conditional should consider the opposite case. Avoid lone if statements; use else if chains and a final else (or a default in switch) to cover unexpected situations, ensuring complete logical coverage.

To reduce logical complexity, the author shows how to replace multiple branches with a more compact expression. Instead of several if/else blocks, the following single line captures the same intent:

userInfo = withDefault(userInfo, fetchUserInfo(userId));
Assert.assertNotNull(userInfo, "userInfo not exist.");
couponing(userInfo, 100);

The principle is to minimize branching; each branch should represent a clear business decision, not a workaround for error handling.

Key advice: Reducing the number of branches directly reduces code complexity.

When business rules become more intricate—e.g., coupons may be granted to VIP users, whitelist users, or users whose IDs end with "00"—the author introduces a helper method any(...) that returns true if any supplied condition is true, thereby collapsing multiple || checks into a single, readable call.

if (any(userInfo.isVip(), inWhiteNameList(userInfo), StringUtil.endWith(userInfo.getUserId(), "00"))) {
    couponing(userInfo, 100);
} else {
    return;
}

This approach lowers the combinatorial explosion of branch paths while keeping the focus on the business outcome (granting a coupon) versus the myriad of condition permutations.

Finally, the article discusses decoupling business capability from implementation. By defining a capability such as canCouponing that operates on a generic runtimeContext rather than concrete objects, the code becomes more stable and easier to replace with rule‑engine logic or expert‑system configurations.

if (canCouponing(runtimeContext)) {
    couponing(userInfo, 100);
} else {
    return;
}

This separation encourages moving business rules out of hard‑coded logic, allowing non‑developers to manage them via rule engines, and keeping the functional code clean.

Overall, the article provides a concise guide to writing maintainable, logically sound code by thinking beyond immediate requirements, handling all conditional branches, reducing unnecessary branching, and abstracting business rules from implementation.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Code reviewsoftware designcomplexity reductionlogic correctness
Java Captain
Written by

Java Captain

Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.