views:

175

answers:

5

I've been using FxCop for a while, almost all of the rules it suggests make sense, but when it comes to naming controls it throws a wobbly.

I tend to name things like this:

btnSavePerson lblForename txtPostcode ddlEthnicity

and so on.

Now, I only use prefixes for controls, I found that identifying them was a bit of a pain. I don't have this problem in all the rest of my code, so for a class representing a person I would have something like this:

public class Person
{
    private string forename;
    public string Forename
    {
        get;
        set;
    }
}

I seems daft for me to have two different naming conventions, FxCop complains about the prefix on the control names and I don't like the naming convention.

So how do people name controls on forms?

A: 

I don't use hungarian prefixes, I don't see them being of more value than they would be on variable names or public properties and I certainly don't use them there.

AnthonyWJones
A: 

Typically I will use an identifying name followed by the control type. For example 'ForenameTextbox', 'SubmitButton'.

Andy Rose
+1  A: 

I actually name controls the same way you do. I find it's provides for a faster way to identify a control from a code behind. If you really don't want to stick with this convention including some sort of prefix may be helpful, especially on big forms. For instance, if you are registering a user, taking their CC info, and billing info, prefixing the billing info inputs with "Bill" would provide the same result as "txt...".

For me it's a speed thing. Without a prefix I'd easily forget what I called an input box when I'm dealing with 10 or 20 others on the same page. And when I forget, I have to switch back to design view which is no fun in VS.

Ian Suttle
A: 

I follow the same method you do(like "imgBtnSaveFinal" for an Image Button), but what i know is that this debate can go on forever, what i feel is one should use one of the many standards after deciding if we are comfortable in using it, and how useful that notation will be for us in the future, i.e if the notation doesn't make sense to anyone, it doesn't make much sense using it either!

renegadeMind
+1  A: 

I always hungarian-notation my form controls and to hell with what FxCop and StyleCop are going to tell me.

Say I have a contact form which has an Email, Name and Message. We use a MVP pattern for our dev so I'll be implementing a view with three properties, Email, Name and Message, so I can't use those as the control names.

I don't like naming a control EmailTextBox as I don't see how that is any better than doing txtEmail. After all, you're still including the type with the name, what's the difference if it's at the front (and abbreviated) or at the back?

What I do like about using a prefix like txt is then all my text boxes are grouped in the intellisense window. If I've got a dozen or so form fields and I forget what I named one it's easy to just type txt and scroll through the list. A lot easier than starting at the top of intellisense or switching back to the design surface.

That said I would never ever, EVER use hungarian notation for anything but form fields. Form fields generally don't make it into a public API so I don't care about them.

Slace