Forum Controls
Spotlight Features

The Rich Engineering Heritage Behind Dependency Injection

Andrew McVeigh takes us on a tour of the rich heritage behind dependency injection, what it represents, and tells us why its here to stay.

NetBeans 6: Matisse Updates

NetBeans 6 delivers great updates to the Matisse GUI builder. Spend a few minutes with Roman Strobl and get an expert briefing on what's new and what has changed.

Introduction to Groovy Part 3

In this, the third and final installation of Andres' Introduction to Groovy series, you learn about how Groovy handles variable numbers of arguments, named parameters, currying, and more about Groovy operators. Including, some new operators.

Easier Custom Components with Swing Fuse

Swing Fuse (actually just Fuse), is a framework designed to make it easier to create your own custom desktop components. In this article, Daniel Spiewak shows you how to get started and provides sample source code you can download.

Benchmark Analysis: Guice vs Spring

Willam Louth shows how he uses JXInsight Probes to investigate probable performance issues with code bases that he is not familiar with. He also highlights possible pitfalls in creating a benchmark, as well as in the analysis of results.
Replies: 3 - Pages: 1  
Threads: [ Previous | Next ]
  Click to reply to this thread Reply

Convention vs. Configuration

At 3:07 PM on Mar 6, 2006, Aviad Ben Dov wrote:

If I try to look at the path software made in its lifetime, I'd see that the amount of configuration required to build an application started with none, gradually grew bit by bit as more and more components were starardised until it reaches the state we're in today, the too much configuration stage.

Quickly peeking at a J2EE application's configuration file would reveal the horror and meaning of too much configuration . A huge, generally unmaintainable file, which essentially allows for configuration of anything in your application - Only that in practice, it allows almost nothing at all, a result of its size and complexity.

It comes with little wonder why Ruby on Rails ' designers chose to attach it the slogan Write beautiful code favoring convention over configuration . They attack one of the major lacks in J2EE development: The lack of defaults . This added complexity and overhead was made into a target, and even though few argue that the deployment phase is an issue as well, it has its own benefits rendering it as a smaller issue to most.

It also comes with little wonder, and maybe with a sigh of relief, that with Java EE 5 its designers added the "convention" to the configuration . There are big amounts of defaults and a lot of the resources can be looked up according to their field's name and type, making the redundant lookup code obsolete and a part of the past.

I gave RoR and J2EE as an example as it's a very hot topic lately, and the idea of this post is not to create a comparison between the two. Instead, it is that many other frameworks are following this path for that kind of change, and some already have: Tapestry , Hibernate and JAXB , just to name a few. It's obvious that using conventions and meta-data is the "next thing" in configuration , but as with anything new, it needs to be implemented wisely.

So, a few things to consider when writing a configurable framework:

  • The rule of thumb: Any component you have should be made to be configurable , if possible. You can never know will be using your framework and what they'll want to change in it - And changing a configuration file is always preferable to changing a framework's code.
  • Always, always make sure your conventions, defaults and meta-data are overridable by the configuration file . If they're not, it's the same as hard-coding your pieces together.
  • Provide and use annotations . They are configuration tags brought into the language. Add meta-data to your classes, fields and methods using annotations so that they would correspond to your xml file and essentially replace the redundancy it sometimes represent. Provide annotations for the framework's users so that they could annotate their classes, methods and fields instead of redundantly writing a configuration file.
  • Use defaults . Even if a configurable entity was not configured, there should be some proper default to attach it with. If you do make use of defaults though, make sure to document it carefully to avoid confusions.
  • Try to infer configuration from your users' annotated elements, and not just from the annotations' properties. The elements can, using reflection, provide a great detail about the annotated element such as the package it arrives from, the class its contained in, their name, etc. Reflection will give you a wide array of information to retrieve, and that information should certainly not be passed into the meta-data instance manually or written in a configuration file, forcing it to be maintained twice.
  • Make sure to provide a proper validator to your configuration data with your framework. It could be a part of the framework or it could be an external application, but it should provide with precise information regarding which configuration is wrong or missing and how it can be fixed. When configurations are provided in multiple ways, it's often confusing as to where a specific information is coming from, and proper validators make the life of a user much, much easier .

1 . At 4:38 PM on Mar 6, 2006, Will Hartung DeveloperZone Top 100 wrote:
  Click to reply to this thread Reply

Or....

The other option is even simpler: Don't bother.

Make configurable the bits that you end up reconfiguring if and when you actually reconfigure something.

Put the most base, stupid, hardcoded layers to act as abstraction firewalls between objects, but that aren't necessarily configurable themselves. You can always replace these simple layers later with a more configurable version later on.

Configuration is important to reusablity, but if you're not reusing, why make it configurable.

Configuration, whether through property files, XML files, annotations or hardcoded constants all equal one thing: code, and code needs to be maintained no matter where it is located.

Simply put, using the XP meme, configurable code is a Code Feature, and if you're not using it, then you don't necessarily need it, so you probably don't need to take the time to put it in in the first place.

The "horror" that is J2EE comes simply from the fact that at day one the designers envisioned a need for extremely portable code running in very fluid environments and containers. Portable method calls across portable components in arbitrary containers talking to arbitrary databases. Pushing all of those hard and fast configuration decisions back in to the container layer and out of the code.

If you find that your code moves from container to container, you will appreciate the "horror" (defaults or no). If your code tends to be stuck doing one thing in one place, then this configurability offers less value.
2 . At 5:25 PM on Mar 6, 2006, Aviad Ben Dov wrote:
  Click to reply to this thread Reply

And what if...

This is not aimed for someone writing code for himself, or for a closed application. A framework, especially as wide-spread as J2EE, can't be expected on how it will be used.

Some use it with one database, and never move it. Some are expanding, as the application they built grows larger. Move from JBoss to WebLogic, from MySQL to Oracle. These configurations change.

The idea of convention means that at first, when you first deploy and you need a quick solution, go with "XP" - You as a developer won't need to start configuring long xml files. You put a few annotations, write a small configuration file for the database, and viola. Later, override everything with a configuration file. If there will be a "later".

I'll say it again: Writing a framework which is wide spread, or might be, configuration is important. If you write a framework internally for your office, for your business or something similar, configuration could certainly be minimised.
-- Another Java Technology Blog, Chaotic Java.
3 . At 10:09 PM on Mar 8, 2006, Madhu Siddalingaiah wrote:
  Click to reply to this thread Reply

Who modifies the configuration?

External config files are great if you plan to modify them in the field or you have a nice GUI tool for editing them or someone other than a developer might need to change something. In cases where only a developer will ever change settings and there is no need to adjust things in the field, I prefer hard coding. It sounds crazy, but look at it this way: you get validation and an excellent GUI for free if you use a nice IDE (e.g. Eclipse).

In many cases, config files can't be edited in the field anyway, so you might as well use Java code rather than another language, parser, validator etc. At the end of the day, you are trading one language for another: XML instead of Java. Ask not what a config file can do for you, ask what can be done without it.
Madhu -> http://madhu.com -> http://madhu.com/blog

thread.rss_message