Refactoring Code: When to do what?

前端 未结 10 2014
情深已故
情深已故 2020-12-28 16:42

Ever since I started using .NET, I\'ve just been creating Helper classes or Partial classes to keep code located and contained in their own little containers, etc.

相关标签:
10条回答
  • 2020-12-28 17:40
    • Re-factor you code when it is causing problems. Any problems will do: performance, scallabillity, integration, maintainance - anything which makes you spend more time on it when you should. It it is not broken do not fix it even if you do not believe it is clean or is up to the modern standards.
    • Don't spend too much time making the code perfect. You will never achieve perfection but you could spend lots of time trying to do so. Remember the law of diminishing returns.
    • Inside a project only re-factor the code when you are actually working on the functionality which depends on it. I.e. if you have a user story for the iteration calls for a "change the upload mechanism" or "fixing the bug in the file upload" you could re-factor the file uploading code. However if your user story is about "facelifting the file upload UI design" do not go into the business logic.
    0 讨论(0)
  • 2020-12-28 17:42

    I just got a copy of Code Complete, and found that there was a section on this.

    Although I will still be reading the accepted answer's book, what Code Complete has taught me has dramatically improved the way I think about designing classes.

    Before today, I didn't know what an ADT was (abstract data type), and now I know how to develop classes adhering to the encapsulation.

    0 讨论(0)
  • 2020-12-28 17:43

    Working Effectively with Legacy Code is one of the best books I have seen on this subject.

    Don't be put off the title of the book - Rather than treating Refactoring as a formal concept (which has its place), this book has lots and lots of simple "why didn't I think of that" tips. Things like "go through a class and remove any methods not directly realted to that class and put them in a different one".

    e.g. You have a grid and some code to persist the layout of that grid to file. You can probably safely move the layout persisting code out to a different class.

    0 讨论(0)
  • 2020-12-28 17:44

    A real eye-opener to me was Refactoring: Improving the Design of Existing Code:

    With proper training a skilled system designer can take a bad design and rework it into well-designed, robust code. In this book, Martin Fowler shows you where opportunities for refactoring typically can be found, and how to go about reworking a bad design into a good one.

    Refactoring http://ecx.images-amazon.com/images/I/519XT0DER6L._SL160_PIlitb-dp-arrow,TopRight,21,-23_SH30_OU01_AA115_.jpg

    It helped me to efficiently and systematically refactor code. Also it helped me a lot in discussions with other developers, when their holy code has to be changed ...

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