开发者

Measuring code reusability

开发者 https://www.devze.com 2023-01-06 00:47 出处:网络
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 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 sin开发者_运维技巧gle 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 ?


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.


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%.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消