有哪些流行的方法可以命名选项类型变量和返回选项类型的方法,以便将它们与非选项对应物区分开来?
假设DAO当前有一个findById
返回实体实例或null的方法,如果我们弃用该方法并添加一个返回选项类型的方法,我们应该如何命名它?
现在假设我们正在重构代码以使用这个新方法,我们不希望用选项类型替换对实体变量的所有引用,我们应该如何命名选项类型变量?
interface Dao<ENTITY ,ID> {
@Deprecated
ENTITY findById(ID id);
//What naming convention should we use?
Optional<ENTITY> maybeFindById(ID id);
}
public class MyService {
PersonDao personDao;
public void changeAge(final Long id,final int age) {
//final Person person = personDao.findById(id);
//if(person !=null)
//What naming convention should we use?
final Optional<Person> maybePerson = personDao.maybeFindById(id);
if (maybePerson.isPresent()){
final Person person = maybePerson.get();
person.setAge(age);
}
}
Run Code Online (Sandbox Code Playgroud)