Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to refactor legacy code effectively and efficiently? [duplicate]

Possible Duplicates:
What should I keep in mind in order to refactor huge code base?
When is it good (if ever) to scrap production code and start over?

I am currently working with some legacy source code files. They have quite a few problems because they were written by a database expert who does not know much about Java. For instance,

  1. Fields in classes are public. No getters and setters.
  2. Use raw types, not parameterized types.
  3. Use static unnecessarily.
  4. Super long method names.
  5. Methods need too many parameters.
  6. Repeat Yourself frequently.

I want to modify them so that they are more object-oriented. What are some best practices and effective/efficient approaches?

like image 415
zihaoyu Avatar asked Sep 09 '10 22:09

zihaoyu


People also ask

Should I refactor legacy code?

Legacy code refactoring is an excellent option for businesses that want to clean up their legacy software, making it easy to understand. It also makes the application easy to manage and improves its scalability.

What is the best approach to ensure that a code fix doesn't break?

Refactoring your code makes it easier to read and maintain. Refactoring doesn't change the external functionality of your code; it changes the way the code achieves that functionality.

Does refactoring improve performance?

Refactoring may lessen performance, but the change may be negligible. You need to balance the change in the maintainability of the code vs. the change in its performance.


1 Answers

Read "Working Effectively with Legacy Code" by Michael Feathers. Great book - and obviously it'll be a lot more detailed than answers here. It's got lots of techniques for handling things sensibly.

It looks like you've identified a number of issues, which is a large part of the problem. Many of those sound like they can be fixed relatively easily - it's overall design and architecture which is harder to do, of course.

Are there already unit tests, or will you be adding those too?

like image 116
Jon Skeet Avatar answered Nov 14 '22 21:11

Jon Skeet