Coding Standards and Best Practices for Robust Software Development
This article presents a set of coding "military rules" covering topics such as avoiding magic numbers, limiting method parameters, proper resource release, specific exception handling, and precise arithmetic, followed by practical development efficiency tips and resource links for further learning.
Introduction: The article discusses the importance of coding standards in large IT companies like Huawei and presents a set of "military rules" for writing robust, maintainable code.
Rule 1: Avoid magic numbers; use meaningful constants.
Rule 2: Each method should have a single responsibility.
Rule 3: Limit method parameters to five.
Rule 4: Do not return null; use exceptions, special‑case objects, or empty collections/arrays.
Rule 5: Ensure resources such as database connections and I/O streams are released in a finally block. Example code:
Connection jdbcConnection = null;
Statement stmt = null;
try {
// ...
} catch (SQLException e) {
// ...
} finally {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
logger.log(Level.WARNING, "异常说明", e);
}
}
if (jdbcConnection != null) {
try {
jdbcConnection.close();
} catch (SQLException e) {
logger.log(Level.WARNING, "异常说明", e);
}
}
}Rule 6: Do not catch generic Exception; handle specific exceptions.
Rule 7: Include an else branch for if‑else if chains and a default case for switch statements to avoid missing branches.
Rule 8: Override hashCode() when overriding equals().
Rule 9: Avoid creating new threads inside loops; use thread pools.
Rule 10: Use BigDecimal or integer arithmetic for precise calculations such as currency.
Additional advice: commit small changes frequently, use meaningful naming, avoid over‑design, keep web applications stateless, prefer logging over breakpoints, limit function size, add concise comments, encourage cross‑review, and communicate promptly when issues arise.
The article concludes with invitations to join a community and provides links to sample SpringBoot projects for further study.
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.
Java Architect Essentials
Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow together.
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.
