Best way to get rid of hungarian notation?

2020-04-07 04:47发布

Let's say you've inherited a C# codebase that uses one class with 200 static methods to provide core functionality (such as database lookups). Of the many nightmares in that class, there's copious use of Hungarian notation (the bad kind).

Would you refactor the variable names to remove the Hungarian notation, or would you leave them alone?

If you chose to change all the variables to remove Hungarian notation, what would be your method?

20条回答
放我归山
2楼-- · 2020-04-07 05:37

How much are you going to break by doing this? That's an important question to ask yourself. If there are a lot of other pieces of code that use that library, then you might just be creating work for folks (maybe you) by going through the renaming exercise.

I'd put it on the list of things to do when refactoring. At least then everyone expects you to be breaking the library (temporarily).

That said, I totally get frustrated with poorly named methods and variables, so I can relate.

查看更多
仙女界的扛把子
3楼-- · 2020-04-07 05:42

I am actually doing the same thing here for an application extension. My approach has been to use VIM mappings to search for specific Hungarian notation prefixes and then delete them and fix capitalization as appropriate.

Examples (goes in vimrc):

"" Hungarian notation conversion helpers
"" get rid of str prefixes and fix caps e.g. strName -> name
map ,bs /\Wstr[A-Z]^Ml3x~
map ,bi /\Wint[A-Z]^Ml3x~
"" little more complex to clean up m_p type class variables
map ,bm /\Wm_p\?[A-Z]^M:.s/\(\W\)m_p\?/\1_/^M/\W_[A-Z]^Mll~
map ,bp /\Wp[A-Z]^Mlx~
查看更多
该账号已被封号
4楼-- · 2020-04-07 05:43

Refactor -- I find Hungarian notation on that scale really interferes with the natural readability of the code, and the exercise is a good way of getting familiar with what's there.

However, if there are other team members who know the code base you would need consensus on the refactoring, and if any of the variables are exposed outside of the one project then you will have to leave them alone.

查看更多
我命由我不由天
5楼-- · 2020-04-07 05:43

Use this java tool to remove HN:

Or just use "replace"/"replace all" with regex like below to replace "c_strX" to "x": regex to replace HN

查看更多
来,给爷笑一个
6楼-- · 2020-04-07 05:45

I'd say a bigger problem is that you have a single class with 200(!) methods!

If this is a much depended on / much changed class then it might be worth refactoring to make it more usable.

In this, Resharper is an absolute must (you could use the built in refactoring stuff, but Resharper is way better).

Start finding a group of related methods, and then refactor these out into a nice small cohesive class. Update to conform to your latest code standards.

Compile & run your test suite.

Have energy for more? Extract another class.
Worn out - no trouble; come back and do some more tomorrow. In just a few days you'll have conquered the beast.

查看更多
啃猪蹄的小仙女
7楼-- · 2020-04-07 05:46

I agree with @Booji -- do it manually, on a per-routine basis when you're already visiting the code for some other good reason. Then, you'll get the most common ones out of the way, and who cares about the rest.

I was thinking of asking a similar question, only in my case, the offending code is my own. I have a very old habit of using "the bad kind" of Hungarian from my FoxPro days (which had weak typing and unusual scoping) — a habit I've only recently kicked.

It's hard — it means accepting an inconsistent style in your code base. It was only a week ago I finally said "screw it" and began a parameter name without the letter "p". The cognitive dissonance I initially felt has given way to a feeling of liberty. The world did not come to an end.

查看更多
登录 后发表回答