Unclear what the Lines of Code metrics are
Closed this issue · 5 comments
Looking at a file, when I open it in VSCode there's 516 total lines (including code, comments, and blank lines). For this file, the report outputs:
- Lines: 515
- nSLOC: 485
- Comment Lines: 199
Number of lines of code + comment lines = 485 + 199 = 684 > 516. Is this a bug, or am I misunderstanding the definitions of these various line number metrics?
Hey @mds1,
- lines: total number of lines in code unit
- SLOC: source lines of code. only counts lines that contain source-code. e.g. if a function signature spans across 5 lines due to formatting, it is count as 5 lines.
- nSLOC: normalized source lines of code. only counts lines that contain source-code. it attempts to normalize certain line-spanning statements (e.g. function signatures) so that formatting doesn't heavily affect the counting.
- comment lines: this counts every line that contains a comment.
Note that a source line can contain code statements and comments and therefore can be greater than total lines. the comment lines metric is usually used to derive the comment ratio metric "how many comments are in the source unit compared to lines of code".
hope that helps.
cheers,
tin
Hm, something still seems off though. Wouldn't the above imply that 684 - 516 = 168 lines contain both code and comments on the same line? But just from looking at the file this does not seem to be the case.
Is it possible NatSpec comments are being counted as both code and comments? If so, is that intentional? This seems plausible as a quick manual count showed ~60 blank lines and ~130 lines of NatSpec comments. And if there's 130 lines of NatSpec comments in a 516 line file, there can't be also be 485 lines of source code
This is a different contract, but also seems to have a SLOC issue: https://github.com/gitcoinco/matching_contracts/blob/main/contracts/MatchPayouts.sol
Running "Report Metrics for Current File" reports that there are 160 lines, 160 nSLOC, and 79 comment lines. But from looking at the file, you can see this is incorrect as not every line is a line of code
@tintinweb I believe you're incorrect about what's displayed. I think the "nSLOC" stat does include comments because it comes from nsloc.total
(rather than nsloc.source
) here: https://github.com/ConsenSys/solidity-metrics/blob/722b52ab4437910cc5f8f9aec5d867b9753cf520/src/metrics.js#L339.
So "lines" is total lines (including comments), "nSLOC" is total lines (including comments) after normalization, and "comments" is how many lines are just comments. It would probably be more useful to have nSLOC only include lines with code. I believe, without testing at all, that this isn't simple subtraction because of blank lines.
oof, thanks guys, the nSLOC
count is indeed wrong as it counts all lines of the normalized source code.
I've introduced the following changes:
- nSLOC - is now
sloc.source
(only source code lines) - added
nLines
which is normalized source code lines (formerly the value ofnSLOC
) - added a short description
Here's a shot for your example @mds1:
I'm planning to get that out after some testing tomorrow.
cheers!