What to Know about Refactoring

Transcript

What is REfactoring? For that matter, what’s factoring?

If you make your living programming, refactoring is for you.  I bet you’re already familiar with refactoring concepts, even if you’ve never heard the term before.  Welcome my friend to WTK, the “What to know” show.

I’m your host Peter, Whelan. 

I won an award for my first book, The Rake.  As a writer, I’ve found its sometimes useful to write badly.  

If I’m stuck and can’t write something good, then I write something bad. Then I keep rewriting it until it’s as good as I can make it.

As a programmer, you’ll often rewrite existing code. You’re not going to write ideal code the first time, every time. 

I’ve hired, trained and managed dozens of programmers. I always ask what books you’ve read. If this is one of them, that’s a point in your favour.

I graduated from a computer science program, but I don’t call myself a scientist.  Science makes falsifiable predictions.

When people feel the need to add science to a discipline, it’s not what I’d call a true science. 

Political science, social science, management science aren’t what I think of as real science.

Years ago my title was software engineer, but I don’t call myself an engineer.

Programming isn’t engineering, it’s a craft.  Refactoring is a craft.  It’s kind of like weeding a garden.

As you know, programming requires attention to detail. That’s why I use the Van Halen test when I’m hiring. Watch my job openings video if you’re curious about that.

In programming, factoring is the process of breaking a complex system down into easy to understand pieces.

So, what is REfactoring?  Most schools aren’t teaching it.

REfactoring is the process of safely changing small bits of code to make a better, more readable program. 

How do you safely change a program? You have automated tests that alert you when you’ve broken something.

Before this book, I called it cleaning up the code.

Refactoring was published in 1999 and it still holds up for the most part. Before you read it, you should be familiar with object oriented concepts such as encapsulation, delegation and reflection.

Refactoring gave names to the dozens of things we do when cleaning up code which makes it a teachable skill.

The author, Martin Fowler, helpfully provides us with Refactoring.com a catalog of the refactorings found in here.

Refactoring is the process of safely changing small bits of code to make it easier to understand and maintain.

The example in the book is built around a video store rental program. Now, I know what you’re thinking.  What’s a video store? 

Let me help you out. 

You see, a long time ago, in every neighbourhood there were places called video stores. You would drive to one in the hopes of renting a movie on a tape or DVD.  You’d walk up and down long aisles, looking at hundreds of empty boxes that represented movies.

You’d take the empty box to the checkout and the clerk would hand you that movie. You’d take it home and forget about it. After a month or two, you’d find it, at which point you’d take it back to the store and pay thousands in late fees.

It was a blockbuster business. Now I know what you’re thinking. Can a book from those primitive times still be useful?  Yes! ABSOLUTELY! I’m older than it and I’m still useful.

But, skip the chapter on tools, that’s completely outdated.  The rest of the lessons still hold up. You’ll get the gist of it, even if you don’t work in Java or have never rented a video.

To demystify refactoring, read these four first.

One, the Extract method. Two, the Move method, Three, the whole chapter on simplifying conditional expressions. 

And finally, Rename is the most common one that I use when I’m trying to get my head around code. Use descriptive and meaningful phrases, DAMP for short.

My favourite tip is that when I feel a need to explain what the code is doing with comments, decompose it into a method with descriptive names.

Comments are for WHY the code is there

If you need comments to explain WHAT the code is doing, simplify the code.

The code will ALWAYS tell you WHAT the code is doing.

Remember, a key condition of refactoring is that you have automated tests in place. 

That way you can feel confident that your changes haven’t broken anything.

Read and use refactoring, improving the design of existing code.  You’ll be a better programmer!

If you want to be. 

What’s that?  A 2019 edition just came out?  Oh! Well, get the latest edition then, I’m sure it’s great too. 

Discuss refactoring with a coworker.

The more you share, the better you’ll understand it.