Naming methods that are registered to events

2019-05-01 17:38发布

问题:

Assume that I have a class that exposes the following event:

public event EventHandler Closing

How should methods that are registered to this event be named? Do you prefer to follow the convention that Visual Studio uses when it assigns names to the methods it generates (aka. +=, Tab, Tab)? For example:

private void TheClass_Closing( object sender, EventArgs e )

Or do you use your own style to name these methods?

I've tried different ways to name these methods (like TheClassClosing, HandleClosing, etc.). But I haven't found a good style to indicate that the intent of a method is to handle a registered event. I personally don't like the style (underscore) that Visual Studio uses to generate method names.

I know that registered event-handling methods are always private and that there is no naming convention like the one for methods that raise events (e.g., OnClosing).

回答1:

The two common options for naming is either after what the method does:

theObject.Closing += SaveResults;

Or alternatively after what the method handles:

theObject.Closing += ClosingHandler;

Which is preferable really depends a bit on context.

In the first case it is immediately clear what the handler is going to do, which makes the code registering the handler more readable... but looking at the handler SaveResults in isolation it is not going to be necessarily obvious when it is going to be called, unless the event arguments have an obvious name (ClosingEventArgs or some such).

In the second case, the registration is more opaque (okay, so what is going to happen when Closing happens?), but on the other hand, looking at the handler implementation it will be obvious what is going on.

I guess the one to choose depends on which of the two you want to be more obvious; the site of the registration, or the implementation of the handler.

Or alternatively, you can go for the unholy combination of both methods:

theObject.Closing += ClosingHandlerSaveResults;

Now both the registration site and the implementation are equally obvious, and neither looks particularly elegant (plus, it probably violates the DRY principle).

For the record I prefer the first naming scheme when theObject is contained in a different scope from the implementation of SaveResults, and the second scheme when I am wiring up handlers to events that are all contained within the same class.



回答2:

Name it after what the handler actually does.

// event += event handler
saveButton.Click += SaveData();
startButton.Click += StartTheTimer();


回答3:

I name my event handlers similarly to those created by Visual Studio (the +,=,tab,tab you mention). I try to keep my naming consistent in my code, and I know that I will be creating handlers with the VS auto-creator at least some of the time.

The underscores don't bother me.



回答4:

maybe: OnObjectNameEventName, such as

private void OnTheClassClosing(object sender, EventArgs e)

This matches the internal event methods, and with the addition of the object name, it should help differentiate, besides, the method to raise events are essentially internal event handlers

User clicks form, form calls OnClicked, does its thing, then raises the Clicked event, it would only be natural from my point of view.