Should I return CompletableFuture or Future when defining API?
In Java 8, is it better for interface or abstract class to define APIs returning CompletableFuture
instead of returning Future
? Considering it is ugly converting Future
to CompletableFuture
and the fact that CompletableFuture
will give the caller more flexibility of using functional style directly, what could be a good reason for an API to just return Future
?
My 2 cts:
So you should probably assess the likelihood that you will want to return something other than a CompletableFuture in the future (haha) and decide accordingly.
Thought I would come back to this and provide some updates on my final decisions:
For my own code/design, I went with using CompletableFuture
as the return type, because
protected abstract
method of an internal part that I want to make extensible; CompletableFuture
is an added benefit/reminder/encouragement of using functional style to the future devs. With that being said, I would definitely use the CompletableStage
interface as the return type, had I been designing a public API, because:
CompletableStage
has a method CompletableFuture<T> toCompletableFuture()
.