Best way to get rid of hungarian notation?

前端 未结 20 1148
萌比男神i
萌比男神i 2021-01-18 20:52

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 th

相关标签:
20条回答
  • 2021-01-18 21:16

    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.

    0 讨论(0)
  • 2021-01-18 21:16

    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.

    0 讨论(0)
  • 2021-01-18 21:17

    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.

    0 讨论(0)
  • 2021-01-18 21:19

    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)

    0 讨论(0)
  • 2021-01-18 21:20

    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.

    0 讨论(0)
  • 2021-01-18 21:20

    Use this java tool to remove HN:

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

    0 讨论(0)
提交回复
热议问题