4

我完全同意我们不应该使用匈牙利符号来命名变量。但似乎匈牙利表示法对命名控件(尤其是 Winform 控件)仍然有用。考虑这些:

GridView grvUsers
TextBox txtPassword

ETC...

我真的怀疑在这种情况下我们是否应该避免使用匈牙利符号?如果应该,哪个是名称控制的替代解决方案?

4

2 回答 2

6

Regardless of whether it is right or wrong, it is still the defacto standard to use Hungarian Notation for naming controls, and it seems very out-of-place not to adopt it. In the same way that methods in .NET languages use Pascal Casing (while in most other languages it is frowned upon), stepping outside of the accepted conventions for the environment you're working in just tends to make your code look even more out-of-place.

I am personally in favour of the practise, as it helps to distinguish class members which are part of the user-interface (view) from those members which are part of the code-behind (model/controller). If the control variables are given similar-looking names to those used to store data, state, etc then I feel as though it is harder to resist the temptation to tightly couple the two. Of course, a more distinct separation of logic would overcome that as well.

Nevertheless, Hungarian Notation leaves no doubt as to which variables are part of the user-interface, and also makes clear as to their type and function, both in the designer and the code editor.

于 2010-10-25T04:37:42.770 回答
1

I usually name controls according to what they are, but with more Englishy names. Like, i'll name a label control for a first name box, "FirstNameLabel", and the textbox "FirstNameBox". It wasn't even intentional; i just noticed one day i was doing it, and it made sense to keep doing it.

I think i'm gonna call this "American notation".

于 2010-10-25T04:36:35.167 回答