Observations, musings and conjecture about the world of software and technology

Control name prefixes are the work of the devil and other religious debates

Earlier today I asked the question I know it's a bit of a religious debate, but control name prefixes (txt, lbl); useful practice or the devil's work? and was a little surprised by the result. Actually, what surprised me was the unanimous “devil’s work” response when I expected some balanced arguments!

What am I talking about? I’m talking about names that look like this:

<asp:Label runat="server" ID="lblFirstName" />
<asp:TextBox runat="server" ID="txtFirstName" />

And why am I talking about it? Because FxCop doesn’t like it very much:

The individual words that make up an identifier should not be abbreviated and should be spelled correctly.

Microsoft goes into more detail in rule CA1704 but the bottom line is simply that they believe names should be spelled correctly and abbreviations kind of play havoc with that.

The argument “for”

Most of the arguments in favour of control prefixes centre around improved intellisense. The argument goes something like “It’s faster because as soon as I start typing the control prefix my list is filtered down to that control type”. The prefix brigade wants to see something like this:

Intellisense of control type prefix

Not having the prefix means the initial intellisense on the few characters picks up all sorts of other names:

Intellisense picking up other names

Thing is though, we’ve now got Pascal case intellisense in Visual Studio which means we can do this:

Pascal case intellisense

Frankly, this is even more explicit than the first version as we’re not going to get every single text box control in the list. It’s both succinct and explicit. Ok, you need to adjust your way of working a little bit but the advantages go way beyond just control references.

But what about when you want to find all the controls of a particular type on a page? Does having the ID of every text box on the page beginning with “txt” help? Let’s try it with a natural language suffix:

Intellisense finding all label controls

So no, you’re not gaining an advantage on that front. In fact so long as there is some form of consistency in control naming, it really doesn’t matter whether it’s an abbreviated prefix or a natural language suffix.

For the record, all these screen grabs are using ReSharper’s intellisense but the native Visual Studio 2010 intellisense is equally capable in these scenarios.

The argument “against”

Well there’s obviously the fact that FxCop doesn’t like it, but I’ll come back to that in the next section. Let’s start with a slightly more pragmatic basis.

The first thing is that abbreviated control prefixes is not “a” standard, it’s actually two standards. There’s agreeing that everyone is going to apply a prefix then there needs to be a standard on what prefixes to use when. It’s no good if John likes “val” for his required field validator and Jane likes “req” for hers, they’ve got to come to an agreement and create a standard on how the standard needs to be implemented! It’s a bit like having a policy on policies (don’t laugh, they exist).

Your other problem is that you can’t spell check them so any automated static code analysis which can check spelling (of which FxCop is obviously one), is not going to be very happy. Yes, you can usually implement exceptions to rules or customise dictionaries but you’re on a slippery slope here and its one more thing you’re going to need to maintain. Introduce a new control; that’ll be another exception. By the by, spell checking of names in code is a great idea because it’s simply so easy so screw it up in this spell-check dependent age (I assume it’s not just me!) and then so easy to proliferate it via intellisense.

The “standard standards” argument

One thing I really like about FxCop standards (and StyleCop standards as well, for that matter), is that they’re not mine. It’s not Troy’s interpretation of what’s right and wrong, it’s not the interpretation of the organisation I work on software for, they’ve been defined by the guys who built the framework we’re all working with.

This doesn’t make them right, and it doesn’t need to. What it does do is makes them very broadly relevant. That’s the whole thing about naming standards, it’s not that Pascal case is right and camel case is wrong or that underscore word breakers are evil, it’s simply that everyone who follows the standard agrees to do it the same way. Americans like to drive on the wrong side of the road but it works just fine for them because everybody does it :)

In my day job I work with a lot of totally independent software vendors and the last thing I want to do is dictate minor details about how to name a variable. What I want to do is say “There – go and follow what those guys say”, and when those guys are Microsoft and they provide the (free) tooling to analyse compliance, it’s a really easy decision to make.

Summary

I’ll admit I’ve switched backwards and forwards between many different naming standards over the years, usually depending on the environment and the people I’m working with. I really don’t have a preference for any one particular pattern of naming, but what I do have a preference for is the ease and consistency with which people can apply it. Today that’s natural language, dictionary compliant names checked by FxCop, tomorrow it might be something different.

The final thought on all this is that standards are never static. The Pascal case intellisense example is a perfect illustration of how the rationale of today may be invalidated by the environmental changes of tomorrow. This is perfectly illustrated in Pete Brown’s post about .net Naming Conventions and Programming Standards - Best Practices where he originally extols the virtues of control prefixing in 2002 but revises to the contrary in 2009. I’m not advocating changing standards every day, Pete’s example is a 7 year gap between drinks which isn’t exactly a radical change to the way of working.

So do what’s right for today, but make sure “right” isn’t based on shaky grounds or spurious arguments and that most importantly, compliance is readily measurable and achievable. And acknowledge you may need to change it in the future, quite possibly in less than 7 years from now!

9 comments:

Neal Blomfield said...

If the only reason for having naming standards like that is to satisfy an intellisense itch, I would argue you have a much larger issue in your team.

Troy Hunt said...

Have to agree with you Neal, but it's amazing how frequently the intellisense argument comes up. Obviously it's important to a lot of people, I just don't think that needs to dictate naming standards given the intellisense capabilities we have available today (if that really is important to someone).

Anonymous said...

I like naming my controls prefixing the type and then the name of it, ie: TextBoxFirstName, TextBoxSecondName, etc.

I don't use abbreviatures, solving some of the problems it causes, I can easily find a control by its type if I don't remember its name, and they are easire to name that way since VS names control using TextBox1... TextBoxn, having to change just the number.

Betty said...

Pascal case (private/protected) variables don't annoy you? Although I guess that really is irrelevant as it doesn't stop intellisense.

I've also seen common prefixes used for all controls either 'ux' eg 'uxFirstName' or an underscore '_firstname' (which is interesting considering controls are normally protected and not private).

I prefer both over the 'txt' style prefix as it requires less refactoring when you change a controls type (dropdown to checkbox list) and is easier to get a list of all controls on the page - i'm looking for the control for the users gender, not necessarily a textbox or dropdown.

Honestly I am more bugged by the fact that your example label doesn't have it's AssociateControlID assigned than anything mentioned thus far.

Troy Hunt said...

Well spotted Betty, I'll let you in on a secret; my original example included an AssociatedControlID attribute but after I found it wouldn't fit on one line in the blog, I deleted it. Form over function, I'm afraid!

Doug Rathbone said...

@Neal and Troy

First off - great post!

Secondly, the longer i code, the more i find it annoying that people extol the virtues of being l33t (your comment Neal) for l33ts sake vs being productive and getting the job done.

If you believe that having control prefixes is a sign that your team has problems, I can assure you that you're not looking at what is really wrong.

I'd much prefer that my teams code is well tested, and sticks to a convention in general (regardless of whether this convention changes project to project). If the project/team lead decides that he prefers txtFirstName or FirstNameTextBox i'm fine with either regardless of what fxcop says - as long as everyone sticks to it.

That being said i think Troy's point about third party suppliers is a huge plus that i had never considered - i will definitely take this into consideration when we next outsource.

Giorgi said...

The biggest advantage of firstNameTextbox is that as a developer I do not care what the type of the control is. All I need is to get the value from it. In case of txtFirstName you need to remember that the first name is inputted via a textbox. What if the type of the control changes later in the project? You will need to forget that it was a textbox and remember the changed control. With the first case you just type first and that's it. No need to remember whether the input is textbox, tichtextbox, combobox or anything else.

Darren said...

Not such an issue in WPF/Silverlight because using MVVM you rarely code against controls... you instead have a class that acts as the "view" and the controls bind to this.

Darren said...

Not such an issue in WPF/Silverlight because using MVVM you rarely code against controls... you instead have a class that acts as the "view" and the controls bind to this.

Post a Comment