41

Microsoft has naming guidelines on their website (here). Also I have the Framework Design Guidelines book.

What I could not find was a guideline about naming controls.

For example, a button, when dropped to a form, gets the typename + number, camel-cased as default name, such as "button1".

This is what I do: I delete the number and add a meaningful description after. For example "buttonDelete" or "buttonSave".

This way you do not have to maintain a big list of controls and their abbreviated names in a guideline somewhere.

Do you agree?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Patrick Peters
  • 9,456
  • 7
  • 57
  • 106

12 Answers12

30

Here are some common ones:

frm  Form
mnu  Form menu
cmd  Command button
chk  Check button
opt  Radio button
lbl  Text label
txt  Text edit box
pb   Picture box
pic  Picture
lst  List box
cbo  Combo box
tmr  Timer

A longer list is at INFO: Object Hungarian Notation Naming Conventions for VB.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
NoahD
  • 8,092
  • 4
  • 27
  • 28
  • 2
    There is also *[Visual Basic Concepts, Object Naming Conventions](http://msdn.microsoft.com/en-us/library/aa263493%28v=vs.60%29.aspx)*. – Peter Mortensen Feb 22 '14 at 17:35
  • https://learn.microsoft.com/en-us/dotnet/standard/design-guidelines/general-naming-conventions?redirectedfrom=MSDN says "DO NOT use Hungarian notation." – Sinan ILYAS Nov 29 '22 at 09:21
28

Caveat: The following is more directed at WinForm/WPF development. Patrick Peters rightly pointed out that there are bandwidth/performance issues at play when dealing with ASP.NET controls.

There isn't really a standard here, and I believe that this is because its one of the most arbitrary naming scenarios. In most cases, controls are private to the class, and only used lightly in event handlers.

Like other answerers, I too used to spend a non-trivial amount of time "fixing" control names. I would do things like "btnSave", "tbxName" (tbx for TextBox), etc. However, when explaining my scheme to someone else, I realized how arbitrary that was. Is "cbx" a ComboBox or a Checkbox?

This led me to re-examine what the designer does automatically and realize that I can clearly, consistently, and quickly name controls if I let the designer do the work. Its actually very similar to the suggestion of the question poster:

I replace the control number with the semantics of the control. Thus "button1" (the designer default) will be "buttonSave", and "listBox3" will become "listBoxWidgets". If there will only be one control of that type, I just remove the number: "errorProvider1" becomes "errorProvider".

So how is this better?

  • Meticulously "fixing" variable names is a waste of time if its an internal variable
  • Your naming scheme is likely to be ambiguous if it shortens a whole bunch of stuff (CheckBox versus ComboBox)
  • The designer gives a good, consistent starting point that is easy (and quick) to specialize
  • The length of the variable name is irrelevant when you use Intellisense
  • Control names group nicely and intuitively (in Intellisense) when prefaced by their type. When you have 15 TextBoxes on your Form, you just first remember you want a TextBox , type "textBox", and then pick the name from the list.
  • Anyone unfamiliar with your "scheme" can see it immediately and adopt it quicker than anything else.
  • It is VERY fast to provide useful control names...very little keyboard/mouse jockeying to do this...so high productivity with intuitive results. What is not to like?

PS. This is tending towards a Bikeshed question, but as I can paint a bikeshed, I went ahead and joined the discussion. ;)

el2iot2
  • 6,428
  • 7
  • 38
  • 51
  • 2
    The length of the variable is relevant when you are regarding the performance in ASP.NET. The variable names could be part of the html-downstream. – Patrick Peters May 18 '09 at 05:51
  • 1
    @Patrick Peters - Good point. From the samples, I felt like this was mostly a discussion of WinForms controls wherein the name length would have minimal impact. However, I agree that, in the larger discussion including ASP.NET it would be different. I will update and explicitly state this assumption. – el2iot2 May 18 '09 at 16:34
22

I don't have a convention as such, but I do try to be very broad with the 'type' portion of the name. e.g. Button, Link Button, Image Button tend to be named 'somethingButton'. Combo boxes, radio button lists all end up as 'somethingSelector'. TextBoxes and Calendars are 'somethingInput'. That way I get a rough idea of what sort of control it is without the name being tied to the actual implementation. If I decide to replace an option button group with a dropdown then no need to rename!

Jon M
  • 11,669
  • 3
  • 41
  • 47
  • 1
    That's a really good idea. The most important thing is that the name reflects what the control is for. +1 – Mark Pim Mar 13 '09 at 13:54
  • That's exactly my point as well, you just expressed it better. +1 – Krzysztof Kozmic Mar 13 '09 at 13:57
  • I like the "implementation independent" name idea in theory, but in practice I find the refactoring of a control name to be near-instantaneous. Thus its a tradeoff between the time taken to "fix" the name when creating vs possibly refactoring it later. In the IDE, the latter is pretty fast. – el2iot2 Mar 13 '09 at 15:15
  • @ee - I agree that renaming is painless in VS, but chances are it'll get forgotten when time is tight and soon the 'LinkButton' controls will point to 'ImageButton's etc etc... – Jon M Mar 13 '09 at 15:26
  • @JonM so how would you call checkbox controls? somethingSelector? – Prokurors Jan 31 '15 at 09:37
  • 1
    @Prokurors I might go with 'somethingToggle', but 'somethingCheckbox' is probably fine - depends if there's another toggle-like control that could replace it in future. One might conceivably replace a combo box with a radio button list, hence using 'Selector' there to capture the intent rather than specifics. – Jon M Feb 05 '15 at 01:18
4

I don't do WinForms for quite some time but what I did was two things.

  • uncheck 'generate member' (or however it is called) for things like labels, etc. Basically ensuring I keep as fields only things I need.
  • for those I need, set descriptive name. It if is necessary, append the name of the control (ie saveButton). If I don't feel like adding a control name adds any value I would not append the 'Button' and leave the name simply as 'save'.

Basically most of the time I would not create a member for save button at all. (If you have some save logic you still can have only OnSaving event handler subscribed to the button's Click event).

https://msdn.microsoft.com/en-us/library/ms233630(v=vs.110).aspx

JRulle
  • 7,448
  • 6
  • 39
  • 61
Krzysztof Kozmic
  • 27,267
  • 12
  • 73
  • 115
3

Yes change those names

For me:

  • Button btnDescription

  • TextBox txtDescription

  • ComboBox cboDescription

etc...

Malachi
  • 3,205
  • 4
  • 29
  • 46
user68610
  • 169
  • 4
  • 1
    +1 because I use this too. However, I'd really like someone to tell me it's out-of-style and there's a better way. Hungarian is soooo icky. – Jon B Mar 13 '09 at 13:48
  • 8
    I _hate_ this style. This is the worst form of hungarian notation and it hurts my eyes. I'd stick a pencil in an eye of a developer in my team who would want to do this. – Krzysztof Kozmic Mar 13 '09 at 13:51
  • Hungarian is only really icky, IMHO, when taken to the Windows API extreme - e.g. lpstrzOut where the type annotation takes up more space than the name itself! Joel, as usual, has it right http://www.joelonsoftware.com/articles/Wrong.html – Mark Pim Mar 13 '09 at 13:52
  • How do you enforce this with a team of 100 developers for example? Well...you have to write it down in a guideline. And then... you have to maintain it for every new control (toolkit) developers will use... – Patrick Peters Mar 13 '09 at 13:58
  • @Patrick - that's a good point. You can have dozens or even hundreds of control types. Having a 2-4 letter prefix for each one is absurd. I usually generalize (an IP address input is just txt, for example). However, the whole concept may be obsolete anyhow. – Jon B Mar 13 '09 at 14:08
  • 1
    +1. I don't use it anywhere else but here. When you're working with the code you want a button but you wont know the names of all the buttons you just say want the one on the top right, whatever the hell it is called. This + intellisense makes it easy to discover the name o it. – Quibblesome Mar 13 '09 at 14:48
  • 2
    +1 I'm with Quarrelsome; just use this notation for controls to make it quicker to find stuff. Plus I work with web apps so I have far less controls to worry about. – Nick Mar 13 '09 at 15:01
  • To you who question this by saying "how do you enforce it" I wonder how do you enforce anything with 100 programmers. Is it better to have no naming conventions whatsoever? – Constanta Apr 09 '13 at 14:52
  • Its what i forget often but its how i should code, its very usefull, and reads nice. – user613326 Feb 20 '15 at 12:29
2

GUI programming gets the short stick when it comes to conventions of all sorts. See my answer to another question for the guidelines I use for naming.

Community
  • 1
  • 1
Michael Meadows
  • 27,796
  • 4
  • 47
  • 63
1

Yes, you need meaningful identifiers for any variable - control or not - the default names are only because your IDE knows nothing about your problem domain and so can't always 'guess' a better name.

Mark Pim
  • 9,898
  • 7
  • 40
  • 59
1

I'm probably one of the last few people that still uses Hungarian notation. I know that argument that the IDE can tell you the variable type, but that doesn't help me when I'm coding in Notepad++ or looking at a printout.... anyway, I use the "btnSave", "cbOptions", "txtFirstName", "lblTitle", "ddlCardType", etc... I just like being able to glance at code and know what I'm looking at without looking for a declaration or hovering over a variable to get it's data type from the IDE.

mlindegarde
  • 395
  • 5
  • 19
0

Yes, I agree totally (but I rename it to ButtonDelete), so lowercase names are for variables in my case :)

Personally, I think as long as you are consistent, you won't run into problems even if someone else is reading your code.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Gambrinus
  • 2,140
  • 16
  • 26
  • I wouldn't rename to ButtonDelete because every control you drop on a form becomes a private instance member of the Form class. Private instance fields are camel cased by default. – Patrick Peters Mar 13 '09 at 13:54
  • okay - as I said as long as you are consistent with your definitions and they are not way out of line - I think you are okay :) – Gambrinus Mar 13 '09 at 13:59
0

I'm not sure, but I think that control naming in Windows Forms is one of the only places I can see a use for Hungarian notation. So I think you're good.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Restore the Data Dumps
  • 38,967
  • 12
  • 96
  • 122
0

I believe that current thinking frowns upon including the control type in the name. I'd be inclined to treat them as another other object I'm using and follow the same naming convention.

Certainly use meaningful naming, that goes without saying :) However, at the end of the day, if your naming convention still makes sense to you when you revisit your code months later then I'd probably stick with it.

Lazarus
  • 41,906
  • 4
  • 43
  • 54
0

This is what we are using

In short, we prefix the controls with an abbreviation of the control. ie

Buttons = btnDelete, btnSubmit, btnReturn

Textboxes = txtUsername, txtPassword etc

that way, by typing the abbreviation you get all the similar controls by the time you finish typing the abbreviation ie type btn and intellisense will list all the buttons you have added so far.

Kostas Konstantinidis
  • 13,347
  • 10
  • 48
  • 61