Refactoring Javascript - [Book Review #1]
Liu Yongliang
Posted on April 30, 2021
Refactoring JavaScript is a book by Evan Burchard, about turning bad code into good code. Since I am going to delve a little deeper into JavaScript in the upcoming months, I thought I could learn from books that actively use JavaScript examples so that I get more familiar with the language. I am about halfway through the book and would like to give some summary and thoughts before I forget them.
The first few chapters lay out the foundation that refactoring is about improving code quality while preserving existing behavior. The author empathized on the importance of testing and having tools to help verify that the code works as intended. He went on to explain some intricacies of JavaScript and the fundamentals of testing. Though the content could be common knowledge to people who aren't beginner coders, I thought that the intense focus was on point. Quite significant efforts were given to drill the following concept into a reader's mind:
Avoid refactoring without tests!
Here is a flow chart that the author used to illustrate decisions about test, refactor or implement. You can tell from this diagram that refactoring is a small part of the decision process and it relies heavily on having testing in place.
Another major discussion in the first part of the book is on understanding functions as six basic components and the author provided some general advice for each of them:
- Bulk
- keep lines of code and complexity low in each function
- Inputs
- could be explicit, implicit or non-local, but prefer explicit over non-local
- Outputs
- avoid returning a null or undefined, or returning different types of values
- Side effects
- keep to a minimal or nonexistent
- this: the implicit input to functions
- have it well-defined when possible
- Privacy
- it necessarily impacts access in JavaScript
Author: Evan Burchard Author's interesting personal site
Link: Book PDF
Posted on April 30, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.