Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Refactoring code in .net


Published on

Refactoring code in .net - Các kĩ thuật Refactoring code trong .NET. Đây là một kỹ thuật không thể thiếu nếu các bạn muốn các dòng code của mình ngày một trong sáng, dễ hiểu hơn, clean hơn. Đặc biệt đây là một kỹ thuật không thể thiếu nếu bạn muốn áp dụng Agile/Extreme Programming với Unit Test.

Published in: Technology
  • Be the first to comment

Refactoring code in .net

  1. 1. Refactoring Code
  2. 2. About me • Nguyen Thanh Tung • Software architect at MISA JSC • Microsoft Certified Professional Developer • Technologies: • Windows/Windows Phone • ASP.NET • XAML/Silverlight • Windows Forms • … • Blog at: • Facebook Page: • Meetup group:
  3. 3. Table of contents • Refactoring General – What is refactoring? – Who will refactor code? – Why we need refactor code? – When to refactor code? – Where to refactor code? • How to refactor code?
  4. 4. Refactoring General
  5. 5. What is refactoring? Refactoring means "to improve the design and quality of existing source code without changing its external behavior". Martin Fowler Bad Code Good CodeRefactoring
  6. 6. Who will refactor code? • You - Developers
  7. 7. Why we need refactor code? • Code constantly changes and its quality constantly degrades (unless refactored) • Requirements often change and code needs to be changed to follow them
  8. 8. When to refactor code? • Bad smells in the code indicate need of refactoring • Refactor when you – Add a new method or class – Fix an existing bug – Reviewing someone else’s code – Implement TDD (Test-Driven-Development) • Unit tests guarantee that refactoring does not change the behavior – If there are no unit tests, write them
  9. 9. Where to refactor code? • Code smells - certain code structures have possibility of refactoring • Types of code smells: • Duplicated code • Long method • Large class • Long parameter list • Switch statements • Improper naming • Data members are public • …
  10. 10. How to Refactor code?
  11. 11. Naming • Should be proper, descriptive and consistent for readability and maintainability
  12. 12. Duplicated code • Violates the DRY (Don’t repeat yourself) principle • Result of copy-pasted code • Solutions: Extract method, extract class, pull-up method for maintainability
  13. 13. Long Method Extract method to small methods are always better (easy naming, understanding, less duplicate code)
  14. 14. Conditional complexity • Extract method
  15. 15. Loosely Couple Code • Extract Interface, Extract factory for extensibility
  16. 16. Return value instead of exception • Increase performance and readability
  17. 17. Switch statement • Can be replaced with polymorphism for extensibility
  18. 18. Remove multiple if • Return ASAP (as soon as possible) For readability
  19. 19. Parameter object • For readability and extensibility
  20. 20. References
  21. 21. References • “Refactoring: Improving the Quality of Existing Code“ by Telerik Academy • 31 days of refactoring -
  22. 22. Q&A