December 12th, 2024

API naming principles for conditional operations: On, When, and If

From time to time, you may need to name something that performs on operation conditionally. The Windows Runtime has adopted the following naming conventions:

If you are requesting that action X occur whenever transition Y occurs, then use the term “On”. For example, if you have a property that specifies whether the Widget should auto-disable itself whenever it is hidden, you could name the property Widget.Disable­On­Hide. Even if this property is set to True, that does not preclude the possibility of enable the Widget while it is hidden. All it’s saying is that when the Widget transitions from Shown to Hidden, it will be flipped to disabled. The property doesn’t say anything about what happens after the transition occurs. In this usage, the term Y describes an action or transition.

If you are requesting that a condition X be in effect as long as condition Y is in effect, then use the term “When”. For example, if the property is named Widget.Disable­When­Hidden, then setting it to True means that the Widget is disabled whenever it is hidden. This is a continuous effect, not just a one-time effect when it transitions from Shown to Hidden. In this usage, the term Y describes a condition or state.

If you are requesting that an operation X occur right now, but only if condition Y is true, then use the term “If”. For example, a method called Widget.Disable­If­Hidden() checks whether the Widget is hidden, and if so, it disables the Widget. If the Widget is not hidden, then it does nothing. This check is done once, at the point of the method call. If the Widget becomes hidden later, this method call has no effect. It had one chance. An example usage as a parameter might be Widget.Create(bool disable­If­Hidden). In this usage, the term Y describes a condition or state.

Here are the results in table form.

Magic word Trigger Example Notes
On Edge triggered DisableOnHide Y is an action or transition
When Level triggered DisableWhenHidden Y is a state or condition
If One-shot DisableIfHidden Y is a state or condition
Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

0 comments