• Solutions
    • I Can Help
      • Define
      • Educate
      • Integrate
      • Advise
    • Consultation
      • Assessments
      • Design Reviews
      • Code Inspections
      • Ongoing Support
    • Training Classes
      • Training Schedule
      • On-Site Classes
      • Online Training
      • Customized Coaching and Mob Facilitation
    • Learning Roadmap
      • Developer Essentials Training
        • Agile Analysis and Design Patterns
        • Hands-On: Extreme Programming Practices
      • Scrum Framework Developer Essentials
      • Design Pattern Developer Essentials
      • Object-Oriented Analysis and Design Patterns
      • Scrum Software Developer Essentials
      • Agile Software Developer Essentials
      • Agile Software Developer Intensives
    • Speaking
      • Beyond the Legacy Code Crisis
      • Five Developer Practices for Agile Software
      • Writing High Quality, CLEAN Code
      • Essential Scrum Developer Practices
      • Improving Your Scrum Process
      • The Agile Development Advantage
    • Testimonials
    • Clients
  • Resources
    • Blog
    • My Book: Beyond Legacy Code
    • Bibliography
    • Referral Program
    • Associates
  • Contact
    • Contact Me
    • Schedule A Call
    • About
  • Schedule A Call

Encapsulation and Testability

  1. Home
  2. Blog
  3. Encapsulation and Testability

2025 Public Training Schedule

June 23 26, 2025 – Agile Analysis and Design Patterns – Half-Day Sessions Online

July 22 – 25, 2025 – Agile Analysis and Design Patterns – Half-Day Sessions Online

Register Now
Or schedule a private class
Course Descriptions
  • Developer Essentials Training
    • Agile Analysis and Design Patterns
    • Hands-On: Extreme Programming Practices
  • Object-Oriented Analysis and Design Patterns
  • Scrum Software Developer Essentials
  • Agile Software Developer Essentials
  • Agile Software Developer Intensives
Follow me on Twitter:

Follow @ToBeAgile

Blog Post Categories
  • Announcements {19}
  • Articles {2}
  • Bits and Pieces {74}
  • Blogosphere {1}
  • Bonuses {2}
  • Build in Small Batches {27}
  • Collaborate {38}
  • Create CLEAN Code {34}
  • Implement the Design Last {11}
  • Integrate Continuously {30}
  • Off-Topic but Interesting {3}
  • Rants {82}
  • Refactor Legacy Code {24}
  • Say What, Why, and for Whom Before How {22}
  • Seven Strategies {38}
  • Specify Behaviors with Tests {17}
  • Write the Test First {27}
Archives
  • 2023
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • 2022
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • 2021
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • 2020
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • 2019
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • July 2019
  • June 2019
  • May 2019
  • April 2019
  • March 2019
  • February 2019
  • January 2019
  • 2018
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • June 2018
  • May 2018
  • April 2018
  • March 2018
  • February 2018
  • January 2018
  • 2017
  • December 2017
  • November 2017
  • October 2017
  • September 2017
  • August 2017
  • July 2017
  • June 2017
  • May 2017
  • April 2017
  • March 2017
  • February 2017
  • January 2017
  • 2016
  • December 2016
  • November 2016
  • October 2016
  • September 2016
  • August 2016
  • July 2016
  • June 2016
  • May 2016
  • April 2016
  • March 2016
  • February 2016
  • January 2016
  • 2015
  • December 2015
  • November 2015
  • October 2015
  • September 2015
  • August 2015
  • July 2015
  • June 2015
  • May 2015
  • April 2015
  • March 2015
  • February 2015
  • January 2015
  • 2014
  • December 2014
  • November 2014
  • October 2014
  • September 2014
  • August 2014
  • July 2014
  • June 2014
  • May 2014
  • April 2014
  • March 2014
  • February 2014
  • January 2014
  • 2013
  • December 2013
  • November 2013
  • October 2013
  • September 2013
  • August 2013
  • July 2013
  • June 2013
  • May 2013
  • April 2013
  • March 2013
  • February 2013
  • January 2013
  • 2012
  • December 2012
  • Privacy Policy

    (c) 2025 To Be Agile

    • June 14, 2017 ( 2 comments )
    • Create CLEAN Code

    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 }

    • Robert says:
      November 6, 2017 at 11:42 am

      (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.

      Reply
      • David Bernstein says:
        November 12, 2017 at 7:46 pm

        Hi Robert,

        I agree that getters can be a code smell.

        Wikipedia defines encapsulation as:

        A language mechanism for restricting direct access to some of the object’s components.
        A language construct that facilitates the bundling of data with the methods (or other functions) operating on that data.

        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:

        “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 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:

        “To me, most fundamentally, encapsulation means hiding implementation details. Instead of expressing how to do something, we express what to do. I like to say that encapsulation is hiding the “how” with the “what.” Encapsulation is an important concept and an important code quality because it gives us the ability to change the implementation details without affecting other parts of the system.”

        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.

        Reply

    Previous Post: « Pathologies of Unencapsulated Code

    Next Post: Quality Code is Assertive »

    Leave a Reply Cancel reply

    Your email address will not be published. Required fields are marked *

    Solutions

    I Can Help
    • Define
    • Educate
    • Integrate
    • Advise

    Consultation
    • Assessments
    • Design Reviews
    • Code Inspections
    • Ongoing Support

    Testimonials Clients

    Resources

    • Blog
    • My Book: Beyond Legacy Code
    • Referral Program
    • Associates


    Read my book!
    Schedule a Call

    Training

    • Training Schedule
    • Learning Roadmap
    • On-Site Classes
    • Online Training
    • Customized Coaching and Mob Facilitation

    Course Descriptions
    • Developer Essentials Training
      • Agile Analysis and Design Patterns
      • Hands-On: Extreme Programming Practices
    • Object-Oriented Analysis and Design Patterns
    • Scrum Software Developer Essentials
    • Agile Software Developer Essentials
    • Agile Software Developer Intensives

    Copyright 2025 © To Be Agile. All rights reserved. Privacy Policy.

    This website uses cookies to improve your experience. I'll assume you're ok with this, but you can opt-out if you wish. Cookie settingsACCEPT
    Privacy & Cookies Policy

    Privacy Overview

    This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.
    Necessary
    Always Enabled
    Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
    SAVE & ACCEPT