Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Naming C# events and handlers properly [closed]

From what I've read I'm not sure if I've got the naming convention for events and handlers correct. (there seems to be some conflicting advice out there).

In the two classes below can anyone tell me if I've got the naming right for the event, the method that raises the event and the method that handles the event?

public class Car
{
 // is event named correctly?
 public event EventHandler<EventArgs> OnSomethingHasHappened;

 private void MoveForward()
 {
  RaiseSomethingHasHappened();
 }

 // is the named correctly
 private void RaiseSomethingHasHappened()
 {
  if(OnSomethingHasHappened != null)
  {
   OnSomethingHasHappened(this, new EventArgs()); 
  }
 }
}

and the subscriber class:

public class Subscriber()
{
 public Subscriber()
 {
  Car car = new Car();
  car.OnSomethingHasHappened += Car_SomethingHasHappened();
 }

 // is this named correctly?
 private void Car_SomethingHasHappened(object sender, EventArgs e)
 {
  // do stuff
 }
}

Thanks in advance!

like image 827
bplus Avatar asked Jul 23 '10 09:07

bplus


People also ask

What is the naming convention for variables in C?

Rules for naming a variableA variable name can only have letters (both uppercase and lowercase letters), digits and underscore. The first letter of a variable should be either a letter or an underscore. There is no rule on how long a variable name (identifier) can be.

Should I use camelCase C?

The general practice for a C style language like Java or JS is to use camelCase for all variables and object members (properties & methods), and PascalCase for class names and constructors. Namespaces (or Packages in Java world) are usually in camelCase. But some languages make an exception to that.

Why do we use naming conventions?

Why use naming conventions? Naming records consistently, logically and in a predictable way will distinguish similar records from one another at a glance, and by doing so will facilitate the storage and retrieval of records, which will enable users to browse file names more effectively and efficiently.


2 Answers

Almost

The method to fire the event - On<When>Event (from RaiseSomethingHasHappened)

i.e. OnBeforeOpen, OnClosing, OnSomethigHasHappened

The event <When>Event (from OnSomethingHasHappened)

i.e. BeforeOpen, Closing, SomethingHasHappened

the handler <The Instance or meaningful Name><_><Event> (from Car_SomethingHasHappened)

i.e. Form_BeforeOpen, Window_Closing, Car_SomethingHasHappened -> perfect

like image 89
MaLio Avatar answered Oct 11 '22 07:10

MaLio


Well, the first point is that you define your own naming convention and there is no 'wrong' way to do it (as long as it's consistent).

Having said that, the Microsoft standards are good if your sharing your code with other.

Normally, you would have events names as:

public class Car
{
 // is event named correctly?
 public event EventHandler<EventArgs> SomethingHasHappened;

 private void MoveForward()
 {
  OnSomethingHasHappened();
 }

 // is the named correctly
 protected virtual void OnSomethingHasHappened()
 {
  EventHandler<EventArgs> locum = SomethingHasHappened;
  if(locum!= null)
  {
   locum(this, new EventArgs()); 
  }
 }
}

Note that the event is titled without the 'On' prefix, and the event firing method is named with the 'On' prefix. The event firing method is also protected virtual so that derived classes can override to change/add to the behaviour as well as use it to fire the event themselves when required.

like image 34
Dr Herbie Avatar answered Oct 11 '22 08:10

Dr Herbie