Best way to get rid of Hungarian notation?

Suppose you have inherited a C # database that uses one class with 200 static methods to provide basic functions (such as finding a database). Of the many nightmares in this class, the abundant use of Hungarian notation (poor look).

Could you reorganize the variable names to remove the Hungarian notation, or leave them alone?

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

+5
coding-style refactoring hungarian-notation
source share
19 answers

Just leave it alone. The best use of your time.

+16
source share

Refactor - I believe that Hungarian notation on this scale really interferes with the natural readability of the code, and exercise is a good way to get to know what is there.

However, if there are other team members who know the code base, you need consensus regarding refactoring, and if any of the variables is set outside the same project, you will have to leave them alone.

+16
source share

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

There are VS add-ons that also do this, but the built-in method works fine for me.

+14
source share

What should I do? Assuming I just need to maintain the code and not overwrite it in any significant way? Leave it alone. And when I add the code, move on to the existing style, i.e. use this ugly Hungarian notation (as dirty as I am.)

But, uh, if you really have a hankerin 'fer refactorin', then just do it a little at a time. Each time you work on this, spend ten minutes renaming the variables. Tidying things up a bit. After a few months, you may find it as clean as a whistle ....

+4
source share

Do not forget that there are two types of Hungarian designations.

The original Charles Simoni HN, later known as the App Hungarian, and the later abomination, called the System Hungarian after some peckerhead (this is a technical term), completely misunderstood the Simonyi original paper .

Unfortunately, the HN system was spread by Petzold and others to become the more dominant abortion that is rightfully considered today.

Read Joel's excellent article about the intent of the original Hungarian novelty Apps and you will regret being lost in a hurry.

If you have a Hungarian application, you probably want to keep it after reading both the original article by Charles Simony and the article by Joel.

Should you land in a smoking heap of System Hungarian?

All bets are off!

Phew! (said holding his nose) (-:

+3
source share

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

+2
source share

I used it religiously in the days of VB6, but stopped when VB.NET came out because it was said in the new VB recommendations. Other developers did not. So, we have a lot of old code with him. When I perform code maintenance, I remove the notation from the functions / methods / sub that I touch. I would not delete all this at once if you do not have really good unit tests for everything and you can run them to prove that nothing is broken.

+2
source share

How much are you going to break by doing this? This is an important question to ask yourself. If there are many other pieces of code that use this library, you can simply create work for people (maybe you) by doing the renaming exercise.

I would put it on the list of things to do when refactoring. At least everyone expects you to break the library (temporarily).

However, I am completely disappointed with the poorly named methods and variables, so I can tell.

+2
source share

I would not have made a project out of it. I would use refactoring tools in VS (in fact, I would use Resharper, but VS worked just fine) and fix all the variables in any method that I was called to modify. Or if I had to make larger changes, I would reorganize the variable names in any method that I was called to to understand.

+1
source share

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

However, I agree with Chris Conway in a certain point of view and ask you, WHY, yes, this is annoying, but at the same time many times "if it has not broken," "the method is really the best way!"

+1
source share

Change it only for direct use. And make sure you have a testbench ready for use to make sure it still works.

+1
source share

I agree that the best way to phase out Hungarian notation is to refactor the code when it changes. The biggest advantage of this refactoring is that you have to write unit tests around the code that you modify so that you have a security network instead of crossing your fingers and hoping that you don't break existing functions. Once you have completed these unit tests, you can change the code to your hearty content.

+1
source share

I would say that the big problem is that you have one class with 200 (!) Methods!

If this depends heavily on / a lot of the changed class, then refactoring may be required to make it more convenient.

In this, Resharper is an absolute must (you can use built-in refactoring, but Resharper is better).

Start by finding a group of related methods, and then reformat them into a nice little cohesive class. Update to meet the latest code standards.

Compile and run the test package.

Is there energy for more? Retrieve another class.
Exhausted - no problem; come back and do it tomorrow. In just a few days you will defeat the beast.

+1
source share

I agree with @Booji - do it manually, on the principle of a subroutine, when you are already visiting the code for some other good reason. Then you will get the most common ones, and who cares about everything else.

I was thinking of asking a similar question, only in my case, my violating code. I have a very old habit of using the โ€œbad lookโ€ of the Hungarian from my days FoxPro (who had a weak typing and unusual scope) - a habit that I just kicked recently.

This is complicated - it means adopting an inconsistent style in the code base. Just a week ago, I finally said โ€œscrew it upโ€ and started the parameter name without the letter โ€œpโ€. The cognitive dissonance that I initially felt gave way to a sense of freedom. The world has not come to an end.

+1
source share

The way I dealt with this problem is to change one variable at a time when I encounter it, and then make more radical changes when you return to make deeper changes. If you are something like me, the different nomenclature of your variables will show you a bit-shit for a while, but you will gradually get used to it. The key is to drop it a bit until you have everything where it should be.

Alternatively, you can completely throw away your variables and simply return any function 42.

+1
source share

It seems to me that the big problem is that the God Object class. I would suggest that refactoring just to remove Hungarian notation is a low-value, high-risk activity in itself. Unless there are a lot of automated unit test suites in this class to give you some confidence in your refactoring, I think you should leave it well and truly alone.

I suppose it is unlikely that such a test suite exists, because the developer, following the TDD methods (naturally, avoided building a god object in the first place), it would be very difficult to write comprehensive tests.

Exclusion of the god object and getting the unit test base in place is of higher value. My advice would be to look for ways to refactor the class itself - perhaps when a suitable business requirement / change arises that requires changing this code (and therefore, we hope that with the purchase and payment of some system and regression tests). You may not be able to justify the refactoring efforts in one go, but you can do it in parts, as the opportunity arises, and test drive the changes. Thus, you can gradually convert the spaghetti code into a clean code base with complete unit tests in stages.

And you can eliminate the Hungarian whenever you want, if you want.

0
source share

I really do the same here to expand the application. My approach was to use VIM mappings to find specific prefixes of Hungarian designations, and then remove them and, if necessary, correct the capitalization.

Examples (goes to vimrc):

"" Hungarian notation conversion helpers "" get rid of str prefixes and fix caps eg strName -> name map ,bs /\Wstr[AZ]^Ml3x~ map ,bi /\Wint[AZ]^Ml3x~ "" little more complex to clean up m_p type class variables map ,bm /\Wm_p\?[AZ]^M:.s/\(\W\)m_p\?/\1_/^M/\W_[AZ]^Mll~ map ,bp /\Wp[AZ]^Mlx~ 
0
source share

If you are going to break the code just for the sake of refactoring, I would seriously think about leaving me alone, especially if you are going to influence other people in your team, which may depend on this code.

If your team is okay with this refactoring and invests your time in it (which may be a saving time in the future, if it means that the code is more readable / supported), use Visual Studio (or something else you use IDE) to help you reorganize the code.

However, if such a major change is not a risk to your team / boss, I would suggest a somewhat unorthodox approach halfway. Instead of doing all your refactoring in one mode, why not refactor sections of code (more precisely, functions) that need to be touched during normal maintenance? Over time, this slow refactoring will bring the code to a cleaner state, after which you can complete the refactoring process with the final scan.

0
source share

I love Hungarian notation. I donโ€™t understand why you want to get rid of it.

-one
source share

All Articles