Over a million developers have joined DZone.

Encapsulate Change in Your Code

DZone's Guide to

Encapsulate Change in Your Code

There is no one-size-fits-all in software development. Check out this post to learn more about one developer's ideas around writing changeable and understandable code.

· Java Zone ·
Free Resource

"I love writing authentication and authorization code." ~ No Developer Ever. Try Okta Instead.

Software development is a unique human activity, but it bears resemblance to other activities, such as math, writing, and engineering. I think of writing software as a modeling process, but, unlike modeling physical things that occur in space, we’re modeling processes that occur in time. Therefore, writing software is a process of creating models that are accurate and understandable.

It turns out that there are many ways we can model processes in software. We can model processes as a set of instructions or procedures. We can model them through a collection of objects that interact. We can even model a process as a series of transformations. Different processes call out a need for different representations.

The way we build software varies tremendously, because we solve a whole variety of problems. This is why we have different programming languages and frameworks, as well as platforms. There is no one-size-fits-all.

Software is used in almost every industry to solve a huge range of problems, so it’s no wonder that the software industry itself has many facets to it. We specialize in languages and frameworks, as well as different technologies, but, beyond that, we have to have some common thread that unites us — a common understanding about software development itself. I feel that all too often this is lacking in our industry. It seems like the goal is often just to do something that works and ship it without much regard to its supportability. The end result is that we’re cutting corners, and we’re writing code that’s not as supportable as it could be. This drives up the cost of maintaining software and makes it difficult to extend.

One of the main benefits of object-oriented programming is that it allows us to build encapsulated, modular components that are independently testable and upgradable. However, rarely do I see developers use object-oriented languages to build software with these advantages. Instead of injecting dependencies as needed, they instantiate them inline, and the result is that they can’t be tested independently.

In an object-oriented system, behaviors should be encapsulated in objects in order to limit side effects and allow the creation of a more modular system.

Code should be expressive, just like good literature. Code should express what it does and do its job plainly and clearly. Software should be expressed in the domain language and encapsulated in objects so that they’re more maintainable.

By spreading the knowledge of a system across a collection of interacting objects, we minimize the impact of change when it happens, because change will typically only affect the objects that are directly involved in the change so the rest of the system will be encapsulated and shielded from the change. This is good, because it minimizes the impact of adding new features to existing software.

The software lifecycle is important. We spend only 20 percent of our budget before release and 80 percent of our budget after the initial release of software. Less than a third of that is maintenance. The rest is fixing defects and providing enhancements. We are essentially building unmaintainable software in our industry, and that is the norm.

Changeable software is not that much more difficult to create. It does take some more typing to put concepts into their own encapsulated entities, but it also simplifies testing. Ideally, we would like to produce complex behaviors by applying a series of simple behaviors that we can independently test and verify.

"I love writing authentication and authorization code." ~ No Developer Ever. Try Okta Instead.

java ,encapsulation ,code expressiveness

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}