Remove all redundant empty lines, be careful :)
Your code should be easy to read. Move all hardcoded values
to constant fields and give them informative names.
For example: constants can be defaultCapacity
, loadFactor
...
We have access to private fields of inner class, so using getters or setters is redundant.
Class Node
should take key and value, so we should parameterized it to prevent problems with data types.
If the method has only a utility purpose and is used only inside the same class, it should not be
public
. Keep your code as close as possible to follow the encapsulation principle.
All fields and methods should have access modifiers. Remember about the encapsulation principle.
Be attentive with the diamond operator in java.
What is the difference between: new Node<>(...)
and new Node<K,V>(...)
and new Node(...)
.
Read more about it here
Writing proper variable names can be a highly valuable investment to the quality of your code.
Not only you and your teammates understand your code better, but it can also improve code sustainability in the future later on.
When you go back to the same code base and re-read it over again, you should understand what is going on.
Do not use abstract words like string
or array
as variable name. Do not use one-letter names. The name of the method should make it clear what it does.
- Bad example:
String[] arr = new String[]{"Alex", "Bob", "Alice"}; for (String s : arr) { System.out.println(s); }
- Refactored code:
String[] usernames = new String[]{"Alex", "Bob", "Alice"}; for (String username : usernames) { System.out.println(username); }