Thursday, July 20, 2006

Windows UI Control Naming Conventions (Hungarian, Casing, AttributeControltype, or ?)

Coding in an Igloo - Naming Conventions for UI Controls

"...
While doing a little bit of research on this topic, I was surprised to find that nobody, Microsoft included, wants to explore more than one possible way to name UI controls. Although, I’m sure that I’m not the first, allow me to explore a few of them now.
...
My Choice
I personally prefer the use of Hungarian Notation when naming my user interface controls. I like the clarity, and, after some exploration of other options here, I also like that you have minimal chance of control vs. variable naming collisions. "


Man, I’m glad I’m not the only one who likes Hungarian Notation for UI controls.

I’ve been using Hungarian like notation for my controls since VB 1.[Oh by god that’s a long time ago] so it’s ingrained pretty deep. But I like to keep my mind open for better ways...

Casing (camel vs Pascal) for controls is not a true option for VB coding, so that’s out (call me a lamer, but case sensitivity drives me crazy anyway).

Recently I’ve been reading through the "Framework Design Guidelines" and like what I see. What it puts forth makes sense, is defensible and is pretty easy too follow. So my .Net class coding is moving toward those conventions. But UI controls are not covered (rightly so, as that’s not the focus of the book).

Also it seems MS/VS is using a new control naming scheme with generated controls, AttributeControltype (PersonIDColumnHeader, LastNameTextBox, etc.). Descriptive control names, that’s good. Much better than Textbox1, Textbox2...

Thinking it worth a try, I’ve used that convention in a couple apps (there’s no telling how any convention will work until you use it "for real"). And while it works and makes it easy to name controls, I miss the easy control grouping and lookup that Hungarian together with Intelisense provides.

When I’m coding I find myself needing to talk to a number of input controls (textboxes, etc) on a form. Instead of being able to easily find them via Intelisense by hitting "txt..." I have to remember the Attribute name first. And in many cases, there’s only one or two attribute items on a form (maybe a label and input control), versus a good number of inputs.

I’m looking for the textbox that holds the Last Name, not the Last Name’s textbox...

Using AttributeControltype naming means your controls are
mixed in with everything else in the Intelisense list. Using Hungarian means your controls are grouped together by type...

But as with all conventions there are issues, questions and nothing’s perfect. The most important thing is to HAVE an convention and to stick with it.

Still maybe Hungarian, for UI controls, is not that bad or old school after all...?

(via Jason Haley - Interesting Finds: July 19, 2006)

No comments: