Refactoring JavaScript

Author: Evan Burchard
Publisher: O'Reilly
Date: March 2017
Pages: 422
ISBN: 978-1491964927
Print: 1491964928
Kindle: B06XK1V629
Audience: JavaScript programmers
Rating: 3
Reviewer: Ian Elliot

There is a lot of JavaScript out there that needs refactoring - can this book help?

JavaScript is one of those languages where you start a project without even realizing that you have. As a result there is a lot of poorly thought out code that needs to be worked over. As we all know, code that is only intended to be around for a few hours often lives a lot longer and it is to be made maintainable then it has to be refactored so a book on refactoring JavaScript with the subtle "Turning bad code into good code" is promising a lot.

If you are expecting any book to magically turn your poor code into something better then you are going to be disappointed. Writing better code means you have to recognise what better code is, and this isn't straightforward. You can't simply produce a checklist of things to do, and finding advice that is specific to a language is also a tough problem.

Banner

This book starts off with a look at what refactoring is and what it isn't. In this case refactoring is defined as improving the code without changing the behavior of the program. The first four chapters are fairly general and mostly deal in issues that are relevant in any language. Chapter 2 is more JavaScript specific in that it condsiders the major problem of working with different JavaScript versions. Chapter 3 is on testing and how testing and refactoring go together and Chapter 4 is about how to create new code with test driven development. This is all very optimistic - you write your tests and implement your refactoring. If you have done the job correctly your tests pass and the code does the same job as it always did. It doesn't say much about the negatives of having to write tests and having to make sure that tests cover all of the ways things can go wrong.

 

 

Chapter 5 is where the JavaScript focus starts and we have a fairly random walkthrough the problems of using JavaScript. The difficulty here is to work out what is good and what is bad. For example, it is explained that in strict mode this is sometimes undefined - but you are left thinking so what? Also many of the explanations are as if JavaScript didn't have a logic in the way that things work and in place of principles you are exposed to examples that just show you the way things work.

I found that part of the problem is that the examples used are too big and the explanations don't really draw out the principles involved. The discussion is very dense and it is difficult to know if ideas are left out on purpose or by accident. For example, in the section on privacy, i.e. private variables and methods, closure isn't mentioned and yet it is the key to creating private entities. Interestingly closure isn't in the index but Clojure is.

Chapter 6 moves on to refactoring simple structures. This deals with the sort of things you will encounter in any language - renaming, dead code, and so on. Chapter 7 deals with functions and objects - alternatives to Array, using a global object as a namespace and so on. Chapter 8 goes over the ideas of refactoring within a hierarchy, i.e. inheritance and how it operates in JavaScript. Chapter 9 goes over the standard OOP patterns as they apply to JavaScript, which isn't that much.  Chapter 9 goes into async refactoring and explains Promises but doesn't deal with async and await.

Finally we have a chapter on functional refactoring (which is where a tiny mention of Clojure, Haskell and Scala is made). This is basically a tutorial on functional programming with a big example program.

Conclusion 

This is not a book for the JavaScript beginner - you need to be fairly confident in your understanding of some of the more subtle points of using the language because you don't get any help. In fact you have to make use of your understanding to follow the examples, not the other way round. Mostly I found the explanations difficult to follow. There are also plenty of places where a deeper idea is passed over as being outside the scope of the book - how Promises work, for example.

There is also no systematic presentation of any of the ideas or principles of JavaScript programming in general. The language is treated as if it was an OOP language, Java say, with some strange diferences.

When it comes to JavaScript this book is fairly weak, but it is strong on the general ideas of refactoring and how to use tests to ensure you don't break your code after refactoring. It is best described as a standard approach to OOP refactoring noting the differences if you try to apply it to JavaScript. You might find it of value if these general ideas are new to you, but a deeper understanding of JavaScript and some good tools might help even more.

  • Ian Elliot is the author JavaScript Async which is devoted to event handling, callbacks, Promises, async and await.

 

To keep up with our coverage of books for programmers, follow @bookwatchiprog on Twitter or subscribe to I Programmer's Books RSS feed for each day's new addition to Book Watch and for new reviews.

Banner


Deep Learning with JavaScript

Authors: Shanqing Cai, Stan Bileschi and Eric Nielsen
Publisher: Manning
Date: February 2020
Pages: 560
ISBN: 978-1617296178
Print: 1617296171
Audience: JavaScript Programmers
Rating: 5
Reviewer: Mike James
JavaScript doesn't seen a natural for AI but...



Learning Progressive Web Apps

Author: John M Wargo
Publisher: Addison-Wesley
Pages: 272
ISBN: 978-0136484226
Print: 0136484220
Kindle: B084T4JK3S
Audience: Capable JavaScript programmers
Rating: 4.5
Reviewer: Ian Elliot
PWAs - I'm not even sure what they are?


More Reviews

 

 

 

 

 

Last Updated ( Monday, 05 February 2018 )