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

2,897 views

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.
http://tungnt.net/meetup-ve-cac-ky-thuat-refactoring-code-trong-net/

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: http://tungnt.net • Facebook Page: https://www.facebook.com/tungnt185 • Meetup group: http://www.meetup.com/Ha-Noi-NET-Meetup/
  3. 3. http://tungnt.net 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. http://tungnt.net 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. http://tungnt.net Who will refactor code? • You - Developers
  7. 7. http://tungnt.net 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. http://tungnt.net 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. http://tungnt.net 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. http://tungnt.net Naming • Should be proper, descriptive and consistent for readability and maintainability
  12. 12. http://tungnt.net 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. http://tungnt.net Long Method Extract method to small methods are always better (easy naming, understanding, less duplicate code)
  14. 14. http://tungnt.net Conditional complexity • Extract method
  15. 15. http://tungnt.net Loosely Couple Code • Extract Interface, Extract factory for extensibility
  16. 16. http://tungnt.net Return value instead of exception • Increase performance and readability
  17. 17. http://tungnt.net Switch statement • Can be replaced with polymorphism for extensibility
  18. 18. http://tungnt.net Remove multiple if • Return ASAP (as soon as possible) For readability
  19. 19. http://tungnt.net Parameter object • For readability and extensibility
  20. 20. http://tungnt.net References
  21. 21. http://tungnt.net References • “Refactoring: Improving the Quality of Existing Code“ by Telerik Academy • 31 days of refactoring - lostechies.com
  22. 22. http://tungnt.net Q&A

×