Programming practices that piss me off
Sep. 10th, 2007 04:22 pmToday's programming practice (and by "practice", I mean "anti-pattern") that pisses me off is repetition of code.
Generally speaking, having the same piece of code repeated over and over in different parts of the same system is bad, for the following reasons:
1) It makes the overall size of the system bigger. If you have a section of code that is 10 lines long, that's 9 lines longer than a single function call would be. If you have this code repeated 10 times in the system, now you're looking at 90 extra lines of code...
2) It makes fixing bugs or adding features more difficult. Instead of modifying a single function and testing it out, you now have to modify every single instance of this code, which makes for lots of repeitive (and therefore, error-prone) labor, and a larger deployment.
3) It breaks abstraction. It has been my experience that well-designed systems are full of abstractions, which in turn make the system easier to understand and maintain. For example, if I am building a page that displays an item for sale, a well designed project will have code that looks like this:
get_page_header();
get_user_info();
display_product($product_id);
get_page_footer();
While each function should still be documented, well-named functions can make it very obvious what is happening in other parts of the system that call those functions. The above example is certainly much easier to read than say, 100 lines of code that does the same thing.
4) It makes it harder to test. Having code sitting off in its own little function is easier to test, because (in the absense of globals), it is very clear what data is being passed in and what data is returned, if any. When that code is instead inlined into the middle of a page, it becomes much harder to test because now you have to worry about what comes before that section of code -- there is no easy way to isolate it. And unit tests are right out.
For further reading:
- Design pattern
- Functional design
- Anti-pattern
Generally speaking, having the same piece of code repeated over and over in different parts of the same system is bad, for the following reasons:
1) It makes the overall size of the system bigger. If you have a section of code that is 10 lines long, that's 9 lines longer than a single function call would be. If you have this code repeated 10 times in the system, now you're looking at 90 extra lines of code...
2) It makes fixing bugs or adding features more difficult. Instead of modifying a single function and testing it out, you now have to modify every single instance of this code, which makes for lots of repeitive (and therefore, error-prone) labor, and a larger deployment.
3) It breaks abstraction. It has been my experience that well-designed systems are full of abstractions, which in turn make the system easier to understand and maintain. For example, if I am building a page that displays an item for sale, a well designed project will have code that looks like this:
get_page_header();
get_user_info();
display_product($product_id);
get_page_footer();
While each function should still be documented, well-named functions can make it very obvious what is happening in other parts of the system that call those functions. The above example is certainly much easier to read than say, 100 lines of code that does the same thing.
4) It makes it harder to test. Having code sitting off in its own little function is easier to test, because (in the absense of globals), it is very clear what data is being passed in and what data is returned, if any. When that code is instead inlined into the middle of a page, it becomes much harder to test because now you have to worry about what comes before that section of code -- there is no easy way to isolate it. And unit tests are right out.
For further reading:
- Design pattern
- Functional design
- Anti-pattern