Inverson of Simplicity

Right now I’m reading about Microkernel, an inversion-of-control thingy used by the Castle Project, which is an opensource .Net web framework along the same lines as Ruby on Rails.

Inversion of control used to make my eyes glaze over when people mentioned it. But this time, I looked at it and went “oh…that’s all it is?”

The basic idea, as described on devx, is dead simple…instead of building objects like this:

public class A{
  private B b

  public A(){
    b = new B();
  }
}

you build em like this:

public class A{
  private B b;

  public A(){
  }

  public setB(B param){
    b = param;
  }
}

This way if, say, B is a logging component and you change how you want to change how A logs things, you don’t have to change the implementation of A, or subclass it, you just pass a different kind of B into it. You can even make this change at runtime.

This is stuff that languages like Smalltalk have been doing forever. You’re always passing blocks of code into the framework objects. And they use it a lot more than Enterprisey folks dream of. When you read about Inversion of Control, they’re always talking about enterprisey things like logging and messaging and database connections. You can even get all sorts of fancy frameworks to do this stuff for you, maybe configured by some xml file somewhere. It’s very impressive and architectural.

By contrast, in Smalltalk you can’t even do a conditional or a loop without passing some custom object into a framework object. And it’s easy, because the language syntax is built around doing this sort of thing very concisely.

Another book I’m reading is Thinking Forth by Leo Brodie. Even if you’re not interested in Forth, it’s a great book on software design. And one thing Leo talks about is exactly this…making each component do one little thing, and building them to plug into each other, so it’s easy and concise to do whatever you want.

It’s like my comment on DSLs in the last entry…a lot of these Enterprisey people seem to make a big deal, and big complicated messes of code, out of things that in other programming communities are utterly simple, built into the language, and part of everyday programming. It’s not a bad thing, necessarily, to have a nifty framework that mashes your objects together according to an XML file – in fact, that might not be a bad DSL, except for the repetitive syntax – but if you’re stuck thinking in that mode you might miss how you can use this same technique, on a smaller and simpler scale, to improve all the code you write.

Just do the simplest thing that’ll possibly work, while thinking of your objects as little words and the whole system of objects as a language, with nouns and verbs and adjectives and adverbs, and I bet you end up with a beautiful, elegant, and supremely flexible design, no matter what language you use.

Advertisements

2 Responses to “Inverson of Simplicity”

  1. bitratchet Says:

    A favorite method I use in an inversion of control design is the notion of a Context object.

    class A { private Object a1;
    public void A( context )
    {
    try{ this.a1 = context.get(‘a1’);}
    catch( NullPointerException npx )
    {
    throw new ApplicationContextX( ‘missing resource a1’ );
    }
    }

    Which can reduce the number of getters and setters, which might not otherwise be necessary.
    It also shortens the signature of your constructor, so you don’t end up with constructors that take dozens of arguments.

    Inversion of control is particularly useful when encapsulated in a factory.

  2. Ehsan Says:

    “Just do the simplest thing that’ll possibly work, while thinking of your objects as little words and the whole system of objects as a language, with nouns and verbs and adjectives and adverbs, and I bet you end up with a beautiful, elegant, and supremely flexible design, no matter what language you use.”

    Hate to disagree but just assuming we have words, nouns, verbs and adjectives will not create a beautiful, elegant and supremely flexible design! There has to be some creativity, skill, experience and abstract thinking alongside many other things to get a system right. Usually on the second try! 🙂 -Just my thoughts

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: