Search code examples
encapsulationobject-oriented-analysis

What is the point of encapsulation?


I just don't see the point of encapsulation, I see that in some cases you can modify a getter/setter to modify the behavior of something or keep track of state, but whenever I am creating a non-conventional getter/setter, I use a word like "modify," or "obtain," so what is the point of wasting hours writing repetitive methods that are practically pointless and inefficient?

I just don't get it, when I was a wee young programmer, I was told by some guy in an IRC, that not having it was the cause for a bug in my program, but I have known for years now that is not the case, I've just been doing it anyway, so what is then point?

If I need to refactor later there are ways around it weird ones but they are ways at least in languages with overloaded operators, and API's don't always have to be backwards compatible so I don't see the point.

  • Can anyone enlighten me to the necessity of encapsulation?

Solution

    1. In many cases you are right - small programs doesn't need encapsulation probably.
    2. Some MS infrastructures (C#/WPF I think in several binding scenarios) requires encapsulation (using properties) and will not work without it.
    3. If you do more in get / set than changing the value or returning it - it will make your code nicer and more robust (do checks, or other staff in the setter for example).

    No one forces you to use it anyway...