Hide your privates, but don’t be a prude

I recently re-read a great post by Alex Martelli on comp.lang.python about OOD in Python. Read the whole thing, it's brilliant.

Alex basically says that most of the encapsulation mantra in languages like C++ and Java is due to (1) remnants of the much derided Waterfall style of software development (BDUF), and (2) deficiencies in the language.

Enforced encapsulation is BDUF because you're deciding ahead of time what bits of your code your clients are going to need access to. Wouldn't it be better to suggest to your clients that they probably won't need this member or class, but if they really need it, here it is? In other words, the "we're all consenting adults" philosophy of programming.

By deficiencies in the language, he means that in Java/C++, if you make member variables public, then later decide that you need to change them to accessors (e.g. for proxying), then you're going to break all your client code. In Python, however, all you have to do is make that member into a property, and the clients don't have to worry about it.

In fact, too much private stuff is a smell. It's telling you that there's another class dying to break free of its current home and live all on its own. So rather than lamenting the lack of ability to enforce modesty in our Python programs, we should be looking for excessive prudery in our C++ ones.

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>