or any other small method. It is needed in two classes. Should I create it in both classes? Or should I make an interface just for this one method? Isnt it form over substance? What is the rule in such cases?
There's no hard rule, but in general if you find yourself repeating code, even if it's just twice, you will want to extract the code and make it callable from the original locations.
Where to put the method depends on what the relationship between the classes where it is used is. Most of the times the classes are in the same package, and you can put a so called 'Utility class' in the same package that contains static helper methods.
Speaking of which, you aught to make your methods static if they don't access instance members and don't override a base method.
...and utility class methods are usually all static.
That return statement looks complicated. What does that utility method do that you can't do withString#isBlank()orString#isEmpty()? Do you really want an Optional[""]? What is going to happen if you pass null to that method?
I'm thinking about a new battle cry. Maybe "Not in the face! Not in the face!" Any thoughts tiny ad?