Does / why does Java need to have void
methods? Reference:
Any method declared void doesn't return a value.
As far as I can think, every use of void
would be better served by returning a status flag, the object being invoked, or null
.
This would make every call a statement that is assignable, and would facilitate builder patterns and method chaining. Methods that are only invoked for their effects would usually return a boolean or a generic Success
type or throw an exception on failure.
Best Answer
Because there is a a difference between "This function can succeed or fail and is self-aware enough that it can tell the difference" and "There is no feedback about the effect of this function." Without
void
, you'd endlessly check success codes and believe that you are writing robust software, when in fact you are doing nothing of the sort.