Ed's Big Plans

Computing for Science and Awesome

Archive for the ‘Object Oriented Programming’ tag

Borrowing Ruby ideas: Returning an object instance

with 3 comments

Brief: Ruby conventions were designed to be particularly satisfying and intuitive for the developer. One convention that Ruby adds to the object oriented world is for mutators (setters) to return the object instance — that is, calling an object’s mutators will not only alter the object, but will also return the object (not the mutated property). This is especially useful if you want to chain a bunch of mutators together for code legibility or developer convenience.

This would be a welcome shorthand for developers of C-derived object oriented languages such as Java and C#. The following chain…

a.setMass(17);
a.setName("cube");
a.setFace(null);

…would become…

a.setMass(17).setName("cube").setFace(null);

…a much more compact and what I feel is a more intuitive chain.

Eddie Ma

June 27th, 2010 at 3:00 am