Tuesday, January 1, 2008

Balanced Methods

How big should a method be?
What components of the method should be?

I had this question for awhile now, the question is what are good rules for writing good method? Everybody knows about coherency, simplicity etc. The problem I am trying to address is of a different nature - how to decide when to refactor a segment of a method into a new method?

After reading Object Thinknig, by David West, he has this concept of a System Abstraction Continium - the idea is that there are different levels of absraction system can be modeled at. It goes:

Conception Decomposition Analysis Design Source Code Object Code Execution

The key points are Conception and Execution. And the rule is that a method should be defined at a single level of abstraction.

E.g.
logMessage(msg):
file = open('file.txt')
foreach c in msg: file.write(c)
file.close()

is not leveled - since (IMO) open and close operation are higher-level operations than foreach c in msg...
To keep it leveled the method's components should have been:
open
write
close

Putting 7-things restriction on the method - and you have a method that makes me happy.

Monday, December 31, 2007

Audible.com

I used to read a lot. These days can't create an opportunity to read as much. Discovering audible.com was a major win. I spend at least an hour driving every day, and it really got me back that hour that normally was wasted.