Announcing the Second Edition of “Refactoring”
martinfowler.com
martinfowler.com
I'm stoked for the book. I'm surprised-but-not-really that it will use JavaScript as its main language to teach the concepts!
> When I wrote the first edition I was frustrated that I was having to write the book's code in a language that was significantly worse than the language I preferred (Smalltalk). Little did I know that I would take another step downwards twenty years later.
Oh, the irony!
That doesn't explain why choose it from among the majority of other languages that aren't centered on classes. JavaScript is so quirky, it seems like a really strange choice for a general programming book.
Perhaps it's my back end bias showing.
I feel its better to reach more developers, than aim for the best technologies that very few are actually even aware of.
I'm sure the author had his reasons, and there are certainly worse choices in the top dozen languages. (Refactoring... in PHP!)
> Such a language needed to be widely popular, among the top half a dozen in language popularity surveys. It really helps to have a C-based syntax, since most programmers would recognize the basic code structure
You can argue others, but at least this will help you understand why he chose it over many other languages that aren't centered on classes.
What I really miss is the integration with the automatic refactoring tools in our IDEs. How would I make a heavy refactoring if I already have a "extract method" in my interface? Would it be the same? Some of the refactorings are easier to automate so are more common in the IDEs. Would are better paths considering what is automated?
But does anything similar exist in JS land?
The VSCode editor is able to leverage the TypeScript engine even with vanilla JS to automatically infer types and also supports JSDoc. Basically you write vanilla JS, add JSDoc comments here and there when the inference doesn't work as you want and it gives you most benefits of a typed language, without tying yourself to a specific ecosystem (your teammates could be using something else entirely). It works pretty well.
In the introduction he drew comparisons with GoF design patterns book, saying he wrote it hoping people would refer back to it as they do to that book. Yes, sometimes I need to rename a function or split it in two, but do I really need to dig out a reference book to look up how to do that? And the comparison to design patterns is crazy, those are at a far higher level of abstraction and are correspondingly less obvious (and more interesting).
In addition to the principles about refactoring, reading a few of the examples helped to drive in how methodical it was.
I'd rather do skim reading on stuff that I already know than for them to skip over it just in case there was a fundamental issue that was missed (or deemed trivial).
I always try to either be refactoring or changing behavior but not both at the same time. That has kept me out of a lot of trouble.
> "For each desired change, make the change easy (warning: this may be hard), then make the easy change"[0]
[0] https://twitter.com/KentBeck/status/250733358307500032
Edit: Added a link to his first Tweet containing the quote that I know of.
Keep those tasks separate.
The biggest benefit is that it makes code reviews easier for others because the reviewer can step through each commit. This makes the refactoring changes much more obvious.
Splitting the commits also makes it much easier to roll back a functional change that goes wrong or isn't needed while still keeping the refactoring improvements.
You don't need to understand the code; you don't need to step through the code. You follow the steps for "extract method" and you go from a working state to another working state with no worries.
The individual refactorings are more-or-less interesting, but as others have said, they're somewhat commonplace now.
Two books that every programmer should read: Software Tools[1] and The Elements of Programming Style[2] by Kernighan and Plauger. Bonus: The Unix Programming Environment[3] by Kernighan and Pike.
[1] https://www.amazon.com/Software-Tools-Pascal-Brian-Kernighan...
[2] https://www.amazon.com/Elements-Programming-Style-2nd/dp/007...
[3] https://www.amazon.com/Unix-Programming-Environment-Prentice...
They describe the design and implementation of many of the classic unix tools. Nowadays it is posible to learn the same principles by reading the userland source code of BSD or Plan9.
If I was going to read a refactoring book while waiting for the second edition, I would take a look at “Refactoring to patterns.” (https://industriallogic.com/xp/refactoring/) R2P combines the best parts of Fowler’s Refactoring and the Gang of Four design pattern book.
Notably: it emphasizes the fluid nature of Refactoring and design patterns. Every Refactoring to a pattern in the book is shown in a few different ways, each explaining some of the trade offs. It has the same recipe book feel that Fowler’s book has and it lends itself to browsing and reference.
The recipe-driven approach makes it easy to carry out the refactorings and provides a fun new way to think about modifying code.
Imho, the section on the “Compose method” Refactoring in R2P makes the purchase worth it. How can such a dead simple Refactoring make code so much nicer? Read the book to find out ;)
Javascript has a lot more first-class options other than classes and interfaces, so there might be some more insight.
As far as good programming books, I quite liked Code Complete when I was a younger programmer, and found that Anti-Patterns was helpful in giving me a lot of vocabulary and patterns in recognizing what happens in job-settings, as well as refactoring overviews as a solution to real problems.
When I was in college, I read "Refactoring to Patterns" which pretty much sums up Refactoring in a short chapter. The ideas aren't really that complicated, but seeing some practical examples is pretty useful.
"Working Effectively with Legacy Code"^1 is usually what I recommend instead. It felt like a more up to date approach
More generally "The Pragmatic Programmer"^2 is a classic for a reason. But from you're comment you've probably already read it.
1. https://amzn.to/2I4rIWP 2. https://amzn.to/2GfxVTa
2.
It's a decent book, though obviously looking a bit dated now, particularly in the OO-centric presentation. I would confidently recommend it to someone like a junior developer at work or a keen self-taught programmer who had figured out the basic mechanics and was starting to explore ideas about building and maintaining bigger systems. I probably wouldn't recommend it to anyone further down the path, though, just because the techniques described tend to be quite simple and they're mostly things that people learn with experience anyway.
1. Refactoring does not change functionality. The observable behavior should be the same before and after.
2. Refactoring should be done one step at a time. Each step should be as small as possible and obviously correct.
3. When changing code, at any given time you should be working on a functional change or refactoring - never both at once. Ideally these should be split into separate commits as well.
I think beyond the principles something like extracting a chunk of code into a separate function is not a task that needs a proper name. It's just something that comes naturally once you get into a mindset of always making things better, or as we say where I work: "suck less." It's getting into this mindset that takes the most work. If every time I deploy code it sucks less, or even just on average, that's good enough.
Refactoring is compound interest for maintainability. Once people buy into this everything starts getting better.
I also believe that you should never ask permission to refactor - just do it. A gardener who's task is to mow the lawn shouldn't ask permission before pulling a weed just because it's outside the narrow scope of the task at hand. Likewise a programmer should not ask permission before refactoring code (excluding large, time consuming refactors).
Kind of like Effective C++, More effective C++, the old Win32 Charles Petzold book, GoF, etc.
To me, it was a classic, and much appreciated it as that was also when the whole TDD thing took off.
The move to a less class-centric approach is a much needed and welcomed shift. It helps to illustrate the spirit of the pattern or technique without getting weighed down with the restrictiveness and arguably dogmatic nature of java.
He could have chosen Python which is also one of the top languages and it does not have the quirks of javascript.
I can't help but feel that he only chose this language because this is the language which will net him the most ROI.
And we'll take the collateral damage which is an influx of people who will wave his book and say "Let's use this language! Even Mr. Fowler uses it! This must mean that this is the best language there is!"
Imagine the damage this will do to us grunt programmers in the field. Now it will be even harder to push real languages through management now that a celebrity in our little scene is using javascript.
I bet that Mr. Fowler doesn't have to program in that crappy, quirky, horrible 9nth circle of hell language.
Sorry for the rant but this had to come out.
Thanks.
Anyone know of any other good books about high-level design/architecture for javascript?
Actually, thinking about it, maybe what I'm really looking for is something for designing/architecting things in dynamic/weakly typed languages... (Been doing JS for a bit now, but I spent much longer in Java before).
[1] http://thinking-forth.sourceforge.net/ Although you can buy a copy on Amazon.
I'll get out now...