Refactoring: The Long Game

I recently gave the talk “Refactoring: The Long Game” at MavenLink, a local software firm. MavenLink works by doing strict test-driven development and pair programming all the time. MavenLink has an office in Salt Lake City and an office in San Francisco. It’s a really interesting place to work. MavenLink provided me with a recording of their meeting which I have uploaded to YouTube.

For about the first 40 minutes, I discuss larger term architectural concerns and goals while refactoring. After that discussion, I show a live example of refactoring some crufty code with IntelliJ which shows just how quickly you can transform code with the right automated tools at your fingertips.

Give Your Old Code Some New Love

If you’re not careful, entropy creeps its way into your code base. You take a shortcut or code something in a way that you know is sloppy and you say to yourself “I’ll come back to that later”, but later you’re faced with new feature requests or some other Imminent Disaster(tm) and you don’t go back and clean up the mess you made earlier. This is only natural in a code base and when the messes are few and far between, it is tolerable.

However, at some point the messes accumulate and you need to do something about it because the mess is preventing you from making forward progress in a reasonable manner. Entire books have been written about this situation such as “Working Effectively With Legacy Code” by Michael Feathers (see my review), “Refactoring: Improving the Design of Existing Code” by Martin Fowler and “Refactoring to Patterns” by Joshua Kerievsky. One could even consider “Domain-Driven Design: Tackling Complexity in the Heart of Software” by Eric Evans to be a book full of advice about how to avoid the accumulation of entropy in your code base. Still, sometimes you find yourself with too much entropy in your code base and it’s time to do something about it.

In this post, we’ll take a look at an open source project with a code base that is over 30 years old and has accumulated some “cruft” along the way. We’ll discuss various strategies for coping with the cruft and how to get rid of it in as safe a manner as possible. After all, we don’t want to introduce bugs while we clean up cruft.

Read the rest of this entry »

C++ Rename Shootout: Visual Studio 2017 vs. ReSharper for C++

In programming we have to come up with names for lots of things on a day-to-day basis. Naming things is hard. Lots of times we come up with names that we’re not particularly happy with, but it may be the best name we could think of at the time. Later we get a better idea for the name but now we have to update all the locations in the code that used the old name and change them to use the new name. Sometimes, responsibilities change and the names we originally chose no longer match the new responsibilities.

Getting help from your development environment for renaming identifiers can be a huge productivity boost. It lets you quickly and easily improve the names of things in your code. In this post, I’ll review two automatic renaming tools for C++ in Visual Studio: Visual Studio 2017 Community Edition 15.5.7 and ReSharper for C++ 2017.3.2.

Read the rest of this entry »

Refactoring to Learn a New Code Base

When you join a team with a large existing code base, it can be intimidating. How long does it take to become familiar with a million lines of code? I don’t have the answer to that question, but based on my past experience it easily takes several years if not more. Meanwhile, the rest of the team is adding and changing code all the time. When I apply small, focused, systemic refactorings to a code base, I visit lots of code that may be unfamiliar to me. Visiting unfamiliar code and making the same kind of systematic change throughout can help me become more familiar with the code.

Read the rest of this entry »

SLC Software Craftsmanship: Rewrite or Refactor?

Tomorrow the Salt Lake City Software Craftsmanship group will be holding their 2nd meetup. The reading discussion topic for this month is Rewrite or Refactor?. Dinner will be served and there will be a programming exercise after the reading discussion. See you there!

Refactoring Test Results for CLion 140-1221.2

I ran through a bunch of refactorings in the latest early access program (EAP) build of JetBrains CLion using my refactoring test suite, now hosted on github.


Refactoring % Passing Passed Failed
Add/Remove Block Delimiters 46.16% 6 7
Change Signature 100% 11 0
Compress To/Expand From Ternary Expression 69.24% 9 4
Create Method Stub 54.55% 12 10
Create Setter Method 66.67% 20 10
Extract Function 73.69% 14 5
Extract Method 5% 1 19
Extract Parameter 40% 2 3
Flatten Conditional 100% 1 0
Inline Macro 83.34% 5 1
Inline Recent Assignment 0% 0 2
Inline Result 0% 0 5
Inline Variable 22.23% 2 7
Introduce Constant 30.77% 4 9
Introduce Local 66.67% 4 2
Move Method 29.73% 11 26
Remove Unused Parameters 100% 3 0
Rename 77.09% 74 22
Simplify Boolean Expression 100% 13 0
Split Initialization From Declaration 81.82% 9 2
Split Multi-Variable Declaration 100% 19 0

The overall score card looks quite promising, particularly for an early access build that isn’t guaranteed to work at all. The general impression I got was that the refactoring support is better than other tools I have tried. It did much better with pointers to functions, pointers to class members, and references to class members than other refactoring tools. It did a good job of recognizing the appropriate scope for identifiers and properly performed well on rename operations, even when combined with using statements.

Read the rest of this entry »

Adding Static Analysis to Your C++ GitHub Repository

Static analysis can be extremely useful for monitoring the quality of your code base. These tools analyze your source code and check for certain kinds of mistakes that can be detected purely based on how the code is written. In this post, I’ll show you how you can add two free static analysis tools to a free continuous integration build for your C++ github repository.

Read the rest of this entry »

DevExpress Discontinues C++ Refactoring Support

DevExpress announces that they will discontinue support for C++ refactoring in their CodeRush product.

Refactoring Test Results for VAX 10.8.2036.0 built 2014.05.22

I ran through three refactorings (Extract Function, Extract Method, Rename) in the latest version of Visual AssistX using my refactoring test suite and posted the results on the Whole Tomato support forums.


Refactoring % Passing Passed Failed
Extract Function 0% 0 18
Extract Method 45% 9 11
Rename 63.55% 61 35

Feel free to comment here on the test suite itself. I’ll probably move this to github to encourage more pull requests for improvements to the tests. I haven’t updated it for C++11 language features. Feel free to post in the Whole Tomato support forum thread for comments on Visual AssistX itself.

Read the rest of this entry »

C++ Now! 2014 Presentations Posted

Ray Fix is posting all the presentations of C++ Now! 2014

You can read my presentations here:

C++ Now! 2014 was a wonderful experience! I very much enjoyed talking with the attendees, both during the show and afterwards at restaurants and the bar. I hope to attend again next year.

Spring 2014 Utah Code Camp: March 15th, 2014

The Spring 2014 Utah Code Camp is coming up and I’ve got some proposed sessions. Utah Code Camps are by the developer community and for the developer community. Utah Code Camp is always free! Spring 2014 Utah Code Camp will be held on March 15th, 2014 at the University of Utah Spencer Fox Eccles Business Building.

Read the rest of this entry »

Refactoring: Sort Members by Visibility

You have a C++ class that shows readers its implementation details before it shows readers its public interface.

Reorder the class members in order of decreasing visibility, preserving the relative order of the declarations within each visibility category.

Read the rest of this entry »

Utah Code Camp on March 27th, 2010

At this Spring’s Utah Code Camp on March 27th, 2010 I will be presenting the following sessions:

  • What’s New in XNA Game Studio 4.0
  • Developing for Windows Phone 7 Series
  • Red, Green, Refactor: Learn Test-Driven Development in Two Hours (with Zhon Johansen)

Check the Utah Code Camp website for more information.

Code Camps are always free.

C++ Refactoring Tools Test Suite Available

I created a test suite for C++ refactoring tools. You can download release 1.0 for yourself to see the tests. Results of the tests are included for ReFactor!Pro v9.1.10.

Read the rest of this entry »

Agile Code Reviews, Part 2

In my post on agile code reviews, I described how we were attacking our code through automated refactorings that we trusted to apply without tests. Mostly we applied a series of Extract Method refactorings to long methods in the process of performing a Compose Method refactoring on the long method. Eventually, your team will run out of situations where you can apply automated refactorings to improve the design of your code and you’ll have to start applying actual code changes. Several of the commenters on my previous post remarked that making changes without unit tests implies risk that can only be averted with sufficient manual testing and that such risk is better mitigated by automated tests. In this followup post, I’ll describe the results from our team at applying the decoupling techniques described by Michael Feathers to our legacy code so that we could backfill unit tests onto the legacy code in preparation for a design change we want to make.

Read the rest of this entry »

Refactoring: Split Module

You have a C module containing variables and functions that implement two different responsibilities.

Split the two responsibilities into two modules.

Read the rest of this entry »

Refactoring C++ Template Classes To Reduce Code Bloat

Test-Driven Development and Refactoring At Agile Roots 2009

I was a guest instructor at Zhon Johansen’s Test-Driven Development and Refactoring course at the recent Agile Roots Conference in Salt Lake City. Zhon and I did a dry-run rehearsal of this course for the Utah .NET User Group meeting in June. The streaming video for this tutorial is now available.

Read the rest of this entry »

Agile Code Reviews

Have you ever been tasked with doing classic “Fagan” style code reviews? They’re boring. They’re tedious. They take up lots of time and yield little, if any, results. At a previous employer, we did these kinds of code reviews. It consisted of printing out the source code to be reviewed, one copy per reviewer. Each of us reviewed the code alone, making notes to the printout. Then we’d gather in a conference room and discuss our findings while one person combined all our notes into a list of items to rework. That person would rework the code alone and then report back to the group the results. Whew! This is what the agile development world would call a “high ceremony process”. It has lots of formal steps and the associated paperwork. Is there a better way? In this post, I’ll discuss what we do at my current employer that makes code reviews a fun team activity.

Read the rest of this entry »

Technical Debt Resources

Brad Appleton has posted a great article on technical debt with lots of links to further resources on the subject. If you’re struggling with an aging code base and your managers just don’t understand why you can’t produce features as fast as you used to or at the same quality as before, then direct them to specific resources after reading this blog post. Using ‘debt’ as a metaphor is perfect for the cruft that inevitably finds its way into our code when talking to non-technical types.

Refactor early and often to avoid accumulating technical debt to the point that it cripples you!