Refactoring

A talk in which a software developer may learn how to possibly modify code in a way that makes it simple and readable and maintainable and not overly verbose or unnecessarily complicated but still has the same functional attributes of the old code, which is a good thing.

Mike Sherov

Sr. Person who is a Principal in Engineering

Text

Refactoring

A talk in which a software developer may learn how to possibly modify code in a way that makes it simple and readable and maintainable and not overly verbose or unnecessarily complicated but still has the same functional attributes of the old code, which is a good thing.

 

Mike Sherov

Sr. Person who is a Principal in Engineering

Part of the CoreUX Team @ Skillshare

Refactoring

how to possibly improve modify code in a way that makes it simple and readable and maintainable and not overly verbose or unnecessarily complicated but still has keep the same functional attributes of the old code.

Mike Sherov

Principal in Engineering

CoreUX Team @ Skillshare

Refactoring

how to possibly improve the design of existing code but keep the same functional attributes of the old code.

Mike Sherov

Principal Engineer

CoreUX Team @ Skillshare

Refactoring

how to possibly improve improving the design of existing code

Mike Sherov

Principal Engineer

CoreUX Team @ Skillshare

Refactoring

Improving the Design of Existing Code

Mike Sherov

Principal Engineer

Core UX Team @ Skillshare

History

Personal History

  • Learned how to modify legacy systems

  • Learned how to recognize what's missing

  • Learned that there were principles in development

  • Learned that there was a method to development

What Is Refactoring?

  • Refactoring (verb): to restructure software by applying a series of refactorings without changing its observable behavior.

What Isn't Refactoring?

  • Fixing Bugs
  • Performance Optimization
  • Adding defensive code (error handling)
  • Making code testable (can be a result of refactoring)
  • Rewriting code in isolation from the existing code.
  • Wrapping code in an additional abstraction layers without looking at the existing code.

When To Refactor?

"If you find your change hard to add, Refactor First, then add it. Don't fall for the quick hack."

When To Stop A Refactoring?

  • When the cost of further refactoring is greater than the cost of not refactoring.
  • When you have accomplished what you set out to do.
  • Beware the never-ending refactor, many are antonyms:
    • extract class vs. inline class

What is A Refactoring?

Refactoring (noun): a change made to the internal structure of software to make it easier to understand and cheaper to modify without changing its observable behavior.

Defining A Refactoring

  • Problem (UML or Code)
  • Solution (UML or Code)
  • Why do it
  • Benefits of doing it
  • When not to do it
  • Mechanical List of Steps
  • Next Steps

Types of Refactors

Types of Refactors

Types of Refactors

Types of Refactors

Types of Refactors

Types of Refactors

Dealing with Generalization

 

​​https://sourcemaking.com/refactoring/extract-interface​

Deciding What To Refactor

  • Complexity Metrics
  • Code Smells

Complexity Metrics

  • Learn What They Are.
  • Learn to See Them.
  • Let them guide you to simple refactorings.

Complexity Metrics

  • Class Method, Field, Line Count
  • Function Line, Parameter Count
  • Conditional Nesting Depth
  • Cyclomatic Complexity
    • logic branch count: n branches = n complexity
  • N-Path Complexity path count: n branches = 2^n paths
  • CRAP Score
    • comp(n)^2 * (1 – cov(n)/100)^3 + comp(n)
      • best case (100% coverage): comp(n)
      • worst case  (0% coverage): comp(n)^2 + comp(n)
    • examples
      • comp: 10, cov: 0%     = 110
      • comp: 10, cov: 100% = 10
      • comp: 9,   cov: 0%      = 90
      • comp: 10, cov: 10%    = 82
    • ​Worse to have untested code than simple code

Complexity Metrics

Enforce Them!

// .eslintrc
{
  "rules": {
    "complexity": ["error", 10],
    "max-depth": ["error", 3],
    "max-statements": ["error", 20]
  }
}

// PHPMD

...

Code Smells

  • Learn the names of the code smells
  • Learn what they look like
  • Learn why they're bad
  • Be Harry Potter. Call them by their names when you see them

Types of Code Smells

Types of Code Smells

Types of Code Smells

Types of Code Smells

Types of Code Smells

Things to Remember

  • Don't Repeat Yourself

  • Keep it Simple

  • You ain't gonna need it

  • Be clear, not clever

  • REFACTOR MERCILESSLY

What Questions

Do You Have?