How to refactor code without pain and do it efficiently.
I'm Alex, a web developer at 0+X.
I've been writing code more than 10 years now and by this time I've seen some good code and a really bad one. In this talk, I want to share with you some technics I use to painlessly refactor bad code.
Refactoring is the process of restructuring existing computer code without changing its external behavior. [It's] intended to improve the design, structure, or implementation...
Refactoring takes effort, but in return we achieve:
Characteristics are subjective and difficult to measure directly. But they all refer to “how readable the code is”.
...If you try hard, you can measure the characteristics, of course, but about that later 🤫Cognitive helpers that tell us how to recognize “bad” code.
Sometimes it's enough to look at the list of code smells, find the right one, and use a recipe against it.
return
.Take a look at the commit history. There I showed how to apply different techniques and use buzzwords™ to find problems with the code.
At the very least, they can help find “dead” code and unused variables.
If something is clear from the context, it can be omitted. But be careful with it.
...But it becomes easier to deal with.
Focus on data transformations. Think of their states as the results of business events.
return
.Null
-objects and Option
-types.Extract repeating conditions into variables.
Simplify patterns with Boolean logic rules.
!(a && b) === !a || !b
!(a || b) === !a && !b
return
Pure functions and immutability are a perfect example of abstraction. If we can replace the function with its result we can “abstract” its details with the result.
Result
-containers.Impureim, because:
Impureim helps to arrange the business rule code in the center and the side effects at the edges.
Type systems can highlight problems with encapsulation, invalid data states and CQS violation.
Result
-containersA component with everything in it is hard to understand, test and modify.
Explain what the code should do, not how.