Posts

Showing posts from September, 2014

Is accepting Optional as argument a code smell?

I was reviewing some piece of code and came across a function that was taking Optional  as argument. In the same class, another function was taking a couple of Optional as arguments. When I thought about it, I felt that taking an Optional should be avoided. You can return an Optional from a function, but avoid taking Optional arguments. Before calling a function, you should check for the presence or absence of the values that you are passing. Hence a function taking one or more Optional is a code smell. One argument that can possibly presented in favor of taking Optional as argument is every caller checking for the presence of the arguments. Consider the code example below: If you pay attention, the error handling from the caller's point of view is ugly if the caller wants to report correct error. Much worse, the error check is done after using the values. A side effect of product() taking Optional is that it must return an Optional. Otherwise it has to throw an