Does anyone have a script which can count the lines of apex code excluding any commented lines? Thanks!
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:
/** * My comments */
// any comment starting like this
/* * My comments */
my commentin 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.