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.
In the absence of named parameters, static builders allow more expressive means of passing parameters at construction.
For example, if a constructor takes more than a handful of ints, then it's easy to confuse what each one is for. Default/min/max pool sizes, initial delay, repeat interval, timeout delay, etc.
Also, when the construction of an object may need to involve more complex logic it may be better to pass a builder around to store state needed to construct that object.
I'd still prefer a constructor generally, but there are occasions when a builder can be useful.
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