Does anyone have a script which can count the lines of apex code excluding any commented lines? Thanks!
-
4I'm not sure why people are voting for this to be closed: I might use such a script (or tool) if it was posted as an answer to this question.– Keith CNov 2, 2018 at 14:07
-
5@KeithC Possibly because LOCs are a notoriously useless metric. Though not a valid reason for closing a question, people may feel that a tool for counting LOCs is a tool that only a deeply misguided bean counter would be interested in.– StackOverthrowNov 2, 2018 at 16:46
-
@KeithC it's because it's a question that is broader than salesforce, a script to count lines of code excluding comments for java would be equally useful. Guessing people feel that this should be on stackoverflow.com since beyond the type of code we're counting it's not really a salesforce question.– Ralph CallawayNov 3, 2018 at 9:29
-
Lots of static code analysis tools charge for their usage based on lines of code being analysed, so knowing how many lines of code in an org would be beneficial in evaluating tooling pricing.– ClintFeb 3, 2020 at 5:34
3 Answers
If you're looking for a "from Salesforce perspective" count of lines, simply go to the Developer Console, click on the Tests tab, and find your class or trigger in the list. You'll see your code coverage (X/Y), where X is the number of lines covered by tests and Y is the number of lines of executable code, minus debug statements (comments and blank lines do not count). You can get a similar output from Salesforce DX's sfdx force:apex:test:run -c -r human -l RunLocalTests
command. Note that other tools, like the Code Scanner, will produce a different count of lines, since it includes debug statements.
Based on this question, I actually came up with a small java code, which you can run locally on any code. You can add/modify the code however you want. If at all it helps.
Note:- I have minimally tested this with few of the patterns and works as expected.
There's some limitation though, that it does not work with all possible scenarios, but only with the ones as below (is documented in the class too).
The following style of comments in any code will be ignored:
Ignored
/** * My comments */
Ignored
// any comment starting like this
Ignored
/* * My comments */
my comment
in this case will not be ignored/* my comment */
Here's what the code does:
- Parses any file
- Reads through the lines of the file
- Eliminates the pattern that as above
- Finally provides the count of non commented lines
The utility can be found on GITHub here.
-
-
It will handle multi-line comments too because the condition that I have used is line starting with
/
or*
. Nov 2, 2018 at 16:23 -
In a multiline comment only first line will have
/*
and all other lines except last line will be like normal code and it will count those lines right? I might be missing something..I will test quickly.– javanoobNov 2, 2018 at 16:25 -
1I see what you are saying .. I usually follow a
/* followed by * and then */
style of multi-line comment. But yes, the solution is just a starter, can be fine tuned. Let me see if I can accommodate that too. Nov 2, 2018 at 16:35
In "The Welkin Suite" IDE you can see in "View" -> "Code coverage" -> "Export to CSV" then in .csv you can already summarize the all the "Covered lines" (as "Lines"), "Uncovered lines" (as "UnLines") so in the exported table you can do any math you want with your test coverage data.