3 ms·
Don't Repeat Yourself, but Sometimes Repeat Yourself
- tabtab 7y agoIn my experience the best reuse comes from studying the domain and existing code first. Pre-guessing without experience in the code base and domain often results in the wrong abstraction. And don't hard-wire yourself to the abstraction: allow one to use the "long-cut "when needed. Almost no abstraction fits 100% of cases. For example, suppose you make a DrawTextBox function for a UI. Make each component of it relatively independent so that if you can't use the entire function, you can still use most of it. function DrawTextBox(fieldInfo) { // full version DrawLabel(fieldInfo); DrawInputbox(fieldInfo); DrawExtraNote(fieldInfo); } Suppose you had to customize DrawLabel for a particular spot in your code: // inside application code myCustomizedDrawLabel(fieldInfo) DrawInputbox(fieldInfo); // regular sub-function still works DrawExtraNote(fieldInfo); // regular sub-function still works etc... Here we can still reuse some of parts even if we can't use the entire text-box abstraction. It's "fractal reuse": big abstractions, smaller abstractions within, no abstractions at the bottom. One is not forced to use the top levels if they don't fit.