DEV Community

Mashcuy Lab
Mashcuy Lab

Posted on

Refactoring #3

In this article we are summarizing the most interesting parts of:

  • Martin Fowler - Refactoring (2018) Chapter 7 "Encapsulation"

This chapter give us some encapsulation advices:

General idea

  • encapsulate mutable date in a project can give us the advantage to see when and how data structures are modified
  • we give the responsibility of modify it's own attributes to the class.
  • encapsulation means that modules need to know less about other parts of the system

Encapsulate Record

  • replace js objects by classes
  • identify how and where are they being used

Encapsulate Collection

  • don't publish your collections to be modified from outside your class, use methods instead (add, remove, etc)

Replace primitive with object ⭐

  • in early stages of development we represent simple facts as simple data items as strings or numbers. With the time those simple facts are not simple anymore.
  • a Linkedin url may be represented as a simple string but then we could need formatting, extracting info or many other operations
  • as soon as we realize that we need to do other operations than a simple print we create a new class for that bit of data.

Extract class

  • classes always grow in it's responsibilities, then we need to extract them in new classes

Substitute algorithm

  • refactor complex algorithms with simpler ones.
  • if you understand better the problem and can use another perspective that give more clarity to the solution, then it worth to substitute it.

Top comments (0)