Chapter 149: Clean Code in Java
Clean code is code others (and future you) can read, test, and change without fear. Java culture favors explicit types and packages—clarity comes from naming, short methods, consistent formatting, and tests that unlock refactors.
1. Topic title
Clean code: minimize cognitive load per line; let structure carry intent
2. What it means
Names — findActiveUsersByTenant beats getData. Booleans read well as isExpired, hasAccess.
Functions — one main idea; early returns flatten nesting; avoid flag parameters that mean two different behaviors.
Comments — explain why, not what the next line already says—delete noise comments.
3. Why it is used
Team velocity—onboarding speed tracks code legibility.
Fewer defects—simple paths host fewer bugs.
4. Mental sketch
Messy code is a hoarder’s garage—everything might be useful someday. Clean code is a workbench with labeled drawers—you grab the right tool first try.
5. Java code example
public final class Pricing {
private Pricing() {
}
public static int discountPercent(int age, boolean member) {
if (age < 0) {
throw new IllegalArgumentException("age");
}
if (member && age >= 65) {
return 20;
}
if (member) {
return 10;
}
return 0;
}
}Small pure functions invite table-driven tests without mocks.
6. Explanation of code
Guard clause on age fails fast—callers get clear exceptions.
7. Common mistakes
Clever one-liners that save lines but cost minutes each read.
God classes mixing HTTP, SQL, and email—layers exist to bound change.
8. Best practices
Boy Scout rule—leave touched files slightly cleaner than you found them.
Pair readable code with fast tests—confidence enables deletion.
9. Small practice task
Pick a 50-line method from your project and extract two named private methods—run tests—ship if green.
Beginner tip
IDE warnings (unused, raw types) are cheap mentors—zero-warning policy in modules you touch often.