How to Write Effective Object Oriented Code
The steps outlined in this article describe various tips and techniques for improving the quality of your object oriented code.
- Difficulty:
- Moderate
Instructions
-
-
1
Write tests first. This gets a programmer into the habit of thinking in terms of what the software is supposed to do and how clients will need communicate with it.
Before a method is written, a test is written that verifies that the function performs according to spec. This allows a programmer to test, implement and debug in smaller steps - it is much easier to debug 10 lines of new code than 2000 after the fact!
-
2
Code by intention. This is the practice of pretending that classes, functions, procedures etc. exist (even though they do not) as you structure and write your code. This helps a developer think about the overall process and larger steps of software rather than the small details.
-
3
Avoid Redundancy. Developers usually know that duplicating code is not a good idea. However, duplication is usually introduced into projects without developers even knowing they are doing it. One common practice is when a new requirement come up that can be implemented exteremely quickly simply by cutting and pasting code and making a few minor adjustments.
A better approach to this would be to get the original piece of code to work in both situations either by passing in variables or using a switch statement, etc.
-
4
Refactor as needed. This usually helps reduce redundancy and can strengthen the quality of code since code is re-written for clarity and maintainability. Test cases should prevent changing codes external behavior.
-
5
Encapsulate, encapsulate, encapsulate! Encapsulation is the concept of hiding implementation details. This allows developers the ability to change how a class stores and retrieves data or how an object is constructed (such as through the use of factories) without changing any objects that use or interact with that class.
-
1