It is a common practice that method names begin with a verb: widget.
, widget.
, widget.
.
Often, the verb is followed by a direct object: widget.
, widget.
. The direct object is the thing that the verb operates on or produces. In the case of SetÂColor
, it is setting the color. In the case of GetÂAssociatedÂDoodad
it is getting the associated doodad.
Sometimes, the verb is not followed by a direct object at all, such as the widget.
method above. In that case, the direct object is the source object: The widget. In the above example, widget.
toggles the widget
.
All of this may sound obvious, but it’s easy to lose sight of this principle.
For example, a team proposed an API with a WidgetÂNotification
and a method widgetÂNotification.
. As written, it sounds like this deletes the widget notification itself, but the intention was for this to delete a notification listener. The methods for creating and deleting listeners should be named something like widgetÂNotification.
and widgetÂNotification.
.
As another example, the name of the ApplicationÂDataÂManager.
method doesn’t say what it’s creating, so the assumption is that it creates an ApplicationÂDataÂManager
. But that’s not what it creates. It actually creates an ApplicationÂData
object. The method should more properly be named ApplicationÂDataÂManager.
.
An exception to this rule is factory objects. The purpose of factory objects is to create things, so a WidgetÂFactory.
method is assumed to create a widget. But I wouldn’t complain if you called your method WidgetÂFactory.
, just to be clear about it.
Of course, if your factory creates many different kinds of objects, then you should probably have the methods state what they create.
MFC was really annoying about this. Class members would use full names that copied the API function names they wrapped, so you would end up with CWnd::MoveWindow() and whatnot.
What I don’t like is boolean getters starting with “Get”. Like “Stuff::GetFrobnicating”. It sounds confusing compared to “IsFrobnicating” but that breaks starting getters with “Get” and get/set pairs in case you also have a “SetFrobnicating” (and “Get/SetIsFrobnicating” is not much better…).
In COM you could mark certain methods as a "getter" or a "setter". The idea being that if you were in a language where an object supports properties you would then create a property based on the of the getter and setter names:
- getIsFrobnicating
- setIsFrobnicating
which then gives you a property you can project into your langauge:
<code>
Or in Delphi:
<code>
So i would argue that IsFrobnicating should be a property. And if your language doesn't support properties,...
I think the difference is whether we are getting a property of the object or querying the status. For example, in an object that represents and article in a store or in an inventory, GetDiscountable() tells us about a (mostly) immutable property (the article admits discounts), while IsInStock() tells us about a volatile status (the article is in stock right now). Or, in a canvas, we could have GetObjectCount() (the number of objects in the...
Just as long as we aren’t making a `WidgetFactoryFactory` we’re all good.
Certain coffee-named language would one-up you with WidgetFactoryFactoryCreator.