Events should not have before or after prefix (CA1713, Microsoft.Naming)

by Mark Zhou 6. December 2010 19:57

An event represents a signal that causes to raise a specified action. For example, when you feel hungry, you will find something to eat. In this case, you can call Hungry an event, and call Eat() an action; as well, the Eat() is called the Event Handler, which will be used to handle things while the event is raised.

As the event is just a signal, or what we called a descriptor, there is a because-therefore relationship between event and event handler, raise an event is specifically happening on the point of a time, hence, most of the events represent present status. To name an event correctly and accurately, it is highly recommended that you use the general form of a verb or a verb phrase. For example, Click, DoWork, Load, Show, SaveControlState and KeyPress.

However, you may also name an event with a post tense form of a verb or a verb phrase to represent a specific sequence when raising an action. Like TextChanged, WorkItemCompleted, Connected, Initialized are kind of these forms.

But do not use “before” or “after” prefix as part of its name to describe the sequence. Event been raised on a point of time so it is most likely to be a present tense stuff, using the general form or the post form of a verb makes more sense. This is also defined in the common naming conventions. If you want to define events in pairs to exactly describe the sequence of a raising action, use present and post form; if you only want to define one event for a raising action, just use the general form or post form.

Here are some example that define events in pairs: the Closing and Closed events of a System.Windows.Forms.Form, the KeyDown, KeyUp and KeyPress events for keyboard strikes, and the PropertyChanging and PropertyChanged events for a data bind object.

Here are also examples that define events just once: the Click event of a System.Windows.Forms.Button, the DownloadDataCompleted event of a System.Net.WebClient, and the TextChanged event of a System.Windows.Controls.TextBox.

Do not suppress a warning message from this rule, to fix this warning, eliminate the “before” and “after” prefix from the name of event.

To get more information, please see the MSDN link for CA1713.

Tags: , , ,

Pingbacks and trackbacks (1)+

Add comment




  Country flag
biuquote
  • Comment
  • Preview
Loading


Translate This Page

About Mark

Mark is a developer who works for building base class libraries and tools for developers.

Mark's Awards

Microsoft Community Contributor

Tag cloud

    Month List

    Who visit this site

    Recent Comments

    Comment RSS