人们在现实世界中使用匈牙利命名约定吗? [英] Do people use the Hungarian Naming Conventions in the real world?

查看:109
本文介绍了人们在现实世界中使用匈牙利命名约定吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

值得学习该约定还是对可读性和可维护性的祸害?

解决方案

考虑到大多数使用匈牙利表示法的人都遵循了被误解的版本,所以我认为这毫无意义.

如果要使用它的原始定义,可能会更有意义,但除此之外,它主要是语法糖.

如果您阅读有关该主题的 Wikipedia文章,则会发现两个相互矛盾的符号, 系统匈牙利表示法 Apps匈牙利表示法.

最初的好定义是 Apps匈牙利符号,但是大多数人使用 Systems匈牙利符号.

作为这两个示例,请考虑在变量前加l表示长度,在a表示面积,在v表示体积.

使用这种表示法,以下表达式是有意义的:

int vBox = aBottom * lVerticalSide;

但这不是:

int aBottom = lSide1;

如果您要混合使用前缀,则它们将被视为等式的一部分,并且volume = area * length对于一个盒子来说不错,但是将length值复制到area变量中会引起一些危险./p>

不幸的是,其他表示法用处不大,因为人们在变量名前加上值的类型,例如:

int iLength;
int iVolume;
int iArea;

有些人将n用作数字,将i用作整数,将f用作浮点数,将s用作字符串等.

最初的前缀原意用于发现方程式中的问题,但是由于不必去寻找变量声明,因此已经以某种方式使代码更易于阅读.在当今的智能编辑器中,您可以将鼠标悬停在任何变量上以查找完整类型,而不仅仅是它的缩写,这种匈牙利符号已经失去了很多含义.

但是,您应该下定决心.我只能说我都不用.


编辑只是要添加一个简短的通知,虽然我不使用匈牙利表示法,但我使用的是前缀,这是下划线.我在类的所有私有字段前加上_,否则将其名称拼写为属性,即首字母大写的首字母大写.

Is it worth learning the convention or is it a bane to readability and maintainability?

解决方案

Considering that most people that use Hungarian Notation is following the misunderstood version of it, I'd say it's pretty pointless.

If you want to use the original definition of it, it might make more sense, but other than that it is mostly syntactic sugar.

If you read the Wikipedia article on the subject, you'll find two conflicting notations, Systems Hungarian Notation and Apps Hungarian Notation.

The original, good, definition is the Apps Hungarian Notation, but most people use the Systems Hungarian Notation.

As an example of the two, consider prefixing variables with l for length, a for area and v for volume.

With such notation, the following expression makes sense:

int vBox = aBottom * lVerticalSide;

but this doesn't:

int aBottom = lSide1;

If you're mixing the prefixes, they're to be considered part of the equation, and volume = area * length is fine for a box, but copying a length value into an area variable should raise some red flags.

Unfortunately, the other notation is less useful, where people prefix the variable names with the type of the value, like this:

int iLength;
int iVolume;
int iArea;

some people use n for number, or i for integer, f for float, s for string etc.

The original prefix was meant to be used to spot problems in equations, but has somehow devolved into making the code slightly easier to read since you don't have to go look for the variable declaration. With todays smart editors where you can simply hover over any variable to find the full type, and not just an abbreviation for it, this type of hungarian notation has lost a lot of its meaning.

But, you should make up your own mind. All I can say is that I don't use either.


Edit Just to add a short notice, while I don't use Hungarian Notation, I do use a prefix, and it's the underscore. I prefix all private fields of classes with a _ and otherwise spell their names as I would a property, titlecase with the first letter uppercase.

这篇关于人们在现实世界中使用匈牙利命名约定吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆