1

Lets say you have a button objects on a form with a click event (c#/.Net). How is the event handler normally named throughout the industry?

  • fooButton_Click
  • fooButtonClick

(or if you like Hungarian)

  • btnFoo_Click
  • btnFooClick

When visual studio creates the event handler it defaults to underscore + EventName. This seems to violate the norm of using camelCasing and UppderCamelCasing instead of separating words with underscores.

Is there any reason to change it, or practically everyone following what Visual Studio does?

closed as primarily opinion-based by gnat, GlenH7, Bart van Ingen Schenau, user40980, Kilian Foth Feb 14 '14 at 11:59

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.

4

We are using well reviewed coding guidelines and standards which forbids the underscore for method (and event handlers) names. And, we are using PascalCase for method names.

Another practice for naming the event handlers is to give them time aware names. For example for the 'Click' event -> Click*ed*. This indicates that the event already happened and you have to deal with the rest. To make this name more time aware, the On prefix is being used. Last but not least, we are indicating the event object within the name, On*CancelButton*Clicked

OnCancelButtonClicked would be my choice.

PS: The underscore for event handlers probably comes from Visual Basic (Classic) to Visual Studio.

  • So what you are saying is that any time one of your team members adds a Click event to a button, and VS throws in the stub, they automatically rename if from Foo_Click() to FooClicked()? – BIBD Feb 11 '14 at 22:06
  • Yes, BUT we are not creating new UI elements daily basis. We do have own UI-Framework which creates the elements and the event handlers with our rules. Mostly we are on the code-behind. – Kaan Feb 11 '14 at 22:29
  • BTW, designer generated files are mostly excluded from those naming rules. Nevertheless, nowadays, with the modern IDEs and IDE-Extensions like Resharper, Code-Refactoring is very easy. – Kaan Feb 11 '14 at 22:36
1

I think there are places where the VS default is used, places where it is changed to some kind of different standard and places where it is up to what the individual developer prefers. Don't expect to find any "industry norm". Just find a consensus with your team how strict you want your coding standard to be, and if you want it strict, find some agreement how the code shall look.

1

The answer is in the language conventions for class library developers on MSDN.

That says that events should be PascalCase like everything else. The exceptions are parameters and protected field instances and that's it.

Based on your example the name should be FooClick or FooButtonClick if the event source is FooButton.

The specification advises developers not to use Hungarian notation at all.

  • The question is not about naming events, it's about naming event handlers. – svick Feb 12 '14 at 8:51
  • @svick if you're going to split hairs like that then I probably need to remind you that the whole section is about naming parts of libraries that other people will reuse but I didn't think that needed spelling out. The overall guidance is still clear to use PascalCase in most circumstances apart from a few listed exceptions. – James Snell Feb 12 '14 at 10:38
0
 FooButtonClick. 

Start all method names, class names and properties with a capital letter.

But however which one you choose - be consistent with the choice. That's the best advice I can give.

Not the answer you're looking for? Browse other questions tagged or ask your own question.