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)
      clip_image002[5]

       

    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.

      image

    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.

      image

    So, which would you choose?

     

    © Copyright 2009 - Andreas Zenker

    12 Comments

    • I stick with hungarian because of searchability and because the full control names are *too* big. Compare "DropDownListStates" to ddlStates". The former has most of its letters devoted to the type whereas with the latter the "States" part pops much more quickly.

    • i use option 2

    • I don't have anything wrong with Hungarian Notation while it's being used wisely. Naming UI controls is a good case of using Hungarian Notation.

      As for a longer naming, I'd still prefer to have the control type in front of its name, like "ButtonDo".

    • Hi,

      When I have been asked about the naming convention to follow, I suggest, dont suffix or prefix control type. I have not found that very useful. Most of the time, I think from application point of view rather than UI elements while coding. I think, I am going to assign value or retrive "first name" of the user. Moreover, may not be common, but sometimes, for eg, calendar object, today I might choose text but sometime later I may use Calendar control. So, now I have to update my control name as well.

      So, favoring "Readable names" and names should be as close as what the business calls it.

    • Option 2. I have discussed this with several other developers recently (outside my group) and they do as well. yet none of us could come up with winnable reasons why :) though we haven't stopped it. I think Intellisense is the number one reason. I'd probably move to option 3 if I had to, but that just looks wrong.

    • I use one, but I don't necessarily name my control instances by the type. e.g. A DropDownList might be "StateSelector" or "CountrySelector"

    • Oh... and I almost never use the IDE drop down to find my controls. I pretty much exclusively work in Source view and use incremental search (Ctrl+I)

    • To Will's point: I have also heard the, valid, suggestion in regards to grids panels and other layout controls to name them FooContainer instead of FooGrid or FooPanel. Then if the layout changes your name is still valid. Very useful in WPF where bindings are often only in markup and won't fire a compile error

    • We'd use uxDateFrom and uxDateTo etc. No type of control, just "ux" prefix

    • I've always used option 1. I've thought about switching to a different naming convention but never found one I liked more.

    • Naming ui control variables.. Neat :)

    • Naming ui control variables.. Bang-up :)

    Comments have been disabled for this content.