What can you do to a legacy codebase that will have the greatest impact on improving the quality?

前端 未结 11 1200
既然无缘
既然无缘 2021-01-30 06:51

As you work in a legacy codebase what will have the greatest impact over time that will improve the quality of the codebase?

  • Remove unused code
  • Remove dup
相关标签:
11条回答
  • 2021-01-30 07:38

    I can relate to this question as I currently have in my lap one of 'those' old school codebase. Its not really legacy but its certainly not followed the trend of the years.

    I'll tell you the things I would love to fix in it as they bug me every day:

    • Document the input and output variables
    • Refactor the variable names so they actually mean something other and some hungarian notation prefix followed by an acronym of three letters with some obscure meaning. CammelCase is the way to go.
    • I'm scared to death of changing any code as it will affect hundreds of clients that use the software and someone WILL notice even the most obscure side effect. Any repeatable regression tests would be a blessing since there are zero now.

    The rest is really peanuts. These are the main problems with a legacy codebase, they really eat up tons of time.

    0 讨论(0)
  • 2021-01-30 07:38

    Good documentation. As someone who has to maintain and extend legacy code, that is the number one problem. It's difficult, if not downright dangerous to change code you don't understand. Even if you're lucky enough to be handed documented code, how sure are you that the documentation is right? That it covers all of the implicit knowledge of the original author? That it speaks to all of the "tricks" and edge cases?

    Good documentation is what allows those other than the original author to understand, fix, and extend even bad code. I'll take hacked yet well-documented code that I can understand over perfect yet inscrutable code any day of the week.

    0 讨论(0)
  • 2021-01-30 07:39

    I'd say 'remove duplicated code' pretty much means you have to pull code out and abstract it so it can be used in multiple places - this, in theory, makes bugs easier to fix because you only have to fix one piece of code, as opposed to many pieces of code, to fix a bug in it.

    0 讨论(0)
  • 2021-01-30 07:40

    Add unit tests to improve test coverage. Having good test coverage will allow you to refactor and improve functionality without fear.

    There is a good book on this written by the author of CPPUnit, Working Effectively with Legacy Code.

    Adding tests to legacy code is certianly more challenging than creating them from scratch. The most useful concept I've taken away from the book is the notion of "seams", which Feathers defines as

    "a place where you can alter behavior in your program without editing in that place."

    Sometimes its worth refactoring to create seams that will make future testing easier (or possible in the first place.) The google testing blog has several interesting posts on the subject, mostly revolving around the process of Dependency Injection.

    0 讨论(0)
  • 2021-01-30 07:45
    • Read Michael Feather's book "Working effectively with Legacy Code"

    This is a GREAT book.

    If you don't like that answer, then the best advice I can give would be:

    • First, stop making new legacy code[1]

    [1]: Legacy code = code without unit tests and therefore an unknown

    Changing legacy code without an automated test suite in place is dangerous and irresponsible. Without good unit test coverage, you can't possibly know what affect those changes will have. Feathers recommends a "stranglehold" approach where you isolate areas of code you need to change, write some basic tests to verify basic assumptions, make small changes backed by unit tests, and work out from there.

    NOTE: I'm not saying you need to stop everything and spend weeks writing tests for everything. Quite the contrary, just test around the areas you need to test and work out from there.

    Jimmy Bogard and Ray Houston did an interesting screen cast on a subject very similar to this: http://www.lostechies.com/blogs/jimmy_bogard/archive/2008/05/06/pablotv-eliminating-static-dependencies-screencast.aspx

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