4

I totally agree that we should not use Hungarian Notation to name variables. But it seems that Hungarian Notation is still useful to name controls (especially Winform controls). Consider these:

GridView grvUsers
TextBox txtPassword

etc...

I really doubt that should we avoid Hungarian notation in this case? If should, which is alternative solution to name controls?

Delta76
  • 13,931
  • 30
  • 95
  • 128

2 Answers2

6

Regardless of whether it is right or wrong, it is still the defacto standard to use Hungarian Notation for naming controls, and it seems very out-of-place not to adopt it. In the same way that methods in .NET languages use Pascal Casing (while in most other languages it is frowned upon), stepping outside of the accepted conventions for the environment you're working in just tends to make your code look even more out-of-place.

I am personally in favour of the practise, as it helps to distinguish class members which are part of the user-interface (view) from those members which are part of the code-behind (model/controller). If the control variables are given similar-looking names to those used to store data, state, etc then I feel as though it is harder to resist the temptation to tightly couple the two. Of course, a more distinct separation of logic would overcome that as well.

Nevertheless, Hungarian Notation leaves no doubt as to which variables are part of the user-interface, and also makes clear as to their type and function, both in the designer and the code editor.

Bradley Smith
  • 13,353
  • 4
  • 44
  • 57
  • 4
    Never mind that MS has said "don't do it". Never mind that every useful IDE in existence now can tell you the type of a variable just by you mousing over its name. Never mind that *you can't freaking read the names aloud without it being awkward.* Never mind that Hungarian notation should be describing the *kind* of stuff, not the class. Never mind that if you change that textbox to a rich text editor later, *your variable names are lying to you.* No, this practice *needs* to die. – cHao Oct 25 '10 at 04:43
  • @cHao: How should one name a control to distinguish it from the property associated therewith? – supercat Feb 14 '14 at 00:03
  • @supercat: If you're separating your concerns decently, then there won't be such a property. Your form is not the data; it's a *view* of the data. Ideally the form has a model object representing the data being shown/manipulated, and the controls' event handlers would tweak the properties of that object. – cHao Feb 14 '14 at 00:44
  • I don't understand the hate either, Hungarian is useful when used correctly, especially in busy source files. It's the same as anything isn't it, use it correctly and in moderation., I do not see how it can cause harm at worst it is ugly and best useful. It's also a great way of filtering using intelisense when you know you are looking for the identifier of a textbox or label. – Gavin Jul 27 '18 at 06:33
1

I usually name controls according to what they are, but with more Englishy names. Like, i'll name a label control for a first name box, "FirstNameLabel", and the textbox "FirstNameBox". It wasn't even intentional; i just noticed one day i was doing it, and it made sense to keep doing it.

I think i'm gonna call this "American notation".

cHao
  • 84,970
  • 20
  • 145
  • 172