您是否使用匈牙利表示法作为控件名称?

发布于 2024-09-29 01:46:41 字数 208 浏览 7 评论 0原文

我完全同意我们不应该使用匈牙利表示法来命名变量。但看起来匈牙利表示法对于命名控件(尤其是Winform控件)还是很有用的。考虑这些:

GridView grvUsers
TextBox txtPassword

等等...

我真的怀疑在这种情况下我们是否应该避免使用匈牙利表示法?如果应该,名称控件的替代解决方案是什么?

I totally agree that we should not use Hungarian Notation to name variables. But it seems that Hungarian Notation is still useful to name controls (especially Winform controls). Consider these:

GridView grvUsers
TextBox txtPassword

etc...

I really doubt that should we avoid Hungarian notation in this case? If should, which is alternative solution to name controls?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

他夏了夏天 2024-10-06 01:46:41

不管是对还是错,使用匈牙利表示法来命名控件仍然是事实上的标准,不采用它似乎很不合适。就像 .NET 语言中的方法使用 Pascal 大小写(而在大多数其他语言中,它是不受欢迎的)一样,超出您所工作的环境的公认约定往往会使您的代码看起来更加出众 -异地。

我个人赞成这种做法,因为它有助于区分属于用户界面(视图)的类成员和属于代码隐藏(模型/控制器)的成员。如果控制变量的名称与用于存储数据、状态等的名称类似,那么我觉得很难抗拒将两者紧密耦合的诱惑。当然,更清晰的逻辑分离也可以克服这个问题。

尽管如此,匈牙利表示法毫无疑问地明确了哪些变量是用户界面的一部分,并且在设计器和代码编辑器中也明确了它们的类型和功能。

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.

荭秂 2024-10-06 01:46:41

我通常根据控件的内容来命名它们,但使用更多的英文名称。例如,我将名字框的标签控件命名为“FirstNameLabel”,并将文本框命名为“FirstNameBox”。这甚至不是故意的;我只是注意到有一天我正在这样做,并且继续这样做是有意义的。

我想我会称之为“美国符号”。

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".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文