My Adventures in Coding

July 3, 2010

Refactoring Principles

Filed under: Refactoring,UberConf 2010 — Brian @ 9:07 am

While at UberConf I had the great opportunity to attend a talk by Venkat Subramaniam titled “How to Approach Refactoring”. The following is a quick summary of his talk and what I took from it.

Refactoring Principles

  1. Reduce code
  2. Avoid clever code – keep it simple!
  3. Make it small and cohesive – single responsibility
  4. Eliminate duplication – DRY
  5. Eliminate dependencies – rather than striving to reduce dependencies, strive to remove them
  6. Write self documenting code – make comments unnecessary
  7. Code should be understandable in seconds – it is not just about reducing the amount of code, but also about clearly expressing meaning.
  8. Avoid primitive obsession – focus on creating higher level abstractions
  9. Checkin frequently, take small steps – every commit should be only one change.
    • Shorter feedback cycle
    • Other developers are kept in the loop
    • Avoid large painful merges
  10. Keep code at one level of abstraction – each method should do one thing, and delegate to other methods that each do one thing.

When not to Refactor

  • Never refactor while making any other code change. Take a note of the refactoring and complete it after the bug fix or functionality change has been committed. Never commit a refactoring at the same time as any other code change. If you do break a build, having small, single change commits, will make spotting the problem that much easier.

    1. When you spot a code smell – take a note of it
    2. Finish your change
    3. Commit
    4. Refactor
  • Never refactor alone. You should always have a second set of eyes on the problem. Pair programming is essential while refactoring. The second person will be their to help ensure we are making the code easier to understand, using good names, and helping to ensure we are not breaking any existing logic.

When to Refactor

  • You can refactor before or after a bug fix or a functionality change
  • If you think the change will improve the design of the code
  • If you think the change will improve the readability of the code for other developers
Advertisements

1 Comment »

  1. […] myadventuresincoding.wordpress.com […]

    Pingback by Design and Strategy | Annotary — February 3, 2014 @ 7:51 pm | Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: