命名前缀

本文探讨了匈牙利命名法的历史、原理及在现代编程语言和IDE中的实用性。它通过对比非匈牙利命名法,解释了这种传统命名约定的优点和局限性,并提供了一个代码片段来说明其实际应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

匈牙利命名法是一个命名的变量的类型和/或范围作为变量的命名前缀。比如说呢
1
2
3
4
5
int value; // non-Hungarian
int nValue; // the n prefix denotes an integer
 
double width; // non-Hungarian
double dWidth; // the d prefix denotes a double

Hungarian Notation was invented in 1972 by Charles Simonyi, a Microsoft programmer. The original idea of Hungarian Notation was to encode information about the variable’s purpose, which is known as Apps Hungarian. Over time, this focus changed to encoding information about the variable’s type and/or scope, which is known as Systems Hungarian.

There is a lot of controversy over whether Hungarian Notation is useful in modern programming languages and with modern IDEs. We believe the advantages still outweigh the disadvantages, though you will find plenty of programmers who disagree.

One advantage of Hungarian Notation is that the variable type can be determined from it’s name. Many argue that this is an obsolete advantage, because most modern IDEs will tell you a variables type if you mouse-hover over the name. However, consider the following snippet:

1
float applesPerPerson = totalApples / totalPersons;

匈牙利命名法是在1972发明的查尔斯西蒙尼,一个微软程序员。匈牙利符号最初的想法是编码的可变信息的目的,即应用匈牙利。随着时间的推移,这将改变编码的变量的类型和/或范围的信息,这是被称为系统的匈牙利。

有在是否匈牙利符号在现代编程语言和现代IDE是有用的许多争论。我们相信,优点远远大于它的缺点,但你会发现很多程序员谁不同意。

匈牙利表示法的一个优点是,变量的类型可以从它的名字确定。许多人认为,这是一个过时的优势,因为大多数现代IDE如果你将鼠标悬停在名字告诉你某个变量的类型。然而,考虑下面的代码片段:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值