Simberon Design Minute
 

Naming Accessors

When I'm developing in Smalltalk, I make it a practice to name my accessors for an instance variable with the name of the instance variable. I don't use the words get and set. For example, the instance variable name would have a getter called name and a setter called name:. This is pretty much the usual standard for Smalltalk. In Java, you always use the words get and set in the accessor names and in C# you tend to define properties instead. Back to Smalltalk for a moment, I recently ran across a case where I broke this rule to make the operation more clear. The variable was one that held onto a symbol or a block that indicated what to do when an element was removed from a collection in this object. It was called removeAction. When you create the object, you give it a remove action which would be performed whenever you called a remove method on the object. The problem is that if I named the accessors removeAction and removeAction:, then the method name becomes misleading. It sounds like the methods perform some sort of remove operation. Instead, they just get or set the instance variable. I decided to call the accessors getRemoveAction and setRemoveAction:. This way, they aren't misleading. There are times when even common conventions should be broken if it makes the code more clear.

Download