Sunday, December 2, 2012

UI Controls naming conventions.

It is almost impossible to find a Windows or Web Forms application with a consistent naming of GUI controls. It is also hard to find a developer that consequently uses one naming convention throughout the applications.

There's been always a strong discussion on the importance of proper variable and method naming. And the GUI controls are not an exception.

Unfortunately it is not uncommon to find the auto generated control name inside of the code:

this.label7.Text = "Product name";

Another case is using various prefixes for the same control type. There is a textbox and then tBox, tbox, tbx, etc. I can live with control names that start with btn, lbl or tBox as long as they are used consequently. The order of words is also very important. If one button is named buttonStart then the second button should be buttonStop and not stopButton.

Personally, I believe it's a good idea to leave the default control full name, remove the auto generated number and provide a one-two word description of the purpose of the control e.g. listBoxCategories.

Going further, why not to make the control name more descriptive e.g.
  • textBoxForName
  • listViewWithProducts
  • linkLabelToDetails
This is in fact sometimes used in the framework. ASP.NET MVC Razor contains methods like TextBoxFor, PasswordFor just for creating HTML controls. They collocate nicely with lambda selectors as parameters and make the code easier to read.