I'm comparing two APIs to generate a method in C# and want to measure how "complex" the code to use them is. Consider:
API A:
MethodDeclaration(PredefinedType(Token(IntKeyword)), "CompareTo")
.AddModifiers(Token(PublicKeyword))
.AddParameterListParameters(
Parameter(Identifier("other")).WithType(IdentifierName("Person"))
API B:
new CodeMemberMethod
{
Attributes = Public | Final,
ReturnType = new CodeTypeReference(typeof(int)),
Name = "CompareTo",
Parameters =
{
new CodeParameterDeclarationExpression("Person", "other")
}
}
(If anyone's curious, API A is Roslyn SyntaxFactory, API B is CodeDOM.)
Notice how with API A, you have to care about things like tokens or identifiers, which in my opinion makes it more complex. But:
- If we count the number of lines, API B is much worse (4 lines vs. 10 lines). But part of that is due to the indentation style I use, which is not really about difference between the APIs.
- If we count the number of non-whitespace characters, API A is worse, but not by much (192 vs. 175). But in this metric, API B is hurt by its overlong names, which I think don't actually makes the code more complex.
So my question is: are there ways to measure this kind of code complexity, accepted in the industry or in academia? If not, is "number of non-whitespace characters" the best I can do?
Some other measures of complexity I rejected:
- Cyclomatic complexity is not relevant here, since both cases are non-conditional expressions.
- Kolmogorov complexity is a theoretical concept that can't actually be computed in practice. Maybe some kind of approximation of it could work?