Do people use the Hungarian Naming Conventions in

2019-01-17 14:27发布

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

20条回答
走好不送
2楼-- · 2019-01-17 15:11

I see Hungarian Notation as a way to circumvent the capacity of our short term memories. According to psychologists, we can store approximately 7 plus-or-minus 2 chunks of information. The extra information added by including a prefix helps us by providing more details about the meaning of an identifier even with no other context. In other words, we can guess what a variable is for without seeing how it is used or declared. This can be avoided by applying oo techniques such as encapsulation and the single responsibility principle.

I'm unaware of whether or not this has been studied empirically. I would hypothesize that the amount of effort increases dramatically when we try to understand classes with more than nine instance variables or methods with more than 9 local variables.

查看更多
放我归山
3楼-- · 2019-01-17 15:12

I think hungarian notation is an interesting footnote along the 'path' to more readable code, and if done properly, is preferable to not-doing it.

In saying that though, I'd rather do away with it, and instead of this:

int vBox = aBottom * lVerticalSide;

write this:

int boxVolume = bottomArea * verticalHeight;

It's 2008. We don't have 80 character fixed width screens anymore!

Also, if you're writing variable names which are much longer than that you should be looking at refactoring into objects or functions anyway.

查看更多
别忘想泡老子
4楼-- · 2019-01-17 15:12

Being a PHP programmer where it's very loosely typed, I don't make a point to use it. However I will occasionally identify something as an array or as an object depending on the size of the system and the scope of the variable.

查看更多
Root(大扎)
5楼-- · 2019-01-17 15:13

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.

查看更多
可以哭但决不认输i
6楼-- · 2019-01-17 15:13

I use Hungarian Naming for UI elements like buttons, textboxes and lables. The main benefit is grouping in the Visual Studio Intellisense Popup. If I want to access my lables, I simply start typing lbl.... and Visual Studio will suggest all my lables, nicley grouped together.

However, after doing more and more Silverlight and WPF stuff, leveraging data binding, I don't even name all my controls anymore, since I don't have to reference them from code-behind (since there really isn't any codebehind anymore ;)

查看更多
趁早两清
7楼-- · 2019-01-17 15:15

It depends on your language and environment. As a rule I wouldn't use it, unless the development environment you're in makes it hard to find the type of the variable.

There's also two different types of Hungarian notation. See Joel's article. I can't find it (his names don't exactly make them easy to find), anyone have a link to the one I mean?

Edit: Wedge has the article I mean in his post.

查看更多
登录 后发表回答