所以我有以下接口:在接口方法的布爾參數的接口中有常量是一種好習慣或代碼異味?
public interface RoleService {
boolean INCLUDE_DEACTIVE_OBJECTS = true;
boolean EXCLUDE_DEACTIVE_OBJECTS = false;
Set<? extends BaseBusinessObject> getOwnedBusinessObjectsOf(final Employee employee, final boolean includeDeactiveObjects);
}
在上層
和某處,示例用法如下..
if (someCondition) {
ownedBusinessObjects = roleService.getOwnedBusinessObjectsOf(employee, RoleService.INCLUDE_DEACTIVE_OBJECTS);
} else {
ownedBusinessObjects = roleService.getOwnedBusinessObjectsOf(employee, RoleService.EXCLUDE_DEACTIVE_OBJECTS);
}
因此,而不是傳遞值如true
(或false
) ,我相信當我說INCLUDE_DEACTIVE_OBJECTS
時,閱讀方法調用要容易得多。
但我不確定,這是否簡直愚蠢?這是一種反模式還是代碼味道或某種違反最佳做法的行爲?
我認爲這有點類似於避免Magic Numbers,但它是有用的,還是它相當混亂?
「語言不可知」標籤的榮譽。目前在設計問題上很少見到這種情況。 – niksofteng