2024 Public Training Schedule
December 9 – 12, 2024 – Agile Analysis and Design Patterns
Half-Day Sessions
(c) 2024 To Be Agile
Poorly encapsulated software is hard to test because without clear boundaries, what we actually test becomes significantly larger than what we need to test. This makes our tests run slower and it also makes them brittle so our tests are harder to maintain.
When code is unencapsulated, it can be hard to lock down behaviors in a system that we want to test. This can get so bad that we may not even be able to use a programmatic interface and must resort instead to simulating the user interacting with the system.
Manual testing or even automating user input to drive testing is a bad idea. It’s far too high a level and it ties your tests to your user interface, making them brittle. It’s far better to provide a programmatic interface that can be used to test code.
Unit testing is the first type of testing we should think of because it’s the simplest and also the most cost-effective.
Find problems early, or better yet, set up the system so we just can’t make mistakes. Encapsulation is like that. Encapsulation is a promise that a boundary is created and that nothing will penetrate that boundary. We can define an object that has public parts and private parts. The public parts can be accessed by anything or anyone but the private parts are internal, nothing on the outside can access the private information or behavior inside.
This guarantee in software languages allows us to create software that is both reliable and secure. Of course, by convention, instance data that an object holds should be marked private so that no other object can access it directly. If outside objects do need access to that data then we will provide public getters and setters.
We may, for example, want to serialize access to a particular resource so we’re granting access to only one request at a time, or we may want to just keep track of the requestors, or keep account of them, or whatever. The object that holds the state gets to decide—and that’s the point of object-oriented programming. We want objects to encapsulate their own state and be in charge of it, that is to say to contain the behaviors that access that state, which is the next code quality that we’ll be talking about: assertiveness.
Testable code tends to be well-encapsulated. It hides implementation details and can validate that behavior is correct. Testable code is code that can be tested at the unit level. When code is built with tests in this way there’s less need for other kinds of tests. A lot of the QA testing, scenario testing, and other types of non-automated testing can go away. We’re then left with a suite of tests that have all the characteristics we need: they run fast, they give the right level of feedback, and they support refactoring—all good qualities in a test base.
Unit tests run fast because we’re only testing what we need to. If the tests were written well and written to be unique, unit tests also provide the right level of feedback.
And finally, when they’re written to test behaviors rather than implementations, unit tests support refactoring. If we test behaviors and then refactor the code so we’re changing the design but not changing the behaviors our tests shouldn’t break.
{2 Comments }
Previous Post: « Pathologies of Unencapsulated Code
Next Post: Quality Code is Assertive »
(Copied my comment from DZone, because you don’t seem to answer there)
You say that encapsulation means that fields are always private, and when outside objects need access, we provide that with getters and setters.
I think encapsulation is about the idea that an outside object _should not need_ access to the data of my object. In fact, encapsulation and getters are two fundamentally incompatible things. Your article states, that objects should be “in charge” of their data. They can’t be in charge of something they publish, can they? Things returned by getters are by definition then out of the control of the object.
Hi Robert,
I agree that getters can be a code smell.
Wikipedia defines encapsulation as:
So, one could say that our industry defines encapsulation by the definition you said that I gave. I believe you were referring to these two sentences above:
This statement is congruent with Wikipedia’s definition of encapsulation but that isn’t how I define encapsulation. In this blog, you’ll find over a dozen posts about encapsulation that I’ve written over the years. The above post is part of a series of posts on code qualities. I define encapsulation two posts back when I introduce the term. I defined it there as:
And by my definition of encapsulation, you are correct, getters are often “implementation details” because often what we’d really like to encapsulate is a process or a behavior rather than a piece of code. I think this may be what you’re getting at and if so I fully agree. For example, providing a getter for an authenticated password would clearly break encapsulation and defeat the security system.
However, there are times when using getters are the right thing to do. Many developers I know would code a leaf object (sometimes called data-only objects), such as an Address object whose sole purpose is to hold an address, by defining the fields as private and then providing public getters to access those fields. This lets us encapsulate the underlying data type so we can change it without necessarily affecting the accessors interface and therefore its callers.
I believe that getters can be a code smell because they violate another code quality that I discuss in the series, assertiveness. Providing getters on a leaf object doesn’t make that object less assertive or put its data out of the object’s control but in some cases, it can force callers of that object to be inquisitive rather than assertive.
That’s the subject of another post someday.
David.