Best Practices for Writing Clean and Maintainable Code
This article summarizes essential clean‑code practices—including commenting, naming, method design, exception handling, concurrency, unit testing, and overall code structure—to help engineers write elegant, readable, and maintainable software.
Writing elegant, readable, and maintainable code is a crucial skill for software engineers. The article recommends three classic books and distills key practices across several dimensions.
1. Comments
Prefer good names over comments; a good name reduces the need for comments.
Avoid "crutch" comments; good code is better than bad code plus good comments.
Use file‑level or class‑level comments to explain overall behavior.
Always comment constants.
Adopt team‑wide tags such as TODO, FIXME, and HACK.
Provide clear input/output examples in comments.
Comments should state intent, not obvious details.
Do not embed copyright or version info in code; version control handles that.
Avoid HTML comments in source code.
Comments must describe the nearest code and stay in sync.
Public APIs need comments; use them sparingly elsewhere.
Bad comments include irrelevant, outdated, redundant, or commented‑out code.
The best comment is the one you don’t need to write—avoid boilerplate, setter/getter, and log‑style comments.
2. Naming
Use standard naming conventions and expressive terms (e.g., fetch instead of get).
Avoid vague names like tmp and include useful details such as units (e.g., timeoutMs).
Match name length to scope: longer names for wide scope, shorter for limited scope.
Boolean variables should start with is, has, can, or should.
Function names should reveal side effects and purpose.
3. Methods
Keep functions short (ideally ≤20 lines, never 100 lines).
Control structures should contain a single statement (usually a function call).
Limit nesting to two levels.
Each function should do one thing and not abstract another function.
Public functions should call private helpers immediately after.
Prefer zero parameters; if more are needed, limit to three and avoid boolean flags.
Do not return null; throw exceptions or return special objects.
Avoid passing null as arguments.
4. Exceptions and Errors
Extract try‑catch blocks into separate helper functions.
Provide sufficient context in thrown exceptions.
Do not attribute system errors to random events.
5. Concurrency
Separate concurrent code from other logic.
Strictly limit access to shared data.
Avoid multiple synchronized methods on the same object.
Keep critical sections small and minimize lock contention.
6. Unit Testing
Test method names can be long; treat them as documentation.
Focus on meaningful tests rather than chasing high coverage percentages.
Use the simplest inputs that fully exercise the code.
Name test functions descriptively (e.g., Test_ValidInput).
Treat test code as equally important as production code.
One assertion per test is ideal.
Follow the FIRST principles: Fast, Independent, Repeatable, Self‑validating, Timely (TDD).
7. Code Structure
Limit line length to 100‑120 characters; files typically 200 lines, max 500.
Place related code close together (declarations near use, callers above callees).
Express conditions positively and extract intent‑revealing functions.
Avoid inheriting constants; keep modules unaware of internal details of other objects.
Use DTOs for data‑only structures.
Expose behavior, hide data; avoid “Yoda” conditions.
Prefer if‑else over complex ternary expressions; early returns reduce nesting.
8. Design
Classes should be small and adhere to the Single Responsibility Principle.
Limit the number of fields; depend on abstractions (DIP) rather than concrete details.
Keep methods and their knowledge of variables minimal.
Reduce variable count, shrink scope, and favor constants.
Strive for the simplest workable solution; avoid over‑design.
Provide a minimal, well‑defined API.
9. Conclusion
The article provides a concise checklist of clean‑code best practices across commenting, naming, method design, testing, concurrency, and overall architecture, with plans for deeper examples in future posts.
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 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.
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.
