开发者

.Net - Better way of naming control - Hungarian Notation or Full Notation such as lbl or Label

开发者 https://www.devze.com 2023-03-20 23:38 出处:网络
In the old days, we name controls as lblFirstName, txtFirstName. I\'m seeing more and more full names like FirstNameLabel, FirstNameTextBox. Obviously, it doesn\'t make any different.

In the old days, we name controls as lblFirstName, txtFirstName.

I'm seeing more and more full names like FirstNameLabel, FirstNameTextBox. Obviously, it doesn't make any different.

My question is whether Microsoft officially recommended using full name like FirstNameLabel.

If this question 开发者_高级运维has been asked, please forgive me.


For UI Only objects such as a textbox or label; the convention of notating what sort of windows control it is is valid, and since it's been a tradition in .NET, I see no reason to change it. If your company has a different standard, use that standard; but for UI controls, the convention I've seen has always been to use the three letter prefix (I hesitate to call it hungarian notation, but that's the closest thing it resembles).


Using a "type" prefix is of the old Hungarian Notation days. It's generally not recommended for use when developing new applications.

The only time I would recommend it is when you're working with a legacy product that uses the convention throughout the application just for the sake of consistency.

Edit:

From MSDN Naming Conventions:

Do not use Hungarian notation.

Hungarian notation is the practice of including a prefix in identifiers to encode some metadata about the parameter, such as the data type of the identifier.


I don't agree not to use Hungarian notation, I think there are cases where it helps a lot, mainly in UI programming where you name your controls with prefixes like txt_ lbl_ and such, so you can find them very fast with intellisense when you need them. If this is cryptic opposed to memorising and being 100% fully concentrated on your variables having all their names in your head ...

What I find really redundant is having _ sign before private variables in classes, but this is different topic.


Read Joel's explanation on the origin of Hungarian Notation:

http://www.joelonsoftware.com/articles/Wrong.html

The gist is that it's not that useful in type-safe languages.


Intellisense makes Hungarian notation less useful but I'm having trouble coming to terms with not using Hungarian notation. If I want to hide a textbox and the associated label on the fly I find lblTown.visible=false and txtTown.visible=false intuitive. How do you do this without naming controls? TownLabel and TownTextBox seem to add more characters to type and no more information.

Am I missing something?


Here's a Microsoft link I found for their recommended naming conventions (Visual Basic).

0

精彩评论

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