mauricioaniche / ck

Code metrics for Java code by means of static analysis
Apache License 2.0
375 stars 145 forks source link

Error writing the CSV file containing the class metrics #82

Closed slivkaje closed 3 years ago

slivkaje commented 3 years ago

Hello,

There seems to be an issue with writing the metrics for class results in the file https://github.com/mauricioaniche/ck/blob/master/src/main/java/com/github/mauricioaniche/ck/ResultWriter.java. Some values are shifted. For example, result.getVisibleMethods().size() is written where abstractMethodsQty should be, abstractMethodsQty is written where finalMethodsQty should be and so on. The majority of metrics are ok; this affects just the abstract method quantity and the fields' numbers.

This is what the CSV row should look like according to the header:

private static final String[] CLASS_HEADER = { "file", "class", "type", "cbo", "wmc", "dit", "rfc", "lcom", "tcc", "lcc", "totalMethodsQty", "staticMethodsQty", "publicMethodsQty", "privateMethodsQty", "protectedMethodsQty", defaultMethodsQty", "abstractMethodsQty", "finalMethodsQty", "synchronizedMethodsQty", "totalFieldsQty", "staticFieldsQty", "publicFieldsQty", "privateFieldsQty", "protectedFieldsQty", "defaultFieldsQty", "visibleFieldsQty", "finalFieldsQty", "synchronizedFieldsQty", "nosi", "loc", "returnQty", "loopQty", "comparisonsQty", "tryCatchQty", "parenthesizedExpsQty", "stringLiteralsQty", "numbersQty", "assignmentsQty", "mathOperationsQty", "variablesQty", "maxNestedBlocksQty", "anonymousClassesQty", "innerClassesQty", "lambdasQty", "uniqueWordsQty", "modifiers", "logStatementsQty" };

and this is the actual row (differences are bolded):

this.classPrinter.printRecord(result.getFile(), result.getClassName(), result.getType(), result.getCbo(), result.getWmc(), result.getDit(), result.getRfc(), result.getLcom(), result.getTightClassCohesion(), result.getLooseClassCohesion(), result.getNumberOfMethods(), result.getNumberOfStaticMethods(), result.getNumberOfPublicMethods(), result.getNumberOfPrivateMethods(), result.getNumberOfProtectedMethods(), result.getNumberOfDefaultMethods(), result.getVisibleMethods().size(), result.getNumberOfAbstractMethods(), result.getNumberOfFinalMethods(), result.getNumberOfSynchronizedMethods(), result.getNumberOfFields(), result.getNumberOfStaticFields(), result.getNumberOfPublicFields(), result.getNumberOfPrivateFields(), result.getNumberOfProtectedFields(), result.getNumberOfDefaultFields(), result.getNumberOfFinalFields(), result.getNumberOfSynchronizedFields(), result.getNosi(), result.getLoc(), result.getReturnQty(), result.getLoopQty(), result.getComparisonsQty(), result.getTryCatchQty(), result.getParenthesizedExpsQty(), result.getStringLiteralsQty(), result.getNumbersQty(), result.getAssignmentsQty(), result.getMathOperationsQty(), result.getVariablesQty(), result.getMaxNestedBlocks(), result.getAnonymousClassesQty(), result.getInnerClassesQty(), result.getLambdasQty(), result.getUniqueWordsQty(), result.getModifiers(), result.getNumberOfLogStatements());

jeandersonbc commented 3 years ago

Hi @slivkaje, thanks for reporting this issue!

Maybe this is a good time to rethink how the result is written to CSV. The way ResultWriter.java writes the CSV entry is very error-prone and this type of thing can happen again in the future.

For now, I'm submitting a fix for it.

jeandersonbc commented 3 years ago

See PR https://github.com/mauricioaniche/ck/pull/83

mauricioaniche commented 3 years ago

Thanks, @slivkaje for noticing the bug, and @jeandersonbc for fixing it. I'll release a new version right away.