Encapsulation, What is it Good for ?

I just had a moment of clarity about refactoring and encapsulation. Its a simple truth and is one of the basic tenants of the concept and I had kind of forgotten about it.

It occured to me because I was working with some javascript. Its very easy to end up with lots of global methods in JS unless you use your closures properly.

By putting variables inside functions you can make them private.

Why is this useful. Well consider that you want to CHANGE one of those functions. Change ? who would EVER want to do that ?

If it is declared as a private function you KNOW FOR SURE that no-one else is using it and therefore that YOU CANNOT BREAK ANY OTHER CLASSES.

Therefore it is COMPLETELY SAFE TO REFACTOR.

great isnt it!

Such a thing you take for granted and yet when you work with a different language domain it suddenly reminds you of all the good things about OO.

Share

One thought on “Encapsulation, What is it Good for ?

  1. Pingback: non-random ramble :: “Object Calisthenics” : Jeff Bay

Comments are closed.