Clean Code Practices and Guidelines for Java Development
This article explains the concept of clean code, why clean code awareness is essential for developers, illustrates common bad‑code patterns with real Java examples, and provides practical guidelines—including formatting, development standards, code‑review rules, Maven conventions, Lombok usage, and refactoring strategies—to help teams write maintainable, readable, and efficient code.
Author Xu Haoran, a senior engineer from Qunar's domestic flight team, shares his experience on clean code—code that looks tidy, has clear logic, and is easily extensible. Clean code aims to reduce the "WTF" feeling when developers inherit or maintain existing systems.
Why clean code matters: Poorly written code leads to frustration, increased debugging time, and higher maintenance costs. Even well‑structured code can contain minor issues; the goal is to strive toward zero WTF moments.
Typical bad‑code examples:
Example 1 – an overly large class with duplicated code and meaningless comments:
public void logPlatInter(String paramStr) {
if (CommonConfig.isOnline()) {
//线上
this.logInfoFilter(paramStr);
} else {
//线下
this.logInfoObj(paramStr);
}
}Example 2 – a short method that hides its purpose behind vague naming and redundant comments.
Example 3 – a massive switch‑case block spanning hundreds of lines, making the logic hard to understand and test.
private static PriceTag compute(PriceType type, int price, int viewPrice, int specialOfferProfite, int insurancePrice, BigDecimal insurancePromotionAmount) {
// ... many case branches ...
default:
break;
}
return result;
}How to achieve clean code:
Code formatting: Use a unified formatter (e.g., GoogleStyle‑qunar1_0.xml) and enforce formatting before each push.
Development standards: Follow established style guides such as Alibaba Java Development Manual or Google Java Style Guide, and use IDE plugins to automatically detect violations.
Code‑review (CR) standards: Check for duplicated code, overly long methods (≈50 lines), unnecessary comments or main methods, proper access modifiers, and resolve IDE warnings (the “yellow poop”).
Dependency management: Keep Maven POM files clean—use dependencyManagement for versions, avoid copy‑pasting whole POMs, and document third‑party dependencies.
Use stable libraries: Leverage Apache Commons, Guava, and Lombok to reduce boilerplate code.
Lombok: Generate getters, setters, toString, equals, and hashCode automatically, reducing repetitive code.
Conclusion: Clean code is a shared goal across teams; by establishing formatting rules, adhering to style guides, enforcing strict code‑review checks, and using modern tooling, developers can continuously improve code quality, reduce maintenance overhead, and enjoy happier coding experiences.
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.
Qunar Tech Salon
Qunar Tech Salon is a learning and exchange platform for Qunar engineers and industry peers. We share cutting-edge technology trends and topics, providing a free platform for mid-to-senior technical professionals to exchange and learn.
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.
