Don't make methods that return null and don't make methods that accept null, unless they are private.
Setters are bad most of the time: Don't write code that constructs objects by incrementally updating their state after instantiation unless it's the only way to solve the problem. Most of the time, you can construct objects correctly the first time, and actually reason about correctness of code just by looking at what objects are rather than what they might be.
Streams are good.
Javadoc is amazing, document everything that is not private.
Local classes are a good way to shelter public/package/even private namespaces from clutter that is only used within a method
Don't write code that constructs objects by incrementally updating their state after instantiation unless it's the only way to solve the problem. Most of the time, you can construct objects correctly the first time, and actually reason about correctness of code just by looking at what objects are rather than what they might be.
Oh god thank you! I'm currently fighting a just crusade in my project because people didn't wirte constructors. Instead they either do it incrementally or worse: static factory methods INSIDE the class which they create an object of... I'm either missing the genious of this because >I< am to stupid or this is the most stupid thing I've seen in 15+ years.
Honestly, for 80% of classes, it doesn't really matter.
I see the point for more complex classes, but the wide range between POJO/Record and "that one central class that handles 90% of the business" has a lot of cases where the constructor simply initializes a handful of members (any many don't even do that).
79
u/Admirable-Avocado888 Apr 14 '23
Don't make methods that return null and don't make methods that accept null, unless they are private.
Setters are bad most of the time: Don't write code that constructs objects by incrementally updating their state after instantiation unless it's the only way to solve the problem. Most of the time, you can construct objects correctly the first time, and actually reason about correctness of code just by looking at what objects are rather than what they might be.
Streams are good.
Javadoc is amazing, document everything that is not private.
Local classes are a good way to shelter public/package/even private namespaces from clutter that is only used within a method