Technorati Tags:
Standards I think one of the best standards any team could have in their pocket is the Cyclomatic Complexity metric by Thomas McCabe. It sounds fancy but really it’s just a very good guideline that every developer on your team should use and think about when creating a method every time in code.
It’s simple, your methods get a risk rating based on the following forumual:number of decision points +1
a decision point is just a line in the method where you had to make a decision with code (see the example in Wikipedia).
And part of this metric says to try to have no more than 7 parameters in your methods. Sometimes you may go over but if you are finding that you have so many incoming parameters, you are most likely trying to do too much in that method OR you should have put a lot of those parameters into an object for example, or used an existing object that maybe you did not know exist. Maybe there is a pattern already setup in your code by other developers that you could use. Or there may be another bad design reason why you’re sending more than 7 parameters into your method. But definitely too many parameters is sloppy. There are better ways to do this, again such as sending in an object or whatever else the case may be that you can rid so many parameters.
Keep in mind Methods are meant to organize your code. They are meant to be a UNIT of work. Not an entire process! If you find your method getting beyond 50 lines it may be time to start moving a lot of that logic into reusable methods and calling those methods from your method that you just cleaned up. The whole point of code is not just encapsulation, inheritance, etc. but also that it’s readable for you and others. Who wants to read a 50+ line method every time and multiply that times several that you may have to debug. You are basically making the code as hard to debug as a damn stored procedure with 500 lines at that point.
Reflector has a plugin which will perform Cylcomatic Complexity analysis on your assembly also, so check that out.
Check out the full details about Cyclomatic Complexity. It’ a simple solid metric that you should never disregard and truly enforce on your team and yourself. This is a discipline that’s in the back of my mind every time I write a method because I think it’s a very solid practice.
Print | posted on Monday, December 08, 2008 9:30 PM