Bad naming:
BufferedReader br = new BufferedReader(new FileReader(fileName))
Good naming:
BufferedReader bufferedReader = new BufferedReader(new FileReader(fileName))
If you have strange strings or numbers in the code it's better to declare them as constants. The name of the constant should display this object's purpose.
Bad practice:
public boolean startWithLetter(String word) {
return word.startsWith("d"); // why do we use 'd' here???
}
Good practice:
private static final String SPECIFIED_CHARACTER = "d";
public boolean startWithLetter(String word) {
return word.startsWith(SPECIFIED_CHARACTER);
}
Connections, streams, files, and other classes that implement the Closeable
or AutoCloseable
interface, need to be closed after use. Furthermore, that close should be done in a finally
block. Preferably, when the class implements AutoCloseable
, the resource should be created using the "try-with-resources" pattern and will be closed automatically.
Leaving an empty catch block or e.printStackTrace
here is a bad practice.
Better re-throw RuntimeException
with the original exception in the parameters:
catch (Exception e) {
throw new RuntimeException(e);
}
Let's make your code simple and easy to read. So better avoid using redundant variables.