Best way to get rid of hungarian notation?
Asked Answered
J

20

4

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?

Jackpot answered 13/10, 2008 at 19:59 Comment(2)
A class with 200 static methods? I think that you've got more problems than Hungarian notation. ;-)Pliant
Write a tool/plugin and share it on github. It does not have to be all corrrect, just a helper. Or simply use replace. Or just use replace.Hypothetical
S
18

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.

Salter answered 13/10, 2008 at 20:4 Comment(1)
Absolutely correct on the team member issue. Otherwise you may find that you spend a month removing the Hungarian but a month later it all comes back!Ecclesiastic
C
17

Just leave it alone. There are better uses of your time.

Cyna answered 13/10, 2008 at 20:1 Comment(6)
How do you know what else he could be doing with his time? Do you know him or his project personally? Or how bad the readability is affected in this case?Alit
1) His time may be worthless. That's true. 2) No, I don't. 3) If it's just "Hungarian notation" (as the question says) and not "Hungarian notation combined with awful, inscrutable, and often misleading choices in variable names," then it can't be that bad.Cyna
Alan: Cost/benefit analysis time. How much time does it cost to refactor that mess, then retest it all, just to clean up variable names? If the code is functional, then leave well enough alone on the variable names and focus on the bigger bang-for-the-buck refactorings.Typhoid
I second that. What kind of brain would produce C# code with hungarian notation? Just leave it alone.Operculum
@Marko - I wish I knew the answer to that question. :(Jackpot
@Marko Dumic - I'm guessing, hungarian kind :)Sideband
J
14

Right click on the variable name, Refactor -> Rename.

There are VS add-ins that do this as well, but the built-in method works fine for me.

Jake answered 13/10, 2008 at 20:1 Comment(1)
I would recommend doing this over time, refactoring as you maintain the code-base.Medium
G
4

What would I do? Assuming that I just have to maintain the code and not rewrite it any significant way? Leave it well alone. And When I do add code, go with the existing style, meaning, use that ugly Hungarian notation (as dirty as that makes me feel.)

But, hey, if you really have a hankerin' fer refactorin' then just do a little at a time. Every time you work on it spend ten minutes renaming variables. Tidying things up a little. After a few months you might find it's clean as a whistle....

Guard answered 13/10, 2008 at 20:7 Comment(1)
I've been in this spot. We actually have comments on some projects we maintain: "We wouldn't write it this way, but we made the decision to be consistent with bad style to ease permanent maintenance." Dirty it may be, but sometimes it's the right choice.Typhoid
P
4

Don't forget that there are two kinds of Hungarian Notation.

The original Charles Simonyi HN, later known as App's Hungarian and the later abomination called System Hungarian after some peckerhead (it's a technical term) totally misread Simonyi's original paper.

Unfortunately, System HN was propagated by Petzold and others to become the more dominant abortion that it is rightfully recognised as today.

Read Joel's excellent article about the intent of the original Apps Hungarian Notation and be sorry for what got lost in the rush.

If what you've got is App's Hungarian you will probably want to keep it after reading both the original Charles Simonyi article and the Joel article.

If you've landed in a steaming pile of System Hungarian?

All bets are off!

Whew! (said while holding nose) (-:

Pliant answered 13/10, 2008 at 21:19 Comment(2)
Note that I said "the bad kind" in my question.Jackpot
I saw that and wasn't sure what you intended. Maybe edit to refer to Sys. HN and also add a link to Joel's article? Bad luck on inheriting the Sys HN BTW! (-:Pliant
M
2

if you're feeling lucky and just want the Hungarian to go away, isolate the Hungarian prefixes that are used and try a search and replace in file to replace them with nothing, then do a clean and rebuild. If the number of errors is small, just fix it. If the number of errors is huge, go back and break it up into logical (by domain) classes first, then rename individually (the IDE will help)

Macklin answered 13/10, 2008 at 20:4 Comment(2)
Bold suggestion! Better have a damn good suite of unit tests for that one.Alit
@[Alan Hensel]: it's a sanity-check ;-) If the names are unique without the hungarian prefixes, everything will be fine; if not, the compiler will complain...a lot! and an alternate route should be taken.Macklin
C
2

I used to use it religiously back in the VB6 days, but stopped when VB.NET came out because that's what the new VB guidelines said. Other developers didn't. So, we’ve got a lot of old code with it. When I do maintenance on code I remove the notation from the functions/methods/sub I touch. I wouldn't remove it all at once unless you've got really good unit tests for everything and can run them to prove that nothing's broken.

Conventioner answered 13/10, 2008 at 20:7 Comment(0)
W
2

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.

Wagoner answered 13/10, 2008 at 20:9 Comment(0)
F
1

I wouldn't make a project out of it. I'd use the refactoring tools in VS (actually, I'd use Resharper's, but VS's work just fine) and fix all the variables in any method I was called upon to modify. Or if I had to make larger-scale changes, I'd refactor the variable names in any method I was called upon to understand.

Fulvia answered 13/10, 2008 at 20:4 Comment(0)
M
1

If you have a legitimate need to remove and change it I would use either the built in refactoring tools, or something like Resharper.

However, I would agree with Chris Conway to a certain standpoint and ask you WHY, yes, it is annoying, but at the same time, a lot of the time the "if it aint't broke done't fix it" method is really the best way to go!

Million answered 13/10, 2008 at 20:6 Comment(0)
F
1

Only change it when you directly use it. And make sure you have a testbench ready to apply to ensure it still works.

Fusiform answered 13/10, 2008 at 20:11 Comment(0)
J
1

I agree that the best way to phase out hungarian notation is to refactor code as you modify it. The greatest benefit of doing this kind of refactoring is that you should be writing unit tests around the code you're modifying so that you have a safety net instead of crossing your fingers and hoping that you don't break existing functionality. Once you have these unit tests in place, you are free to change the code to your heart's content.

Journalist answered 13/10, 2008 at 20:46 Comment(0)
N
1

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.

Nearby answered 13/10, 2008 at 20:53 Comment(2)
Refactoring is another issue altogether.Jackpot
No it's not. "Smart variable renaming" is a very common refactoring task.Tsarevna
W
1

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.

Whirlabout answered 13/10, 2008 at 21:0 Comment(0)
E
1

The way I've been going about this problem is changing one variable at a time as I come across them, then perform more sweeping changes when you come back to do more in-depth changes. If you're anything like me, the different nomenclature of your variables will drive you bat-shiat crazy for a while, but you'll slowly become used to it. The key is to chip away at it a little bit at a time until you have everything to where it needs to be.

Alternatively, you could jettison your variables altogether and just have every function return 42.

Emerick answered 13/10, 2008 at 21:49 Comment(0)
P
0

It sounds to me like the bigger problem is that 200-method God Object class. I'd suggest that refactoring just to remove the Hungarian notation is a low-value, high-risk activity in of itself. Unless there's a copious set of automated unit tests around that class to give you some confidence in your refactoring, I think you should leave it well and truly alone.

I guess it's unlikely that such a set of tests exists, because a developer following TDD practices would (hopefully) have naturally avoided building a god object in the first place - it would be very difficult to write comprehensive tests for.

Eliminating the god object and getting a unit test base in place is of higher value, however. My advice would be to look for opportunities to refactor the class itself - perhaps when a suitable business requirement/change comes along that necessitates a change to that code (and thus hopefully comes with some system & regression testing bought and paid for). You might not be able to justify the effort of refactoring the whole thing in one go, but you can do it piece by piece as the opportunity comes along, and test-drive the changes. In this way you can slowly convert the spaghetti code into a cleaner code base with comprehensive unit tests, bit by bit.

And you can eliminate the Hungarian as you go, if you like.

Perplex answered 13/10, 2008 at 21:26 Comment(0)
T
0

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~
Targum answered 25/11, 2009 at 15:55 Comment(0)
D
0

If you're gonna break code just for the sake of refactoring, I would seriously consider leaving i alone, specially, if you are going to affect other people in your team who may be depending on that code.

If your team is OK with this refactoring, and investing your time in doing this (which may be a time-saver in the future, if it means the code is more readable/maintainable), use Visual Studio (or whatever IDE you are using) to help you refactor the code.

However, if a big change like this is not a risk your team/boss is willing to take, I would suggest a somewhat unorthodox, half-way approach. Instead of doing all your refactoring in a single sweep, why not refactor sections of code (more specifically, functions) that need to be touched during normal maintenance? Over time, this slow refactoring will bring the code up to a cleaner state, at which point you can finish the refactoring process with a final sweep.

Disapproval answered 1/6, 2011 at 20:15 Comment(0)
H
0

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

Hypothetical answered 16/12, 2019 at 1:48 Comment(0)
C
-1

I love Hungarian notation. Don't understand why you would want to get rid of it.

Charlet answered 13/10, 2008 at 20:39 Comment(1)
The problems with hungarian notation are when you need to change a variable's data type the notation often won't reflect the change out of fear, etc. It does tend to hurt readability when you are try to reada lot of code. Modern IDE's w/ tooltips, etc tend to make it unnessary anyways.Conventioner

© 2022 - 2024 — McMap. All rights reserved.