Are there any hard and fast rules regarding returning boolean in a method signature to indicate a successful operation as opposed to declaring void? I find that for more critical operations in my calling method I want to know if an operation completed so I can log any issues. Is this an “inappropriate” use of boolean?
Are there any hard and fast rules regarding returning boolean in a method signature
Share
Usually I use
Exceptions to signal when something went wrong.Instead of returning
false, you canthrowanExceptionwith a detailed message about what the problem was.Returning
falsedoesn’t give you much information about the problem.Then, instead of checking for a
falsereturn value, just put the method call intry/catchif you expect that the method could easily fail.Many people will complain that this method is slower. But, the benefits you gain greatly outweigh the slowdown. Besides, if you are using Java speed shouldn’t be your #1 concern.