I'm looking for a code metric for monitor and track over time the size of several projects and their components.
Also, I would like to use it for:
- evaluate size reduction after refactoring
- compare the size/length of two implementations of the same specification, even across languages.
I know there are cyclomatic complexity and ABC metrics for complexity, but in addition to that I want a separate metric about the length/size/volume/extension of some code regardless of their complexity.
Being aware of the advantages and disadvantages of SLOC, I wouldn't use it for these purposes, mainly because I'm trying to measure code that is in different styles or languages.
For example this method body has 3 SLOC:
public static String threeLines(String arg1) {
String var1 = arg1 + " is";
String var2 = var1 + " something";
return var2;
}
Also this one:
public String otherThreeLines(String arg1) {
IntStream stream1 = Arrays.stream(arg1.split(";"))
.sequential()
.map(s -> s.replaceAll("[element", ""))
.map(s2 -> s2.replaceAll("]", ""))
.mapToInt(Integer::parseInt);
double var1 = stream1.mapToDouble(Double::new).map(d -> d / 2).sum();
return String.valueOf(var1);
}
Clearly, the second one is "bigger" or "longer", has more to read and think about, so I would like it to have a higher value in the metric.
There is no aim to evaluate if some piece of code is good or bad because of this metric, it's just for statistical analysis.
It would also be nice if it were simple to implement, without the need to fully parse file language.
So, I'm thinking of counting identifiers, keywords, and operators. for example this fragment
String var2 = var1 + " something";
could be analyzed as [String] [var2] [=] [var1] [+] [" something"];
and have a score of 6
And this fragment from the second method:
double var1 = stream1.mapToDouble(Double::new).map(d -> d / 2).sum();
could be analyzed as [double] [var1] [=] [stream1].[mapToDouble]([Double]::[new]).[map]([d] [->] [d] [/] [2]).[sum()];
and receive a score of 14
So the size/length of the second one should be roughly 2x of the first one.
Are there any known code metrics that would show similar results?
a=>a[a.map((e,i)=>!e&&(t=0,a.map((e,j)=>t+=(j-=i)&&e/j/j),t<m&&(m=t,k=i)),k=0,m=1/0),k]=1