Are variable prefixes (“Hungarian notation”) reall

2020-02-19 04:16发布

Since C# is strongly typed, do we really need to prefix variables anymore?

e.g.

iUserAge
iCounter
strUsername

I used to prefix in the past, but going forward I don't see any benefit.

29条回答
爱情/是我丢掉的垃圾
2楼-- · 2020-02-19 04:49

Well, to counter, I'd say - it depends. I'm personally against them, but it comes down to your team. Each team should be responsible for developing their own set of guidelines. Hopefully that doesn't include so-called Hungarian Notation, but it really should be a team decision. You might find cases where you want to break with the style guidelines.

查看更多
\"骚年 ilove
3楼-- · 2020-02-19 04:49

What makes me curious about this question is the fact that the languages (C then C++) where prefixing (i.e., Hungarian notation) was introduced were also strongly-typed. For all I know, it was done with Pascal's use at Microsoft as well. And it would seem that it was also used with Mesa, a strongly-typed language that the Hungarian may have had some familiarity with [;<).

That being the case, it is fair to get beneath the question and consider (1) what problem was prefixing used to help solve and (2) how has that problem gone away?

I know that's not exactly an answer, but it might be more useful than the blanket objections to use of prefixes as outmoded or wrong-headed.

查看更多
地球回转人心会变
4楼-- · 2020-02-19 04:50

I always prefix instance member variables with m__ and static member variables with s_. I've come across variable articles from MS people who recommend/discourage the approach.

I'm also pretty sure I've come across the m_ prefix when looking through the standard .NET libraries using Reflector...

查看更多
Summer. ? 凉城
5楼-- · 2020-02-19 04:51

I personally don't anymore, however you will still people argue to prefix for scope.

I go with Microsoft and use their Capitalization Styles for all naming conventions. The entire "Design Guidelines for Class Library Developers" section, which that link is a part of, is pure gold, in my opinion.

Additionally, I love the "Framework Design Guidelines" book from Addison Wesley. It covers all of these guidelines with annotations from Microsoft team members on to why they recommend what they are proposing and how it is adopted within the organization.

查看更多
Evening l夕情丶
6楼-- · 2020-02-19 04:52

Nope. Did we ever need to?

查看更多
倾城 Initia
7楼-- · 2020-02-19 04:52

Hungarian notation is ugly. The only exception is with interfaces, where most people think it's acceptable.

Linus sums it up well: "Encoding the type of a function into the name (so-called Hungarian notation) is brain damaged - the compiler knows the types anyway and can check those, and it only confuses the programmer"

查看更多
登录 后发表回答