Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Throw exception in interface default method

Recently I came across this code.

public interface CustomerQueryService {    
    default Customer getCustomerById(long id) {
        throw new NotImplementedException();
    }
}

Later, it turned out that it's general convention for this project. Is it considered a good practice?

like image 800
k13i Avatar asked Dec 11 '22 12:12

k13i


2 Answers

One could starte here to see what Brian Goetz, one of the "acting fathers" of Java has to say about "how to use default methods".

So, according "to the books"; one could use them like in your example: to throw an exception for a method regarded "optional". But that would mean: you have already some methods; and you are adding new ones.

The main purpose of adding default methods to the Java language was to allow for non-breaking enhancements of existing interfaces. They were not meant to be used as some sort of "mixin" / multi-inheritance / traits-like providing construct.

But beyond that: in your example, you have a new interface ... that only has that one method. I think this does not fall under those "intended usages".

On the other hand; don't be too much "about the books". When a whole team of people agrees "this is what we do", and everybody understands and buys into that; why not?!

But there is one caveat ... my C++ coworkers have a strict policy: they allow for exactly one implementation of any abstract method within an inheritance tree; as it is very hard to debug a problem when you are looking at the wrong implementation of some method. And now that we can inherit default methods in Java, too ... debugging problems could become harder in Java for us in the same way. So be careful how such things are used!

Long story short: it is a good practice if the big majority of your development team finds it a helpful practice. If not, it is not.

like image 159
GhostCat Avatar answered Dec 26 '22 04:12

GhostCat


I'd say it's bad because of the exception type and the reports that it's a convention in the project.

NotImplementedException:

NotImplementedException represents the case where the author has yet to implement the logic at this point in the program. This can act as an exception based TODO tag.

So this is a lazy way to provide an implementation for a method across all CustomerQueryService just to find out at runtime that you've yet to write it.

Note UnsupportedOperationException might be an acceptable one in some cases, but neither would be a good "convention" for a project.

like image 39
weston Avatar answered Dec 26 '22 06:12

weston