The Artima Developer Community
Sponsored Link

Java Community News
Tools and Metrics for Measuring Code Verbosity

7 replies on 1 page. Most recent reply: Jul 13, 2006 7:10 AM by Fireblaze .

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 7 replies on 1 page
Bill Venners

Posts: 2284
Nickname: bv
Registered: Jan, 2002

Tools and Metrics for Measuring Code Verbosity Posted: Jul 6, 2006 8:41 AM
Reply to this message Reply
Summary
In his latest developerWorks article on code quality, Andrew Glover discusses the relationship between high efferent and afferent coupling and long methods. He shows how to use PMD and JavaNCSS to measure metrics such as method and class length, and the numbers of unique types and public methods.
Advertisement

I once worked on a project that had an 11-page while loop. Refactoring such a long bit of code by factoring out methods that do one thing, and giving each method a good name that explains what the method does, can help readers of the code understand what's going on. Each method should be an understandable amount of code, preferably focused on doing just one thing, which makes it easy to name the method. Although the number of lines of code that will require depends on the situation, very long methods are often a sign that the code could be improved.

In Tame the Chatterbox: Tools and metrics for measuring code verbosity, Andrew Glover describes tools that can help detect verbose areas of code. How do you decide whether a method or class is too big, too small, or just right?


Morgan Conrad

Posts: 307
Nickname: miata71
Registered: Mar, 2006

Re: Tools and Metrics for Measuring Code Verbosity Posted: Jul 6, 2006 9:20 AM
Reply to this message Reply
He makes a good case for small methods. Then throws in that a class should also be short, with no supporting arguments, let alone evidence. In fact, his call to avoid "too many" imports contradicts the call to have short classes, cause shorter classes by definition lead to more imports.

While I'm not in favor of classes the length of War and Peace, I beleive that a mindless principle of "let's keep the class real short" frequently leads to procedural programming instead of Object-Oriented programming.

Fireblaze .

Posts: 21
Nickname: fireblaze
Registered: Jul, 2006

Re: Tools and Metrics for Measuring Code Verbosity Posted: Jul 6, 2006 10:39 AM
Reply to this message Reply
My decision if a class or method is to big is based in Readablity and "No duplicated code";

1) To read the only the name of the metod and try to figure out everything the metod does, it should only do what its name implies. (Readablity test)

2) Look at what the method does an split the diffrent stuff up in smaller method, eg Introducing free-extra functions. (Done with refactring tool so no errors will be introduced) Excelent example is Martin Fowlers DateRange.overlaps() http://www.martinfowler.com/ap2/range.html where its only one line of code in that method. overlaps() also introduce the includes-method, cause of the no duplicated code rule.

3) Ask another developer what he/she think your metod does by just reading the class name add method name like "What do you think daterange.expand() does?" If the developer answer correct you probably got a good name for your method.

4) Normally there is steps 1,2,3 which can be translated into step1(),step2(),step3()-functions, if there isnt a need for order then rewrite the code to run concurrently.

Anjan Bacchu

Posts: 18
Nickname: anjanb
Registered: Mar, 2002

Re: Tools and Metrics for Measuring Code Verbosity Posted: Jul 6, 2006 2:14 PM
Reply to this message Reply
hi there,

I agree.

While the guidelines published should work for majority of the codebases, there are codebases where atleast a few methods in many classes need to be bigger than 100 lines.

Sure, it's nice to have classes with less than 500 lines. It would be nice to know what is the size for infrastructure projects JDK, eclipse, netbeans, tomcat, hibernate, xerces/xalan, saxon and JBoss. How about the statistics for non-infrastructure projects like Compiere, OFBiz.

Sites like krugle, codezoo, koders can have such statistics for public source projects like above. This will enable people to find the average for a given category and measure themselves appropriately.

BR,
~A

Fireblaze .

Posts: 21
Nickname: fireblaze
Registered: Jul, 2006

Re: Tools and Metrics for Measuring Code Verbosity Posted: Jul 6, 2006 5:07 PM
Reply to this message Reply
A quick look at the Java API and source-code will tell you that class size is one problem.
Method-size however should be able to to stay within one page or the risk of voilating the "No Duplicated Code"-guideline is pretty high.
JTable is a prime example to look at with is huge code base and many methods, refactor that code is a challange which I hope someone will solve.

Jeff Ratcliff

Posts: 242
Nickname: jr1
Registered: Feb, 2006

Re: Tools and Metrics for Measuring Code Verbosity Posted: Jul 8, 2006 12:33 PM
Reply to this message Reply
> Method-size however should be able to to stay within one
> page or the risk of voilating the "No Duplicated
> Code"-guideline is pretty high.

Are these VT100 pages (from the era when these rules were first proposed) or 21-inch LCD monitor pages? It reminds me of a CASE tool from the mid-80's that followed this principle so religiously that it wouldn't allow you to write pseudocode more than two screens in length.

IMHO classes and methods should be whatever size is appropriate for the task at hand. Specific knowledge is always more valuable for solving a problem than generic rules.

Fireblaze .

Posts: 21
Nickname: fireblaze
Registered: Jul, 2006

Re: Tools and Metrics for Measuring Code Verbosity Posted: Jul 13, 2006 7:08 AM
Reply to this message Reply
>
> Are these VT100 pages (from the era when these rules were
> first proposed) or 21-inch LCD monitor pages? It reminds
> me of a CASE tool from the mid-80's that followed this
> principle so religiously that it wouldn't allow you to
> write pseudocode more than two screens in length.
>

One page is one page on both VT100 page and the 21-inch LCD monitor pages.

Fireblaze .

Posts: 21
Nickname: fireblaze
Registered: Jul, 2006

Re: Tools and Metrics for Measuring Code Verbosity Posted: Jul 13, 2006 7:10 AM
Reply to this message Reply
I did not and will not say how many lines a method should be, cause it should fit into one page, and page size varies between projects, time and other stuff.

Flat View: This topic has 7 replies on 1 page
Topic: Tools and Metrics for Measuring Code Verbosity Previous Topic   Next Topic Topic: Article: SOA Development with Geronimo GBeans

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use