Intro to Refactoring

A lot of time I hear people using the terms refactoring and optimizing synonymously. I personally don’t see it that way. In fact, to me, sometimes refactoring makes code less optimized! It reminds me of OOP and how putting code in classes and functions slows down the speed of your code but greatly speeds up development. So refactoring is making code more human-readable and optimizing is making code run more quickly.

Today I’m going to talk about refactoring. There are basically three steps you take when you refactor code:

  1. Unit Test You need to make sure your code has sufficient unit tests BEFORE you refactor or you could just be breaking your code.
  2. Refactor After making sure you have your unit tests you actually need to refactor your code
  3. Unit Test After refactoring you need to make sure all of your unit tests still pass

I can’t emphasize enough the importance of unit tests BEFORE you start refactoring.

One question that usually arises when it comes to refactoring is, how do you know what needs refactoring. Usually in the developer world we would say if there are any “code smells” the you need to refactor. There are some out there that will put actually statistics to these code smells but for now I just go with my gut feeling. Here are some things to look for:

  • Function that has too many lines
  • Code that is indented really far
  • Comments (see below for my comment on comments)
  • Functions with too many parameters
  • Duplicate Code
  • Fragile Code
  • … and many more

Comments Growing up in the computer world you get a lot of people, especially academics, that want you to comment everything! Not only is this tedious but a lot of times it’s pointless. Take the following example:

/**
 * Get id
 */
public function getId() {
    // return the id
    return $this->id;
}

Code, if written well, should be self-explanatory. One way to think of comments is that they should describe the why and not the how.

An example of refactoring

I want to give a little sample refactoring to show how refactoring can help out code. First we start out with code that’s not very helpful:

public function getTemp() {
    $temp = $this->model->getTemp();

    $temp = $temp * 9 / 5 + 32;

    return $temp;
}

It’s kinda hard to tell what this function does. To fix that we need more descriptive function/variable names like so:

public function getTemperatureInFarenheit()
{
    $temperatureInCelsius = $this->model->getTemp();

    $temperatureInFarenheit = $temperatureInCelsius * 9 / 5 + 32;

    return $temperatureInFarenheit;
}

That’s so much easier to understand. But we can go even further like so:

public function getTemperatureInFarenheit()
{
    $temperatureInCelsius = $this->model->getTemp();
    return $this->convertCelsiusToFarenheit($temperatureInCelsius);
}

public function convertCelsiusToFarenheit($temperatureInCelsius) {
    return $temperatureInCelsius * 9 / 5 + 32;
}

What do you think? I hope to expand on this post soon.

I've always worked as an innovative programmer. My insights and creative thinking result in superior products and customer satisfaction. Working full time as a senior web architect I've used lots of exciting technologies (i.e., Ember, Backbone, Handlebars, etc) and developed a lot of exciting sites. In my spare time I hope to develop even more exciting and new technologies. I've been programming professionally for 9 years in everything from desktop applications to web applications to mobile applications.

Posted in Wisdom

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

About the Author

  • Chris LondonSenior Web ArchitectPartners In Leadership

    I've always worked as an innovative programmer. My insights and creative thinking result in superior products and customer satisfaction. Working full time as a senior web architect I've used lots of exciting technologies (i.e., Ember, Backbone, Handlebars, etc) and developed a lot of exciting sites. In my spare time I hope to develop even more exciting and new technologies. I've been programming professionally for 9 years in everything from desktop applications to web applications to mobile applications.

    Web: https://plus.google.com/116403409191372324375