How to improve Java code quality
Java's quality problems tend to cluster around encapsulation and error handling — patterns that compile cleanly and pass a quick glance, but cause real maintenance pain later. Here are the four worth fixing first.
Empty catch blocks
An empty catch block compiles fine and silently discards the exception, which means a real failure produces no signal anywhere — no log line, no stack trace, nothing to search for when something downstream breaks.
Before — failure vanishes silently
try {
connection.send(payload);
} catch (IOException e) {
}
After — failure is visible
try {
connection.send(payload);
} catch (IOException e) {
logger.error("send failed", e);
}
Public fields
A public field lets any caller mutate it directly, with no chance to validate the new value or react to the change — the entire point of encapsulation is to prevent exactly this.
Before — anyone can set a negative balance
public class Account {
public double balance;
}
After — validated on the way in
public class Account {
private double balance;
public void setBalance(double value) {
if (value < 0) throw new IllegalArgumentException();
this.balance = value;
}
}
printStackTrace() in production paths
printStackTrace() writes straight to stderr, which is easy to lose entirely in a production environment with real log aggregation — a proper logger routes the same information somewhere it can actually be searched later.
Before
catch (Exception e) {
e.printStackTrace();
}
After
catch (Exception e) {
logger.error("operation failed", e);
}
Raw generic types
Using a generic type without its type parameter (a "raw type") throws away the compile-time type checking generics exist to provide, pushing what should be a compile error into a runtime ClassCastException instead.
Before — no compile-time safety
List names = new ArrayList();
names.add("Ada");
After
List<String> names = new ArrayList<>();
names.add("Ada");
How REPO-SIGHT checks for these
These four are REPO-SIGHT's actual Java rules — java-empty-catch-block, java-public-field, java-printstacktrace, and java-raw-type-usage — alongside long-method, deep-nesting, and TODO-without-ticket checks. Run it on a Java repository to see which of these show up in yours.