views:

356

answers:

7

Are there objective metrics for measuring code refactoring?

Would running findbugs, CRAP or checkstyle before and after a refactoring be a useful way of checking if the code was actually improved rather than just changed?

I'm looking for trends we can capture that can help us improve the code review process without wasting time on code that gets changed for simple personal preference.

+5  A: 

Number of failed unittests must be less or equal to zero :)

Alexander Artemenko
I love it when I have a negative number of failed tests.
Michael Myers
... That means I'm passing even the ones I haven't written yet, I guess.
Michael Myers
+4  A: 

Depending on your specific goals, metrics like cyclomatic complexity can provide an indicator for success. In the end every metric can be subverted, since they cannot capture intelligence and/or common sense.

A healthy code review process might do wonders though.

David Schmitt
Its my hope that we could capture trends and use them to improve our code review process. I've seen a lot of loops being re-written and re-re-written without improving readability, speed or anything we can objectively measure.
sal
+1  A: 

Code size. Anything that reduces it without breaking functionality is an improvement in my book (removing comments and shortening identifiers would not count, of course)

Michael Borgwardt
Smaller code size is good, but clarity and understandability is better - even at the expense of a few lines of code.
Alister Bulman
I actually think that *reducing* code size has a much better chance to improve clarity and understandability than increasing it.
Michael Borgwardt
+1  A: 

No matter what you do just make sure this metric thing is not used for evaluating programmer performance, deciding promotion or anything like that.

Journeyman Programmer
+2  A: 

Would running findbugs, CRAP or checkstyle before and after a refactoring be a useful way of checking if the code was actually improved rather than just changed?

Actually, as I have detailed in the question "What is the fascination with code metrics?", the trend of any metrics (findbugs, CRAP, whatever) is the true added value of metrics.
It (the evolution of metrics) allows you to prioritize the main fixing action you really need to make to your code (as opposed to blindly try to respect every metric out there)

A tool like Sonar can, in this domain (monitoring of metrics) can be very useful.


Sal adds in the comments:

The real issue is on checking what code changes add value rather than just adding change

For that, test coverage is very important, because only tests (unit tests, but also larger "functional tests") will give you a valid answer.
But refactoring should not be done without a clear objective anyway. To do it only because it would be "more elegant" or even "easier to maintain" may be not in itself a good reason enough to change the code.
There should be other measures like some bugs which will be fixed in the process, or some new functions which will be implemented much faster as a result of the "refactored" code.
In short, the added value of a refactoring is not solely measured with metrics, but should also be evaluated against objectives and/or milestones.

VonC
I agree about the trend line being the value. If code test coverage is increasing and the number of broken builds in a month is zero and the number of warnings or findbugs hits is decreasing; clearly things are ok. The real issue is on checking what code changes add value rather than just adding change.
sal
+1  A: 

I would stay away from metrics for measuring refactoring success (aside from #unit test failures == 0). Instead, I'd go with code reviews.

It doesn't take much work to find obvious targets for refactoring: "Haven't I seen that exact same code before?" For the rest, you should create certain guidelines around what not to do, and make sure your developers know about them. Then they'll be able to find places where the other developer didn't follow the standards.

For higher-level refactorings, the more senior developers and architects will need to look at code in terms of where they see the code base moving. For instance, it may be perfectly reasonable for the code to have a static structure today; but if they know or suspect that a more dynamic structure will be required, they may suggest using a factory method instead of using new, or extracting an interface from a class because they know there will be another implementation in the next release.

None of these things would benefit from metrics.

John Saunders
Actually, I'm interesting in gathering this data to improve the quality of the code reviews. I suspect that there is too much change of questionable value
sal
There may be too much change to be of _measurable_ value, but that will be due to the fact that not everything can be measured. How do you measure how easy it is to understand the code? How do you measure how flexible the code is? There are metrics that purport to measure these things, but I belive they're all wrong, by definition.
John Saunders
A: 

There are two outcomes you want from refactoring. You want to team to maintain sustainable pace and you want zero defects in production.

Refactoring takes place on the code and the unit test build during Test Driven Development (TDD). Refactoring can be small and completed on a piece of code necessary to finish a story card. Or, refactoring can be large and required a technical story card to address technical debt. The story card can be placed on the product backlog and prioritized with the business partner.

Furthermore, as you write unit tests as you do TDD, you will continue to refactor the test as the code is developed.

Remember, in agile, the management practices as defined in SCRUM will provide you collaboration and ensure you understand the needs of the business partner and the code you have developed meets the business need. However, without proper engineering practices (as defined by Extreme Programming) you project will loss sustainable pace. Many agile project that did not employ engineering practices were in need of rescue. On the other hand, a team that was disciplined and employed both management and engineering agile practice were able to sustain delivery indefinitely.

So, if your code is released with many defects or your team looses velocity, refactoring, and other engineering practices (TDD, paring, automated testing, simple evolutionary design etc), are not being properly employed.

Cam Wolff