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条回答
Viruses.
2楼-- · 2020-02-19 04:34

Are variable prefixes ( Hungarian ) really necessary anymore?

NO!

In fact, Microsoft's own style guidelines (where the practice originated) now recommend against it. In particular, see the section on General Naming Conventions, which includes the following text (in bold type, no less):

Do not use Hungarian notation.

查看更多
放我归山
3楼-- · 2020-02-19 04:34

I only use it in my database (PostgreSQL)

t_ for table name
v_ for view name
i_ for index name
trig_ for trigger


sp_ for stored procedure name
p_ for parameter    
v_ for variable
c_ for cursor
r_ for record
查看更多
何必那么认真
4楼-- · 2020-02-19 04:35

If Hungarian means "prefix with a type abbreviation" such as uCount or pchzName, then I would say this practice is bad and thankfully seems to be fading from common use.

However, I do still think that prefixes are very useful for scope. At my studio we use this convention for prefixing variables :

i_  // input-only function parameter (most are these)
o_  // output-only function parameter (so a non-const & or * type)
io_ // bidirectional func param
_   // private member var (c#)
m_  // private member var (c++)
s_  // static member var (c++)
g_  // global (rare, typically a singleton accessor macro)

I've found this to be very useful. The func param prefixes in particular are useful. Way down inside a function you can always tell where that var came from at a glance. And typically we will copy the var to another when we want to modify it or change its meaning.

So in short: prefixes for types are unnecessary with modern tools. The IDE takes care of identifying and checking that stuff for you. But scope-based prefixes are very useful for readability and clarity.

There are also fun side benefits like with Intellisense. You can type i_ ctrl-space and get all the input params to the func to choose from. Or g_ ctrl-space to get all your singletons. It's a time-saver.

查看更多
Lonely孤独者°
5楼-- · 2020-02-19 04:35

No, if your methods are so long that you cant read the definition at the same time as the use, or the name doesnt imply the type then you have more serious design issues than just naming.

However, i INSIST that you do prefix controls with their type, such as txtName.

查看更多
啃猪蹄的小仙女
6楼-- · 2020-02-19 04:37

Yes, if Hungarian notation were used as it was originally meant, instead of as implemented by Microsoft. Much like the example above, which shows text boxes and corresponding labels as lblWhatever, txtWhatever. Use it to define the use of the variable, not the type. It can provide information to know that your number is moneyTotal, which tells me more than just the data type.

But, as commonly used? No.

查看更多
何必那么认真
7楼-- · 2020-02-19 04:40

A solution to a problem that doesn't exist.

查看更多
登录 后发表回答