In the source code I'm evaluating (jarjar), there exists java code that can be used like this:

JarJarTask fixture = new JarJarTask();
fixture.addConfiguredRule(new Rule());

Which will throw an exception like:

The <rule> element requires both "pattern" and "result" attributes.

Now, to me it is clear that the API is less than optimal here - if the rule element requires these attributes, it should ask for them in the constructor (or provide a Builder or similar that would require them). But I was unable to find coding conventions or java programming recommendations to agree with me.

Is there some specific name for this that I can use to look this up in research, programming literature, style guides? Which terms could I use to find some discussion on this?

Surely it's been discussed somewhere using some terms, but I seem to be unable to find anything.

I did find discussion around constructor injection as presented by Kent Beck in Smalltalk Best Practice Patterns and supported by Martin Fowler, which, according to this, makes it "immediately clear what a class requires when it is instantiated, and furthermore it is impossible to instantiate the class without passing in the field’s objects". So that's a starting point at least to find some discussion.

closed as primarily opinion-based by gnat, Bart van Ingen Schenau, user40980, Robert Harvey, user53019 Apr 9 '14 at 17:26

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.

  • such a name would be a matter of opinion – gnat Apr 9 '14 at 13:24
  • 1
    @gnat there are plenty of bad programming practices that have some canonical names to refer them to. It would be matter of opinion if there isn't any canonical term. So are you saying there isn't one? That would be an answer as well, I guess. – eis Apr 9 '14 at 13:25
  • 3
    It's the: API Named After Worse Character in Star Wars Saga Anti-Pattern. – JeffO Apr 9 '14 at 13:29

The best you're going to do here is to state what the API isn't, or what it fails to provide. Constructor vs. Setter Injection is a bit of a side journey, but the principle is sound: write your classes so that it is clear from the constructor arguments exactly what is required to create a fully-formed, functional object.

If you follow best practices in your organization, simply state that the class fails to follow the practice of describing its instantiation requirements in the constructor.

Design by Contract

  • Thank you for the answer! I read this so that there isn't, which does surprise me a bit. – eis Apr 9 '14 at 17:32
  • Many best practices don't have a name. "put your local variable declarations as close as possible to their first use" doesn't have a name, AFAIK. "Put braces on their own line" does; it's called "Allman Style." – Robert Harvey Apr 9 '14 at 17:36

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