chapter four

4 Advanced coding skills - Refactoring

 

This chapter covers

  • How to plan a refactor and what to consider while doing so.
  • What to look for while refactoring code.
  • The tools and techniques you’ll use to perform the actual code refactor.
  • The scenarios where refactoring code will not make any sense.

Other than unit testing, understanding the importance of refactoring and the correct way of doing it is definitely one of the most valuable things you can learn early in your career as a software developer.

If you want to do proper refactor of your or other people’s code (which we’ll talk about in this chapter), you’ll have to learn all you can about best coding practices, and you’ll have to understand how to pick up on anti-patterns that need to be broken down and rearranged to be cleaned up.

Ironically, doing proper refactoring of a code base requires years of experience, which I’m assuming you don’t have as of now. So why are we bothering talking about it then? Because you can’t get experience doing proper code refactors if you don’t know what “proper code refactor” means. You need to understand what to look for and what you should do once you find something that needs to be re-worked.

4.1 What does refactoring mean anyway?

4.2 What do you do before you start refactoring?

4.2.1 Version control is your friend!

4.2.2 Unit tests are all the rage

4.2.3 Baselining your code

4.2.4 I love it when a plan comes together

4.3 What to focus on when refactoring?

4.3.1 Does my code smell?

4.4 How to perform code refactors?

4.4.1 Common refactoring techniques

4.4.2 Tools to reduce human error

4.4.3 Refactoring best practices

4.5 What if you don’t need to refactor your code?

4.6 Summary