David Buck
August 29, 2004
Originally published on David Buck's Blog
There's a principle of object oriented programming that I've never seen written up formally. For me, it's one of the key reasons to have object oriented software in the first place. I don't remember whether I came up with the term myself or whether I heard it from someone else first, but since I haven't seen it written up anywhere, I thought I'd do it here.
The principle is called "Local Change / Local Effect". It states:
Any localized design change to a software system should only require changes to a small amount of code in the immediate vicinity of the change. There should be no wide-spread or system-wide changes needed.
Encapsulation helps follow this principle by allowing changes in the representation of an object's state. The methods for the object may be affected, but callers of those methods shouldn't be. The effects of the change are localized.
Polymorphism helps by allowing us to add new objects without changing existing code to know about them. You only need to add the new classes and new methods. You shouldn't need to change existing code. (This is also known as the Open/Closed Principle).
Inheritance helps by providing one place to put common code for many similar objects. Changes to this code can be isolated to the superclass and may require no changes to subclasses in order to make them work.
There are many coding practices that tend to work against the local change/local effect principle. They include:
In any software system, the one thing you can count on is change. The local change/local effect principle makes change possible. Without it, as a system gets larger, it becomes more brittle and eventually becomes unmaintainable.
Think about your design principles. If they don't support local change/local effect, you may be building a system that will become too brittle to ever change again.