A common instinct among a certain type of programmers is to try to refactor, generalise and abstract away every repeated line of code. The opposite tendency is to cut and paste large sections of code, which can quickly generate long sprawling programs that are difficult to maintain.
Avoiding repetition is a good thing then? Well yes, but it has to be balanced against readability. In order to avoid repetition common functions, data structures and behaviours are packaged up in abstractions such as functions and classes. Taken too far this scatters the flow of the code across multiple functions and classes, all for the sake of avoiding a little repetition. I've come back to code I've written myself and found the layers of abstraction I've used more of a hindrance than a help. While I was writing it of course I thought I was being really clever.
Good abstractions are hard to work out and not always worth making. Now I consciously try to write my code using a few simple abstractions that can be easily explained and remembered. Repetition is allowed if it means that the information I need to understand the code I'm reading is right there in front of me, rather than n-steps away in a forgotten object heirarchy.
Rigidly defined areas of doubt and uncertainty.
Thursday, July 22, 2004
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment