Big Decimal
- Always use BigDecimal as the type instead of Double when dealing with amount
https://stackoverflow.com/questions/1359817/using-bigdecimal-to-work-with-currencies
Char Array vs String for Password
- Use Char Array instead of String to store password.
https://stackoverflow.com/questions/8881291/why-is-char-preferred-over-string-for-passwords
Object Equality:
- Always override equals and hashcode if you want to do object equality
https://www.mkyong.com/java/java-how-to-overrides-equals-and-hashcode/
https://stackoverflow.com/questions/2265503/why-do-i-need-to-override-the-equals-and-hashcode-methods-in-java
Immutability:
- Unless you have a good reason, everything should be immutable
- Always use final variables, unless you have a good reason
- No setters for state mutation, unless you have a good reason
https://jlordiales.me/2012/12/24/the-ins-and-outs-of-immutability/
Never return null:
For example, if I were to define a method in Java that returned a Collection I would typically prefer to return an empty collection (i.e.Collections.emptyList()
) rather than null
private final List<String> items;
private final Meta meta;
//Return an empty array instead of null
public List<String> getItems() {
return meta.total == 0 ? Collections.emptyList() : items;
}
//Bad practice to return null, as the calling code need to check for null - Potential NPE
public List<String> getItems() {
return meta.total == 0 ? null : items;
}
https://stackoverflow.com/questions/1274792/is-returning-null-bad-design