Changeable code is happy code. Here's more about object-oriented programming, and some advice about why your code should be more malleable.
Join the DZone community and get the full member experience.Join For Free
Sensu is an open source monitoring event pipeline. Try it today.
So, what are the practices that support changeability in code?
There are surprisingly few, and I’ve written about them extensively here in this blog, in my book, and I discuss them in detail in my courses: inject dependencies, embody the SOLID principles, keep code quality high, and understand design patterns…there’s a lot to it but the list is finite. If you’re aware of just a few dozen key concepts you’ll be well equipped to write changeable code.
We can sum up what good code is all about by saying that it is changeable. That’s why we care about code at all because we need the ability to change it. If it was sufficient to write software once and never touch it again then we could write it any way we wanted and the quality of the code wouldn’t matter. But since software will need to be changed in its lifetime, we must write code in such a way that it is changeable.
This is why we moved to object-oriented programming and away from procedural programming. By defining our programs as a collection of objects, we could limit the knowledge of each object in the system, thus decreasing the impact of change across a system.
That was the intent of coding to an object model, anyway. Object-oriented programs are far more encapsulated and maintainable than procedural programs because, if we create our objects well, they will encapsulate their implementations and give us the freedom to change them later without breaking a lot of other code.
But object-oriented programs have their own problems.
Just because we can write decoupled code in an object-oriented language doesn’t mean all developers do. Object-oriented programs are like power tools. You can cut more wood with a chainsaw than a hand saw but you can also hurt yourself much worse. With the great power of object-oriented programming also comes the great responsibility to use OO in ways that improve maintainability.
Most programs that I’ve seen written in an object-oriented language do not take advantage of any object-oriented features. They are procedural programs written in an object-oriented language, so they’re difficult to maintain and extend. Objects aren’t encapsulated so their responsibilities become spread out. Even when objects are encapsulated, we build dependencies in such a way that it’s nearly impossible to break them, so our code isn’t independently testable or reusable.
When we write procedural code in an object-oriented language we miss opportunities to increase maintainability. It’s like mixing metaphors. Ideas become hard to follow. Behaviors are intertwined with other objects. Code degrades into a mess.
Supporting changeability is all about writing functionally independent pieces that are independently verifiable. It’s about creating accurate models in code. The “right design” is revealed by the problem itself, so we have to learn to listen to our code and see what we need to do to improve it.
My suggestion is to start by paying attention to testability because when we can verify that a small behavior does what it’s supposed to do, independently from the rest of the system, it’s more likely that code will be straightforward to change in the future.
Published at DZone with permission of David Bernstein , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.