mardi 30 mai 2017

Is having Constants in Interfaces for boolean parameters of Interface methods a good practice or code smell?

So I have the following Interface:

public interface RoleService {
    boolean INCLUDE_DEACTIVE_OBJECTS = true;
    boolean EXCLUDE_DEACTIVE_OBJECTS = false;
    Set<? extends BaseBusinessObject> getOwnedBusinessObjectsOf(final Employee employee, final boolean includeDeactiveObjects);
}

and somewhere in an upper layer, an example usage is as follows..

if (someCondition) {
    ownedBusinessObjects = roleService.getOwnedBusinessObjectsOf(employee, RoleService.INCLUDE_DEACTIVE_OBJECTS);
} else {
    ownedBusinessObjects = roleService.getOwnedBusinessObjectsOf(employee, RoleService.EXCLUDE_DEACTIVE_OBJECTS);
}

So instead of passing values such as true (or false), I believe it is much easier to read the method call when I say INCLUDE_DEACTIVE_OBJECTS.

But I am not sure, is this just plain stupid? Is this an anti-pattern or a code smell or some sort of violation of a best practice?

I think this resembles avoiding Magic Numbers somehow, but is it as useful or is it rather confusing?

Aucun commentaire:

Enregistrer un commentaire