开发者

When using a form designer, should labels be named?

In IDEs like QtCreator or with Visual C#'s form designer, I'd like to hear whether labels "should" be named or just left with the default naming scheme from the form designer. If the object is not going to be touched in code and only serves as non-changing textual informa开发者_高级运维tion, does the object need to be part of some naming scheme? If I have a label above a text field that the user enters their name in, should I name that label "lblNamePrompt"?

Just curious to get some responses.


I disagree with the accepted answer. There are some aspects of programming where it can be argued that laziness is a virtue, but naming isn't one of them—whether it's controls, variables, or members, your goal should be clarity, not efficiency.

Someone is going to have to understand and maintain this code eventually, even if it's only me trying to answer a question that you've posted on Stack Overflow. The more descriptive names that you have given your objects, the easier time me or anyone else is going to have sifting through your source code.

Assuming that you're using the forms designer in Visual Studio, I propose a very simple rule:

  • If you're not planning on using the control from anywhere in your code, you should set its GenerateMember property to "False". This prevents the designer from emitting a class-level member variable for the control, instead generating only a local variable in the InitializeComponent method, which limits clutter. Then and only then should you feel comfortable not giving the control a name.

  • Otherwise, if you are planning to or might ever reference the control from somewhere in your code, you should give it a descriptive name. Period. It's not that hard to type something.

I assumed the controversial part of this question would be whether quasi-Hungarian notation is useful
(as suggested in Garis's answer), not whether you should give controls a name at all. I personally like the way "btn" causes all button controls to be sorted together in Intellisense, but I understand that some people have a pathological hatred of Hungarian that presumably stems from having been forced to use it at some earlier stage in their coding career, so feel free to use it or not depending on your pleasure.


If the object is not going to be touched in code and only serves as non-changing textual information, does the object need to be part of some naming scheme?

I wouldn't give it a name if I am not going to use it.

Because I am lazy and because if something works as it is, I ain't going to fix that something.


I think keeping the code clear even if you are not going to touch it "never" (notice the double quotes) is a good practice. I will recommend to give a good name to it.

Im not a .NET programmer, but I've seen in some code is that they use names like:

For:

Labels:

  • lblFirstName
  • lblLastName
  • lblAddress

Buttons:

  • btnSend
  • btnCancel

I'm very PRO-YAGNI. But I think it worth to give objects a name that references it purpose/usage.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜