views:

1054

answers:

18

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?

+13  A: 

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.

Tom Ritter
I would recommend doing this over time, refactoring as you maintain the code-base.
Jared
+15  A: 

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

Chris Conway
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?
Alan Hensel
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.
Chris Conway
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.
John Rudy
I second that. What kind of brain would produce C# code with hungarian notation? Just leave it alone.
Marko Dumic
@Marko - I wish I knew the answer to that question. :(
Robert S.
@Marko Dumic - I'm guessing, hungarian kind :)
ldigas
+1  A: 

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.

Robert Rossney
+2  A: 

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)

Steven A. Lowe
Bold suggestion! Better have a damn good suite of unit tests for that one.
Alan Hensel
@[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.
Steven A. Lowe
+13  A: 

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.

Rob Walker
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!
BlackWasp
+1  A: 

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!

Mitchel Sellers
+2  A: 

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.

Booji Boy
+4  A: 

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....

MDCore
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.
John Rudy
+2  A: 

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.

itsmatt
+1  A: 

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

Paul Nathan
A: 

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

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.
Booji Boy
+1  A: 

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.

IAmCodeMonkey
+1  A: 

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.

David Laing
Refactoring is another issue altogether.
Robert S.
No it's not. "Smart variable renaming" is a very common refactoring task.
bzlm
+1  A: 

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.

harpo
+1  A: 

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) (-:

Rob Wells
Note that I said "the bad kind" in my question.
Robert S.
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! (-:
Rob Wells
A: 

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.

sgreeve
+1  A: 

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.

Lieutenant Frost
A: 

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~
James Henderson