
The Second Edition of “Refactoring” - sz4kerto
https://martinfowler.com/articles/refactoring-2nd-ed.html
======
lemoncucumber
Compared to the first edition of the book the second edition has the same
functionality but is much better organized, which will make future editions
much easier to develop.

~~~
dajonker
Would you say the book has been refactored? Nah, just kidding, the author
would probably call it restructured.

~~~
IshKebab
That's the joke.

------
mherrmann
No other book improved my coding style as much as the first edition of
Refactoring. On a superficial level, it teaches you how to "clean up" code,
according to various patterns. But what it really does is give you a feeling
for why the code should be improved in the first place. Highly recommended.

~~~
cpeterso
Once one adopts the refactoring mindset, its lessons and approaches can also
infuse new code aka "prefactoring". :) Ken Pugh wrote a 2005 book called "
Prefactoring: Extreme Abstraction - Extreme Separation - Extreme Reliability
1st Edition", which won a 2006 Dr. Dobbs' Jolt award.

[http://www.drdobbs.com/joltawards/2006-jolt-
awards/187900423...](http://www.drdobbs.com/joltawards/2006-jolt-
awards/187900423?pgno=3)

~~~
clumsysmurf
Did you also read "Refactoring to Patterns"? If so, could you compare that
with Prefactoring ?

[https://industriallogic.com/xp/refactoring/](https://industriallogic.com/xp/refactoring/)

~~~
cpeterso
Sorry. I haven't read that book. :)

------
raymondh
I think almost every programmer would benefit from reading "Refactoring". It
sharpened my skills quite a bit, and I was already experienced before
encountering the book.

The book was an easy read and was an efficient use of my time. I expect that
rewrite in Javascript will further improve the information density.

~~~
kristiandupont
I bought the book ages ago and remember not really finding anything of value
in it. I don't mean to sound like someone from /r/iamverysmart but I recall
thinking I would benefit in the way I did from the design patterns book: by
giving me terminology for things I already did. But as far as I remember, the
names were really obvious ("rename parameter", "extract method", etc.) and the
instructions equally so. Can you give an example of something you learned from
it?

~~~
quietbritishjim
I remember having exactly the same experience, including the anticipation that
I would benefit from it the way I would from Design Patterns. For most of the
refactorings in the book, a bullet point in a list would've seemed a bit
excessive, never mind a step-by-step sequence of instructions. In the end I
literally threw the book away – the only technical book I've ever done that
to.

I distinctly remember one of the things in the book, which in retrospect isn't
even really a refactoring, called something like "null object". The idea was
that if you were going to return null from a function (e.g. get student by ID
but that ID doesn't exist) then you could instead return a concrete singleton
object that represented null. That way, if you forgot to check for it then at
least your program wouldn't crash. Now, the merits of this idea are debatable
and I don't think I've ever used it in the years since. But I strongly
remember thinking at the time that it was the only idea in the book that
wasn't totally obvious.

There was a good chapter early on about testing, which I think was my first
exposure to that idea. It was a bit brief but that's not a surprise given it
was a bit of a digression from the main topic. A whole book on that might've
been quite a bit more interesting.

~~~
joshka
I use this pattern fairly regularly in a variety of ways. Here's a quick piece
of example code that might help explain this a bit better. I'm writing this
example in C#, but I usually work in other languages (Java / ruby / Python /
JS),

before:

    
    
      public Student GetStudentById(int id)
      {
        var record = db.findStudentById(id)
        if (record == null) return null;
        return new Student(record.id, record.name, ...);
      }
    

Then in say a student record controller class:

    
    
      public View Get(int id)
      {
        var student = getStudentById(id);
        if (student == null) return View("NotFound");
        return View("Display", student);
      }
    

After:

    
    
      [ContractAnnotation("=> notnull")]
      public StudentSearchResult GetStudentById(int id)
      {
        var record = db.findStudentById(id);
        if (record == null) return StudentSearchRsult.NotFound;
        return new StudentSearchResult(record.id, record.name, ...);
      }
    

Then:

    
    
      public View Get(int id)
      {
        var student = getStudentById(id);
        return new View(student.ViewName, student); // look no null check becase student.ViewName is always valid here
      }
    

Note: of course this is entirely simplified for the purposes of example.
Perhaps your real world example doesn't return null from the db, but a
RecordSet with either 0 or 1 elements. Perhaps your db uses exceptions that
you translate into results like this.

The cool thing there is that you get tool support (from e.g. ReSharper)
telling your that this is entirely safe to do without a null check and tool
support when it's not.

~~~
goto11
But then the 'Student' object does not represent a student but represent the
result of a search which may or may not be a student, and this result decides
its own view. Effectively the model is coupled to a specific view, which seems
wrong to me. I would much prefer to have the search return an Option<Student>
and then have the controller select the view based on this option. This also
makes it explicit that the search may nor return a result, which is not really
explicit if it always returns a Student instance.

~~~
joshka
Note the explicit change to StudentSearchResult over student there to make
your last comment kind of a yes, I agree and that was the intent...

There are many ways that this can be implemented that avoid an if result !=
null / == no value, often a map suffices.

------
jammygit
I've read and enjoyed quite a few of Fowler's articles. The things I've taken
from his writings have been concretely valuable so far too. I'm not
experienced enough though to evaluate how his ideas stand up in big or
complicated projects.

Can anybody comment?

~~~
danmaz74
I can't comment on other articles, but I would be careful with some
suggestions about mocking everything in automated tests. I've seen a couple of
totally botched projects because of that approach.

~~~
jammygit
How did the mocks do the damage? Lack of integration tests, wasted time, or
something else?

~~~
billsix
It results in tautological tests.

~~~
softawre
Not really. I mean, you're still testing that the compiler works and that the
memory works. ;)

------
azhenley
For most people, I think his online catalog of refactorings and blog articles
are sufficient. Though I think the first edition is a good book, I wouldn’t
need it if I didn’t do research on refactoring tools.

~~~
kalev
Did not know there was a online library of refactorings. Here’s the link
[https://refactoring.com/catalog/](https://refactoring.com/catalog/) if
anyone’s interested.

~~~
bpicolo
What's the target demographic for something like this? It seems like the
trivial part of refactoring.

I've seen a similar, entire book dedicated to database refactoring that had
roughly the same pattern. Chapters named akin to "The Change Table Name
Refactor" felt kind of bizarre, when what I was hoping would be contained was
advice about refactoring large databases in practice (moving data to new
service boundaries, etc). Practical advice on building systems to move data
and such seems to be just missing

~~~
darrenhobbs
I read the first edition when I was nearly 2 years into my professional
career. I understood the basic 'what' and 'how' of software development.

Refactoring helped me start to understand the 'why' and 'when'. Why is design
A better than design B? When is it time to tidy up this code? Which choice
will be easier to change later? That kind of thing.

------
yazr
Is anyone aware of a birds-eye catalog of common code abstractions?

e.g. linked list, hashes, lookup table, classes, counters, iterators, etc

(Not referring to books such as TAOCP which cover a few topics in-depth)

~~~
steveklabnik
Sounds like you want
[https://en.m.wikipedia.org/wiki/Introduction_to_Algorithms](https://en.m.wikipedia.org/wiki/Introduction_to_Algorithms)
maybe?

~~~
yazr
Well, I can probably mine 10 tops books and come up with a good list of 100
abstractions. But i am sure someone has done a much better job than me on
this..

------
myth_buster
> When I choose a language for examples in my writing, I think primarily of
> the reader. I picked Java because I felt the most people would be able to
> understand the code examples if they were written in Java. That was the case
> in 1997, but how about in 2017? [...] 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. Given that, two stood out. One was Java,
> still widely used and easy to understand. But I went for the alternative:
> __JavaScript__. [...] But the compelling reason for choosing it over Java is
> that isn't wholly centered on classes. There are top-level functions, and
> use of first-class functions is common. This makes it much easier to show
> refactoring out of the context of classes.

This is very interesting choice of language. I wonder whether typescript could
have been a candidate as well. Although not as popular as JS, I believe most
of JS community has migrated to typescript (due to obvious benefits) and it is
more closer to a C-based language.

~~~
chrisseaton
> I believe most of JS community has migrated to typescript

You think the majority of JS developers have switched over to TypeScript?

~~~
myth_buster
_has_ may be incorrect but I do feel the switch is happening [0]. Angular
which is the most popular client side JS framework is using Typescript. React
is also moving in the same direction with typescript-react-starter and tsx
support.

[0] [https://2018.stateofjs.com/javascript-
flavors/overview/](https://2018.stateofjs.com/javascript-flavors/overview/)

~~~
BurningFrog
In my (part of the) JS world Angular is dying and React has long since won.

~~~
twblalock
I agree, I don't know any front end devs who would choose Angular for a new
project. The current choice seems to be between React and Vue.

------
alasdair_
I am a huge fan of this book. The Pragmatic Programmer and this book are the
two that I recommend most.

When I first read Refactoring, IntelliJ wasn't released yet and refactorings
still needed to be done by hand. Automated refactorings are so incredibly
useful when it comes to improving code quality over time that I don't know
what it was we did without them.

------
Ericson2314
Refactoring with and without types feels so different. I'm surprised the book
so lightly made the switch.

~~~
josteink
Is it really? Types just help formalize (for you and the compiler) what the
code actually says.

Types or not, the refactored code itself is still refactored.

~~~
BurningFrog
In a typed language, an IDE can make _breathtakingly_ large refactorings that
are 100% correct.

~~~
josteink
That’s obviously true, but you seem to be conflating refactored code with
_tooling support_ for refactoring.

Done by a tool or not, the refactoring in principle remains the same. The tool
merely automates the transformations which defines the refactoring.

And in that sense types isn’t really essential to refactoring at all.

~~~
twblalock
Types make it much, much easier to write useful refactoring tools, and to
verify that the tools did the right thing.

------
billsix
I thought this new book would be the third edition.

[https://www.amazon.com/Refactoring-Ruby-Addison-Wesley-
Profe...](https://www.amazon.com/Refactoring-Ruby-Addison-Wesley-
Professional/dp/0321984137)

------
ninjakeyboard
Oh man thanks for posting this. Did not expect to see this come. I have both
of them ("Refactoring" and then the "ruby edition") - it's a part of my
vocabulary.

Also thanks to other posters who describe their view of the updates of
content.

------
ksec
Does anyone know if Martin Fowler still doing any Ruby? Or has he given up on
it?

