12

Code Metrics

by sciske 27. May 2009 07:43


Let’s talk about code metrics for a second.  It’s not something that is typically tracked by development teams, but here’s why you should.  Gathering code metrics can give you valuable insight into the testability and structure of your code.  By monitoring a few key metrics you can spot places where your code may need to be refactored.

Tools:

Let’s start with a free tool named Source Monitor.  There are lots of tools out there, but this one will do the job.  Essentially what you do is point the tool at a directory containing your source and the tool extracts all kinds of information for you.  I’ll let you go through all the bells and whistles on your own time.

The Metrics:

I personally like the Kiviat graph provided by Source Monitor because it displays a quick overview where your metrics are compared to your thresholds (These are configurable in the Options section). 

test

So what you say?  Let’s take a look at some of these metrics.  As you can see my test project’s metric for Max Complexity is outside of my threshold of 2-8.  So what’s a Max Complexity?  It measures the amount of code execution paths per method.  That’s right… All the If’s, Else’s, For each loops, etc.  Judging by how far it’s out of bounds there are probably some methods I need to consider refactoring to reduce the paths and make my code more testable!  Easy.  The same with Max Depth.  It measures the maximum block level depth.  clearly I’m outside the bounds I set.  The tool is configurable but you get the idea.

Still wondering so what?  I would take a look at this book review of the ThoughtWorks Anthology:

1. Use only one level of indentation per method. If you need more than one level, you need to create a second method and call it from the first. This is one of the most important constraints in the exercise.
2. Don’t use the ‘else’ keyword. Test for a condition with an if-statement and exit the routine if it’s not met. This prevents if-else chaining; and every routine does just one thing. You’re getting the idea.
3. Wrap all primitives and strings. This directly addresses “primitive obsession.” If you want to use an integer, you first have to create a class (even an inner class) to identify it’s true role. So zip codes are an object not an integer, for example. This makes for far clearer and more testable code.
4. Use only one dot per line. This step prevents you from reaching deeply into other objects to get at fields or methods, and thereby conceptually breaking encapsulation.
5. Don’t abbreviate names. This constraint avoids the procedural verbosity that is created by certain forms of redundancy—if you have to type the full name of a method or variable, you’re likely to spend more time thinking about its name. And you’ll avoid having objects called Order with methods entitled shipOrder(). Instead, your code will have more calls such as Order.ship().
6. Keep entities small. This means no more than 50 lines per class and no more than 10 classes per package. The 50 lines per class constraint is crucial. Not only does it force concision and keep classes focused, but it means most classes can fit on a single screen in any editor/IDE.
7. Don’t use any classes with more than two instance variables. This is perhaps the hardest constraint. Bay’s point is that with more than two instance variables, there is almost certainly a reason to subgroup some variables into a separate class.
8. Use first-class collections. In other words, any class that contains a collection should contain no other member variables. The idea is an extension of primitive obsession. If you need a class that’s a subsumes the collection, then write it that way.
9. Don’t use setters, getters, or properties. This is a radical approach to enforcing encapsulation. It also requires implementation of dependency injection approaches and adherence to the maxim “tell, don’t ask.”

I’m not sure about you, but I’m always looking to do better OO. :)

Tags: ,

Software | Software Development | Strategy

Comments (12) -

sulumits retsambew
sulumits retsambew
5/30/2009 7:34:52 PM #

To be frankly I didn't like math. Since I read this blog entry I could a little bit understand about metric, Thanks for sharing. Keep up the good work.

payday loans
payday loans
8/4/2009 1:39:20 PM #

Hey very nice blog!!

games for girls
games for girls
8/8/2009 11:15:48 AM #

Arcade Games r FUN!

cash loans
cash loans
8/10/2009 10:36:31 AM #

my God, i thought you were going to chip in with some critical insight at the end there, not leave it with �we leave it to you to decide�.

Party Supplies
Party Supplies
11/15/2010 9:33:24 AM #

Well , the view of the passage is totally correct ,your details is really reasonable and you guy give us valuable informative post, I totally agree the standpoint of upstairs. I often surfing on this blog when I m free and I find there are so much good information we can learn in this blog!

Packers & movers India
Packers & movers India
11/25/2010 9:35:25 PM #


I was thinking of looking up some of them websites. This post is good in regards of both knowledge as well as information.

Dissertation help
Dissertation help
12/6/2010 10:35:21 PM #

I have to admit the post is well written and the point of view is quite interesting. I had no idea about this until now.

cord blood collection kit
cord blood collection kit
2/18/2011 12:30:33 AM #

Hi, The topic that you have discussed in the post is really amazing, I think now I have a strong hold over the topic after going through the post. I will surely come back for more information.

writing dissertation
writing dissertation
3/8/2011 5:22:15 AM #

Smile  I must appreciate you for the post you have shared. . I really like it. . thank you for sharing Smile

vino senza solfiti
vino senza solfiti
3/13/2011 11:34:49 PM #

Hi, I found your post really helpful. It helped me all the way in completing my assignment, I am also giving a referance link of your blog in my case study. Thanks for posting such informative content. Keep posting.

UGG Boots Sale
UGG Boots Sale
6/27/2011 10:41:47 PM #

well I’m not writing all that over again. Anyway, just wanted to say superb blog!

London Property management
London Property management
7/1/2011 10:47:36 PM #

I loved that you written about the tools. I liked the information. Keep it up!

Pingbacks and trackbacks (7)+

Comments are closed

Powered by BlogEngine.NET 2.5.0.6
Original Design by Laptop Geek, Adapted by onesoft