Thursday, 1 October 2015

Wise words on encapulation

From nklein software...
Encapsulation is about protecting the person who has to read your code. It’s not about protecting your code.
That is to say, if you have some complicated data structure and you diligently hedge it about with getter and setter functions, the benefit there is to those who will read and write code that uses that data structure. Which includes you of course. By using the access functions it is clear in the code what you are actually doing.  Your functions give meaning to the operations.

The reason often cited for using encapsulation functions - that it lets you change the implementation at a later date without needing to change the interface - is nugatory - that situation hardly ever arises.

This is a good point, well said.

No comments:

Post a Comment