chris clarke
software development that works…or something
Annoying Patterns: The Hidden ‘On’ Switch
March 15, 2007 on 11:12 pm | In Annoying Patterns |The hidden ‘on’ switch is a method that you need to call on an object before it starts actually working. Specifically, the object does not make it clear that you need to flick the ‘on’ switch when you start calling methods before turning it ‘on’.
A couple of examples:
javax.jms.Connection:
If you don’t call start() it will appear to be perfectly happy but nothing will happen.
org.apache.tools.ant.Project:
If you don’t call init() it won’t tell you that init() needs to be called, instead it will say something like “Could not create task or type of type: property.”
Possible Solutions?
The JMS one is quite tricky because you want set up all your listeners before you start the connection - dunno. With the ant one, you could put init() code in the Project constructor (duh) or use a flag:
if (! initCalled) {
throw new YouForgotToCallInitYouMoronException("call init() first");
}
1 Comment »
RSS feed for comments on this post. TrackBack URI
Leave a comment
Annoying Patterns: The Hidden ‘On’ Switch
March 15, 2007 on 11:12 pm | In Annoying Patterns |The hidden ‘on’ switch is a method that you need to call on an object before it starts actually working. Specifically, the object does not make it clear that you need to flick the ‘on’ switch when you start calling methods before turning it ‘on’.
A couple of examples:
javax.jms.Connection:
If you don’t call start() it will appear to be perfectly happy but nothing will happen.
org.apache.tools.ant.Project:
If you don’t call init() it won’t tell you that init() needs to be called, instead it will say something like “Could not create task or type of type: property.”
Possible Solutions?
The JMS one is quite tricky because you want set up all your listeners before you start the connection - dunno. With the ant one, you could put init() code in the Project constructor (duh) or use a flag:
if (! initCalled) {
throw new YouForgotToCallInitYouMoronException("call init() first");
}
1 Comment »
RSS feed for comments on this post. TrackBack URI
-
Yep, this kind of thing is really irritating. Sometimes you feel like there’s a whole hidden state machine in there.
api.doStuff(1);
api.doStuff(2);Exception: doStuff(1) called without init()
api.init();
api.doStuff(1);
api.doStuff(2);Exception: doStuff(2) called without finish()
api.init();
api.doStuff(1);
api.finish();
api.doStuff(2);
// aargh - should there be a finish() here?
}
If the code is able to validate its own state it ought to be able to manage it for you too. The fact that it can’t is usually down to poor API design: they often carry too much state.
Comment by Duncan Pierce — April 6, 2007 #
Leave a comment
Powered by Cheese.
RSS Entries Feed.
RSS Comments Feed
^Top^
Yep, this kind of thing is really irritating. Sometimes you feel like there’s a whole hidden state machine in there.
api.doStuff(1);
api.doStuff(2);
Exception: doStuff(1) called without init()
api.init();
api.doStuff(1);
api.doStuff(2);
Exception: doStuff(2) called without finish()
api.init();
api.doStuff(1);
api.finish();
api.doStuff(2);
// aargh - should there be a finish() here?
}
If the code is able to validate its own state it ought to be able to manage it for you too. The fact that it can’t is usually down to poor API design: they often carry too much state.
Comment by Duncan Pierce — April 6, 2007 #