ProLesson 12.2Intermediate3 min readUpdated 30 October 2026
Writing clean Java: naming, small methods, immutability
Habits that make Java code easy to read, change and test, shown with before-and-after refactoring of a real-looking method.
Code is read far more often than it's written. Interviewers notice clean code in machine-coding rounds, and teammates notice it every day. Here are the habits that matter most, shown on one messy method.
01Before
java
public double calc(List<Object[]> l, String t) {
double r = 0;
for (int i = 0; i < l.size(); i++) {
Object[] o = l.get(i);
if ((boolean) o[2] == true) {
if (t.equals("GOLD")) {
r = r + (double) o[1] * 0.8;
} else if (t.equals("SILVER")) {
r = r + (double) o[1] * 0.9;
} else {
r = r + (double) o[1];
}
}
}
return r;
}It works, but you need to think hard to see what it does, and adding a tier means editing the loop.
Loading the rest of the lesson…