Thursday, 11 August 2011

Is ‘Convention Over Configuration’ Going Too Far?

I’m all for convention over configuration, especially in frameworks such as Spring and I really like the idea that the Spring XML configuration file can boil down to one line:

<mvc:annotation-driven />

...and there’s even argument for suggestion that if the XML config file is missing from your project then Spring should assume that your application is annotation driven and attempt running - though I’m not too naive to assume that more thought would be needed here as this may have other ramifications.

But, I occasionally wonder whether it’s being taken too far with yet more and more convention rules to remember. As an example of this, consider the Model interface, you know the one: Spring passes a model object to your Controller’s handler method, you bung in some attributes and let the JSP get hold of them when your next view is being displayed. The Model has two methods for adding a simple attribute:

    model.addAttribute("myBean", new MyBean());
    model.addAttribute
(new MyBean());

In the above code snippet, both of these methods do exactly the same thing: they add an attribute to the model’s map with a “myBean” key. The first method uses explicit naming whilst the second figures out some name using the Conventions class:

    String name = Conventions.getVariableName(new MyBean());

...which means that more code needs to run to add a simple value to a map. I’d also argue that the first, explicit, two argument addAttribute(...) is simpler to read and understand than the the second, single argument method. In the first case, you know the name of the attribute in the HTTP request , whereas in the second it’s assumed. However, the Guys at Spring haven’t fallen down on the job, the addAttribute(Object attributeValue) is fully documented with a link to the Conventions class and a mention that it uses the ‘JavaBeans property naming rules’ (which are buried for all to see on Page 57, Section 8.8 of the Sun JavaBeans Specification).

So, I guess the point is that we should think about the effects of adding Conventions to your code before you do it as we may be adding complexity. This leads back to a little psychology: programmers (including myself) like to look clever, we get kudos and cash for being clever, so I guess that we should remember that other important rule of programming: KISS.

2 comments:

Macluq said...

I just discover your blog, I'm loving it. Keep on the nice work.

And about the matter in question, I totally agree. Sometimes frameworks can overuse conventions in order to "make developer's life easier". I wonder what it'd be to make his life harder in some cases...

Black Hand said...

this is one of the reasons because i prefer Django over RoR in web development. RoR is too much convention over configuration oriented. Django try to maintain one of the points in the Python Philosophy "explicit over implicit is better" and have the correct balance at least to me in convention and configuration.