function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Soundhariyaa MSoundhariyaa M 

How to get the number of lines of comments used in Apex code?

Hi All,

Is there any tools like static code analyzers or any other means to get the number of lines of comments used in our code in Salesforce

Please suggest me any solution!
ANUTEJANUTEJ (Salesforce Developers) 
Hi Soundhariyaa,

>> https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_expressions_comments.htm

As mentioned in the above link the comments are not interpreted by the parser so we won't be able to fetch the number of lines of comments.

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.
Suraj Tripathi 47Suraj Tripathi 47
Hi Soundhariyaa,
You can count the lines of apex but, I think there is no direct way to count the comment line in the code.
But there is the way you can count the lines but it might become tricky.
You can query the body of the apex class using SOQL query.
For Example:
List<ApexClass> classes = [Select Name,Body From ApexClass Where Name Like : '%NAMEOFCLASS%'];
System.debug('Classes-->'+classes);
In this list, you will be having the whole apex class in the body including comments. Then you can use the String methods to specifically count the comments i.e using split and slicing functions, etc.

If this helped you in finding your solution please mark it as the best answer.
Thanks and Regards
Suraj Tripathi 
ezekiel ernserezekiel ernser
I'm a certified admin with about 3 years experience and I'd like to start learning apex to diversify my skillset. I have no prior coding knowledge.

I'm guessing Trailhead is a pretty good place to start? Any other resources/classes you'd recommend? I could potentially get a course fee paid for by work so long as it isn't too crazy expensive.


Kabosucoin official website (https://kabosucoin.net/)