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 Soundhariya,
You can take reference from this below code.
Integer classLines = 0;
List<String> lines= new List<String>();
for(ApexClass : [Select Body From ApexClass]){
                    lines = a.Body.split('\n');
                   
}
for(string str: lines){
    //system.debug(str);
    if(str.contains('/*')||str.contains('*/')||str.contains('//')){
        classLines++;
    }
}
system.debug('Apex Class lines: ' + classLines);

In case you find any other issue please mention. 
If you find your Solution then mark this as the best answer.