Friday, October 20, 2006

Optimal Function Size

How big should a function be? Someone has very practical answer, paraphrasing what the author said:

"Once my friend interviewed a candidate for programming job, in the interview my friend casually asked how the candidate tell if a function is too big. 'When it is bigger than my head', the candidate replied. My friend was puzzled, thinking this candidate was referring to his brain capacity. The candidate went on to explain 'I will stick my head on the monitor and if the function is bigger than my head, it is not good'."


This is probably one of the most practical definition for optimal function size I have seen so far, mainly you have something very measureable during formal code reviews. i.e. What? you think your function is all right? You have a 2-feet face?

If I were the interviewer, I would have hired that guy.

So dear reader, do your have bigger-than-your-head functions today?

4 comments:

Jimmy L. said...

Cyclomatic complexity >= 10 is big.

A more accurate measure is Coffee-O-Matic Complexity: I count it with the number of cups of brewed coffee used in making the program, either for drinking or throwing.

Cuppa Chai said...

Cyclomatic complexity is the canonical metric, can't argue with that.

The Soothsayer said...

It should be as big as it should be and not any bigger nor should it be smaller.

Cuppa Chai said...

Einstein's version "things should be as simple as possible, but not simpler". :P