Naming UI control variables
Discussed variable naming conventions with a fellow developer yesterday, particularly variables that are instances of UI control (e.g. textbox, label, button) Now, the new direction away from Hungarian Notation and toward longer, more readable and, therefore, more meaningful and maintainable field names would seem to spell the end of some old friends such as: cmdSubmit, btnRun and lblMaxAge.
So, when thinking about naming conventions moving forward here are the three options I argue back and forth with myself, with varying results….
1 - Follow the new "readable" naming conventions (this is the one I settle on most often)
- Combine the purpose of the control with the control type in one readable name.
e.g. SubmitButton, AgeText, AgeLabel etc
- This approach, while my favorite from a straight-up conventions point of view, does have one side-effect that may not be desirable. In the VS IDE your drop down of all the controls on the form will no longer be ordered by control type as it may have been in the old days of yore (Hungarian notation)
2 - Use the old style on controls as an "exception" to the new rule to preserve IDE experience
One reason I see mentioned to use Hungarian notation still for controls is, in fact, is to have them sorted by control type in the IDE drop down. This can be helpful if you have a large form with many controls and forgot what you named something.
3 - Opt for readable names but reverse the order of the words to favor IDE discovery.
A blend of the two previous suggestions results in this option. Preserve the readability of the full words naming convention but reverse the order. Somewhat less readable but now sorted by control type in the IDE.
e.g. ButtonSubmit, TextAge, LabelAge etc.
So, which would you choose?
© Copyright 2009 - Andreas Zenker