Sometimes we try to take the short cut when creating our Interfaces and methods. For example, take the IWorkspaceRoot.delete method for an example.
It takes several different parameters:
delete(boolean deleteContent, boolean force, IProjressMonitor monitor);
Most of the time this is implemented in code as the following:
workspaceRoot.delete(true, false, new NullProgressMonitor());
It’s a little difficult to understand just by looking at it without looking at the javadoc what it is doing. The more parameters we add the more possibility for mistakes and more complicated the method’s code. There is a mindset that boolean parameters should be avoided. That it is better to be more descriptive and create a few more method names:
So instead of the above we could have the following specific method names.
deleteAll(IProgressMonitor monitor);delete(IProgressMonitor monitor);forceDeleteAll(IProgressMonitor monitor);forceDelete(IProgressMonitor minitor);
Yes it is four methods now, but each serves only one purpose. The other method was doing at least four things depending on how the switches were set. I’m sure others could come up with even better method names than I have. It is important when we are designing API and interfaces to look at it from a user’s point of view. If it isn’t clear from just looking at the method and its parameters what it is doing, then the method is too complicated and doing too much.
Resources: Avoid Too Many Arguments.