Good Examples of Hungarian Notation? [closed]

2019-01-13 05:10发布

This question is to seek out good examples of Hungarian Notation, so we can bring together a collection of these.

Edit: I agree that Hungarian for types isn't that necessary, I'm hoping for more specific examples where it increases readability and maintainability, like Joel gives in his article (as per my answer).

22条回答
劳资没心,怎么记你
2楼-- · 2019-01-13 05:17

Well, I use it only with window control variables. I use btn_, txt_, lbl_ etc to spot them. I also find it helpful to look up the control's name by typing its type (btn_ etc).

查看更多
贼婆χ
3楼-- · 2019-01-13 05:18

I think the key thing to take away from Joel's article, linked above, and Hungarian Notation in general, is to use it when there's something non-obvious about the variable.

One example, from the article, is encoded vs non encoded strings, it's not that you should use hungarian 'us' for unsafe strings and 's' for safe strings, it's that you should have some identifier to indicate that a string is either safe or not. If it becomes standard, it becomes easy to see when the standard is being broken.

查看更多
爷的心禁止访问
4楼-- · 2019-01-13 05:19

t

Tainted data. Prefix all data incoming from an untrusted source to make that variable as tainted. All tainted data should be cleansed before any real work is done on it.

查看更多
看我几分像从前
5楼-- · 2019-01-13 05:20

I only ever use p for a pointer, and that's it. And that's only if I'm in C++. In C# I don't use any hungarian notation. e.g.

MyClass myClass;
MyClass* pMyClass;

That's all :)

Edit: Oh, I just realised that's a lie. I use "m_" for member variables too. e.g.

class
{
private:
bool m_myVar;
}
查看更多
看我几分像从前
6楼-- · 2019-01-13 05:21

I agree that Hungarian notation is no longer particularly useful. I thought that its original intention was to indicate not datatype, but rather entity type. In a code section involving the names of customers, employees and the user, for example, you could name local string variables cusName, empName and usrName. That would help distinguish among similar-sounding variable names. The same prefixes for the entities would be used throughout the application. However, when OO is used, and you're dealing with objects, those prefixes are redundant in Customer.Name, Employee.Name and User.Name.

查看更多
7楼-- · 2019-01-13 05:21

I was strongly against Hungarian notation until I really started reading about it and trying to understand it's original intent.
After reading Joels post "Wrong" and the article "Rediscovering Hungarian Notation" I really changed my mind. Done correct I belive it must be extremly powerful.

Wrong by Joel Spolsky
http://www.joelonsoftware.com/articles/Wrong.html

Rediscovering Hungarian Notation
http://codingthriller.blogspot.com/2007/11/rediscovering-hungarian-notation.html

I belive that most Naysayers have never tried it for real and do not truly understand it. I would love to try it out in a real project.

查看更多
登录 后发表回答