Skip to content

Latest commit

 

History

History
34 lines (30 loc) · 1.98 KB

refactoring-from-good-to-great.md

File metadata and controls

34 lines (30 loc) · 1.98 KB
  1. Temporal variables to private methods
  • Reduces the number of methods
  • We can reuse them
  • Easier to read
  • Hides implementation details
  1. Feature envy (a class inspects other classes). Tell don't ask
  • Move logic to the object
  • Behaviour closer to the data
  1. Data clump (passing a lot of parameters)
  • Better to pass a minimum number parameters as possible
  1. Invert control. Instead of passing params we pass the data clump
  • Reduces coupling
  • Easiert to change
  1. Parameter coupling. Passing and inspecting objects in methods increases coupling, less parameters is better.
  2. Behaviour magnet. Now that we have the appropriate classes, we have a place to put behaviour.
  3. Refactor and improve performance of the classes we have probably with native behaviour
  4. Extract behavior into private methods
  5. Collection as first class citisens so we have a place to put collection behaviour
  6. We do not have all data in objects (null data). Pej: a Job may not have a contact name. We have to check it all over the place. NullContact (Null-Object pattern). NullContact (name and phone). No need for conditions or obfuscation.
  7. Adapter pattern. A user managing payments (Depending on Braintree). Depend upon abstractions. If I want to change the payment provider I have to change a lot of places. Extract it to a PaymentGateway and make the user depend on that abstraction. Easy to mock and test.

When to refactor:

  • All the time. Refactor on green. On code review
  • God objects (like user, the main class of the business journey)
  • High churn files, a file that changes all the time. Do not waste a lot of effort on files that don't change often
  • Bug

Check out for number of lines, there are better ways of measuring complexity

References: