Design smell: PleaseSirCanYouCallMyMethod

3

Occasionally I see methods like this:

public static void pleaseSirCanYouCallMyMethod(Thing thing, Arg1 arg1, Arg2 arg2) {
    thing.myMethod(arg1, arg2);
}

Basically the caller gives an object and some args to a method which calls the method with those args, which the caller could have done all by himself. This is usually a symptom of bad design or a need for more refactoring.

Sometimes instead of a static method, you’ll see a bunch of these in a class using delegation. Generally if class A holds a reference to B and has a bunch of methods that duplicate methods in B and just delegate to them, this is a smell that class responsibilities are not well-defined. Rather than having the caller go through A to get to B, maybe he should just be calling B directly. Or there should be a common interface or base class. Or maybe the classes should merge.

In general, if you see methods that do nothing but forward a call to another method, then that method is typically adding little to no value.

Comments

3 Responses to “Design smell: PleaseSirCanYouCallMyMethod”
  1. Jim Standley says:

    Be careful that you don’t throw Double Dispatch out with the bath water. Good DD design can look a lot like what you showed.

    http://en.wikipedia.org/wiki/Double_dispatch

  2. Alex says:

    Good point! In the case I mentioned and that I’ve seen, it was not an example of double dispatch.

    Double dispatch uses method calls like this to pin down which version of myMethod is being called based on knowing the type of this in the caller. So, the telling indicator that double dispatch was being used would be if the call to myMethod was passing “this” as one of the args.

Trackbacks

Check out what others are saying about this post...
  1. [...] Design smell: PleaseSirCanYouCallMyMethod- Gotta love the code smells– this is a good one too. [...]



Speak Your Mind

Tell us what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!