[MS] API naming principle: If there is no direct object, then the direct object is the source object - devamazonaws.blogspot.com

It is a common practice that method names begin with a verb: widget.Toggle(), widget.Set­Color(), widget.Get­Associated­Doodad().

Often, the verb is followed by a direct object: widget.Set­Color(), widget.Get­Associated­Doodad(). 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.Toggle() method above. In that case, the direct object is the source object: The widget. In the above example, widget.Toggle() toggles the widget.

<P< 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.Delete(). 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.Create­Listener() and widget­Notification.Delete­Listener().

As another example, the name of the Application­Data­Manager.Create­For­Package­Family 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.Create­Data­For­Package­Family.

An exception to this rule is factory objects. The purpose of factory objects is to create things, so a Widget­Factory.Create() method is assumed to create a widget. But I wouldn't complain if you called your method Widget­Factory.Create­Widget(), just to be clear about it.


Post Updated on July 24, 2024 at 03:00PM
Thanks for reading
from devamazonaws.blogspot.com

Comments

Popular posts from this blog

[MS] Pulling a single item from a C++ parameter pack by its index, remarks - devamazonaws.blogspot.com

[MS] Boosting Azure DevOps Security with GHAS Code Scanning - devamazonaws.blogspot.com

[MS] Going beyond the empty set: Embracing the power of other empty things - devamazonaws.blogspot.com