views:

104

answers:

4

I am trying to measure how much of the code produced in our organisation is actually reusable and I'd like to set some guidelines. I'd like to have some reference to outer world:

How much code is typically reused in a single application ? More specifically - if we consider all the code of a complete end-user product (and eventually exclude 3rd party libraries), how many functions and methods are called from more than one place ?

What metrics are used to measure code reusability ? Are there any available numbers or studies for opensource and/or closed source software ?

+2  A: 

How much code is typically reused in a single application ?

IMO there is no "typical" application, especially not in this respect. Apps have wildly different architectures and execution flows, which result in different patterns of "reuse".

Consider a batch data processing app, which reads data from a file in a specific format, converts it to another format, then saves it. It practically has a single execution path, so not many methods are called from more than one place.

OTOH consider a plugin framework with several independent plugins which all use the same infrastructure layer, so functions in that layer are called from many different places.

You can't really say the design of the first app is worse than that of the second app (without actually going into the case-specific details).

Note also that the metric in the 2nd case is tricky: if you measure only the core framework itself without plugins, you get a low reuse count, but with the actual plugins the reuse count is higher. Since the plugins may be externally developed, you may not even have access to these, so your metric will be skewed.

Which leads to another point: reuse can happen on many levels. You can reuse code within an app, or between apps. The latter can be measured only by taking into account all the apps in question.

I think a better approach to this might be to start from the other end, and search for duplicated code (e.g. using tools like PMD for Java code). If you have large chunks of duplicated code in lots of places, you need to refactor.

Péter Török
I am aware of all these obstacles, but I still would like to do something and I need some sort of a reference.Different modules can be analysed separately. (haven't tried yet) A good approach is invesigating the distribution of number of uses per function (which I have already done - Number of uses on X axis, number of functions used X times on Y axis.) What I assume is, that these distributions will be similar for different large software packages, so it should be possible to compare with them. As a simple start, I would be interested, how many functions are not called more than once.
A: 

Interesting question, I don't know the answer but for a basic idea I'd look for some kind of pattern matching software to run through the code. Other than that, start refractoring into different class librarys. Maybe you could look at the business functionality for patterns to give you a high level idea?

Mr Shoubs
+2  A: 

If you define the question as to "how many functions are called from more than one place", you can technically build a static analyzer to answer that question; its just a matter of building a call graph and doing some counting (see this for a tool that extracts call graphs from C, Java and COBOL). As a practical matter, you might find that a lot more work that you are willing to do to answer this question directly.

You might consider running a clone detector across your code base. This will show code that has actually been reused (and should have been abstracted out somehow) by people doing copy and paste and provide precise metrics. Copying code like this is the most direct and common form of reuse.

I've been building clone detectors for about a decade. Almost every system I encounter, no matter what the langauge, has 20% of its code involved in clones (--> some 10% is reused). I've seen examples of up to 55%.

Ira Baxter
A: 

if you are working on a .NET platform, consider using NDepend to give you many metrics about your software. "Code reuse" is not avaibable as a metric directly (probably for reasons that other posters already mentioned), but things like coupling and cohesion might be of interest for you, too.

Even if you're not on .net, maybe the metrics definitions are helpful.

Philipp