Comment Re:The third option (Score 1) 536
- A method should do one thing, and be named for exactly what it does or returns.
- If the method is unable to successfully perform the one thing it's supposed to do, it must throw an exception.
- Returning null is not an acceptable course for error situations within a method. Null values indicate non-existence of the return data the caller is seeking, not an error.
- It is the caller's responsibility to determine whether it can safely continue if a method cannot complete its task successfully. If not, it must throw the exception upward, as it then cannot complete its own task successfully. This includes the entire program itself.
- For methods that return a Collection, a null value should generally never be returned. The caller is seeking a Collection of something, so it's either empty or it's not.
Nothing profound here, but putting these together and articulating them has helped our staff to write better, more reliable code.