If you don’t think it’s a good idea, there are two different methods here. If in doubt about migration, keep the old one.
But there is a way to reorganize all the code in two stages:
First change the interface from
interface Dao<ENTITY ,ID> { ENTITY findById(ID id); }
to
interface Dao<ENTITY ,ID> { default ENTITY findById(ID id) { return newFindById(id).orElse(null); } Optional<ENTITY> newFindById(ID id); }
I suppose from your question that adapting interface implementations is not a problem. Now tell your refactoring tool to embed the old, now default , findById method.
Second, rename the newFindById method to findById .
So you moved interface to
interface Dao<ENTITY ,ID> { Optional<ENTITY> findById(ID id); }
whereas all call sites have been changed from:
Person maybePerson = personDao.findById(id);
to
Person maybePerson = personDao.findById(id).orElse(null);
This way you have a clean interface , while other code is adapted to work as before. You can then go through the call sites one by one and decide how and how to change them. This may take some time, but since the interface already clean and the naming convention problem is resolved, there is no need to rush.
Note that your sample method should look something like this:
public void changeAge(final Long id,final int age) { personDao.findById(id).ifPresent(person -> person.setAge(age)); }
Note that in both forms, in the old updated code and the new code, there is no need to specify a variable of type Optional , therefore there is no need for a naming convention.
Refactoring requires a tool that supports Java 8.