Here's a pattern I see a lot in imperative languages:
I try to live by the following rule:
So please, take that hint. Or I shall track you down and strangle you.
¹ As any fule kno, there are only two really hard problems in computer science: cache invalidation, naming things, and off-by-one errors.
² I once had a colleague who'd not only write subroutines that consisted of commented whitespace-separated sections, but who put each section into its own lexical scope. At which point, you've done 90% of the work to extract a new subroutine, so why not go the final step?
That is, a long subroutine with different sections separated out by vertical whitespace.sub doStuff { # do this code code code more code # do that code code code code more code # do t'other code code code code }
I try to live by the following rule:
If you are ever tempted to insert vertical whitespace in the middle of a subroutine to separate out different sections, just put the sections in their own freaking subroutines already.The usual reasons to strive for short subroutines apply:
- It's very hard to get an overview of a block of code that doesn't fit into one screen.
- Long subroutines make it much harder to follow the dataflow: where was this variable defined? When was it last set? When will it be used again?
- The subroutine is the basic unit of re-use; if a section of code isn't isolated into a subroutine, you can't use it again elsewhere.
- For maximum comprehensibility, a subroutine should do one well-defined task. If your subroutine's long, it suggests that it's doing lots of things.
So please, take that hint. Or I shall track you down and strangle you.
¹ As any fule kno, there are only two really hard problems in computer science: cache invalidation, naming things, and off-by-one errors.
² I once had a colleague who'd not only write subroutines that consisted of commented whitespace-separated sections, but who put each section into its own lexical scope. At which point, you've done 90% of the work to extract a new subroutine, so why not go the final step?
Tags: