Which metric(s) could help to indicate that开发者_开发百科 i have procedural code instead of object-oriented code? I would like to have a set of simple metrics, which indicate with a high probability, that the analyzed code contains procedural transaction scripts and an anemic domain model instead of following sound object-oriented design principles.
Would be happy about any set of useful metrics and tools for measuring.
Thanks, Thomas!
Some just out of my mind
- relative number of interfaces, abstract classes, etc. (more OO)
- member variable usage per method (more OO)
- number of methods in which the same member variable is used (more OO)
- quantity of static variables and methods (more procedural, specially static methods (some exceptions apply, like factories))
- lines of code per class (more procedural)
These don't ensure your code is more OO or more procedural, but might find helping those.
Cohesion and Coupling
Metrics focused on cohesion and coupling should give a good indication that your classes are real as opposed to collections of functions. There's a very readable paper here which I quote below to indicate why I think cohesion is a good metric:
"Cohesion refers to the relatedness" of module components. A highly cohesive component is one with one basic function. It should be difficult to split a cohesive component. Cohesion can be classified using an ordinal scale that ranges from the least desirable category (coincidental cohesion) to the most desirable (functional cohesion)."
The classic measurement of cohesion is Chidamber and Kemerer's "Lack of Cohesion in Methods" (LCOM). You can also look at Response for a Class (RFC) which measures the linkage between methods in a class.
You could consider design metrics such as NOC and DIT but my own experience is that they are too crude and easily confused by classes within a third-party framework.
You haven't said which language you use but there are some tools built around Eclipse so a quick web search for Eclipse based object oriented metrics should yield something useful
精彩评论