Search code examples
encapsulation

Is there a reason to encapsulate if I am the only one using my code?


I understand that we encapsulate data to prevent things from being accessed that don't need to be accessed by developers working with my code. However I only program as a hobby and do not release any of code to be used by other people. I still encapsulate, but it mostly just seems to me like I'm just doing it for the sake of good policy and building the habit. So, is there any reason to encapsulate data when I know I am the only one who will be using my code?


Solution

  • Encapsulation not only about hiding data. It is also about hiding details of implementation.

    When such details are hidden, that forces you to use defined class API and the class is only who can change it inside. So just imagine a situation, when you opened all methods to any class interested in them and you have a function that performs some calculations. And you've just realized that you want to replace it because the logic is not right, or you want to perform some complicated calculations.

    In such cases sometimes you have to change all the places across your application to change the result instead of changing it in only one place, in API, that you provided.

    So don't make everything public, it leads to strong coupling and pain during update process.