Camilo Martin
Camilo Martin

Reputation: 37898

Rename ClassName_SomeEvent() handlers to SomeEventHandler()?

Edit 2: Actually SomeEventHandler is more concise than OnSomeEventHandler:

If I let Visual Studio generate an event handler for me, the name is like this:

private void ControlOrFormName_SomeEvent(object sender, SomeEventArgs e) { }

For example, the Load event handler of a form becomes:

private void MyForm_Load(object sender, EventArgs e) { }

I don't find this scheme attractive, it has an underline, etc. Besides, it's private, so I thought of this:

private void LoadHandler(object sender, EventArgs e) { }

I expect this won't confuse anyone (it's an open source project I'm starting) and since it resides in the control, I see no other possible meaning for MyForm.LoadHandler().

Does this make sense? Am I missing something that warrants the name having the class name?

Upvotes: 0

Views: 109

Answers (3)

Saeb Amini
Saeb Amini

Reputation: 24410

There's already an OnLoad method in the base Form class, as well as many other OnEvent methods that are protected. You generally override these methods in your child classes when required.

In your form's code type protected override On and IntelliSense would show you that this naming convention is already used so if you defined the same methods you would hide the inherited members.

Update

It's up to you but the important thing is being consistent with whatever naming convention you choose throughout the project and making sure everyone in the dev team is aware of the naming convention. Personally, I wouldn't bother as it would cause unnecessary headache later on when the project grows.

The power of naming conventions doesn’t come from the specific convention chosen but from the fact that a convention exists, adding structure to the code and giving you fewer things to worry about. -Steve McConnell

Upvotes: 2

Matías Fidemraizer
Matías Fidemraizer

Reputation: 64943

Visual Studio assists you on following well-known .NET naming conventions.

You can follow them or just use your own ones.

After all you know why widely-used naming conventions are the right way of doing things: a good number of developers should understand your code with less effort.

Upvotes: 0

Arnaud F.
Arnaud F.

Reputation: 8452

Generally On<EventName> is used to raise the event and nothing else.

Upvotes: 1

Related Questions