May 16, 2003

Hiding Implementation

I have been reading the classic Object-Oriented Software Construction by Bertrand Meyer. He talks about abstracting the implementation details, in particular hiding wether a feature of a class is stored or computed at run time, and then goes into the syntax for doing this in Eiffel, the language that the book points people towards.

Eiffel has the concept of a no-args function being accessed in a similar way to a attribute, so that clients cannot tell the difference, making it possible to transparently replace functions with attributes.

I had a bit of a think about how Java does it, and the obvious answer is JavaBeans. ie lots of getX and setX style methods on a class that have single lines that do the get or set. (<ctrl>-ins in IntelliJ).

So the Java way is always to make things look like method calls. Betrand raises some good points about how to hide things, and I think that Eiffel looks pretty good. It is does more in the language to make it easier to write things than in Java. ie in Java you have to do things by best practices, and it doesn't make things as easy natively. But then IntelliJ makes best practices shortcuts so -- meeting their "Develop with pleasure" tagline.

Ruby does it differently to either language. In Ruby you create a method called attribute= for assignment, and the compiler does the magic to treat it like straight assignment (see Programming Ruby for more). Of course Ruby rocks ;). Lots of nice bits of syntactic sugar and good stuff.

Posted by rob@rojotek at May 16, 2003 10:08 AM
Comments