Best way to get rid of hungarian notation?

前端 未结 20 1147
萌比男神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:33

    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.

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

    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.

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