private void doStuff(){}


private void doStuff()

Where to put common code ?

Shall I create a new service, but then what interface will it implement ?

Or shall I just create a utility class ? Please guide on what is the best approrach.

  • If it is a non-static void method, it probably relies on some functionality within the class. Beware if you can successfully abstract this information before you try to refactor your method. Just a similar name may not infer an identical implementation. I mean, there are a lot of implementations of toString(). – Maarten Bodewes Nov 14 '14 at 14:16
  • @owlstead the doStuff methods are exactly identical. – coding_idiot Nov 14 '14 at 21:27

Well it actually depends on the stuff. If the stuff is related to the business logic of the application and inside has to do with domain objects then it has to go in another class and be aggregated in the services or depending on domain in a superclass of both services. For example both services need to check if a user already exists.

On the other hand if that method has nothing to do with business logic, it could go in a utility class. For example suppose it has to poll a given function, calling it multiple time until response is not null.

  • superclass of both services, seems like a nice idea. Thanks. – coding_idiot Nov 14 '14 at 21:28
  • @coding_idiot Only go that way if the classes are naturally of the same type. You could also go for composition and let both classes use another class that contains the method. – Maarten Bodewes Nov 14 '14 at 21:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.