/videostore-refactor

following a martin fowler tutorial

Primary LanguageJavaScript

Refactoring a JavaScript video store

My attempt to follow a tutorial

https://martinfowler.com/articles/refactoring-video-store-js/

The simple example of calculating and formatting a bill for a video store opened my refactoring book in 1999. If done in modern JavaScript, there are several directions you could take the refactoring. I explore four here: refactoring to top level functions, to a nested function with a dispatcher, using classes, and transformation using an intermediate data structure.

Many years ago, when I was writing the refactoring book, I opened the book with a (very) simple example of refactoring some code that calculated a customer's bill for renting some videos (in those days we had to go to a store to do that). I was contemplating this refactoring example recently, in particular how it would look if it were written in modern JavaScript.

Any refactoring is about improving the code in a particular direction, one that suits a development team's coding style. In the book the example was in Java, and Java (particularly then) suggests a certain style of coding, an object-oriented style. With JavaScript, however, there is a lot more options about what kind of style to go for. While you can do a Java-like OO style, particularly with ES6 (Ecmascript 2015), not all JavaScript pundits favor that style, many indeed consider using classes to be a Bad Thing.