Last active
August 29, 2015 14:20
-
-
Save charltonAthletic/bcddd1a5c4f1a720e773 to your computer and use it in GitHub Desktop.
How many comments in Apex classes / triggers? (Execute anon script)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Taken from: http://www.johnwestenhaver.com/2015/03/how-much-of-your-apex-is-comments.html | |
I am unsure why string method .replaceAll is needed but I assume it is because the .trim method doesn't remove white | |
space at the start of a line. | |
http://www.salesforce.com/us/developer/docs/dbcom_apex250/Content/apex_methods_system_string.htm | |
This is an execute anon script */ | |
Integer classLines = 0; | |
Integer classComments = 0; | |
Integer triggerLines = 0; | |
Integer triggerComments = 0; | |
List<String> lines = new List<String>(); | |
String line; | |
for (ApexClass a : | |
[SELECT Body FROM ApexClass WHERE NamespacePrefix = null]) { | |
lines = a.Body.split('\n'); | |
classLines += lines.size(); | |
for (Integer i = 0; i < lines.size(); i++) { | |
line = lines[i].replaceAll('\t', '').trim(); | |
if (line.startsWith('//')) classComments++; | |
} | |
} | |
for (ApexTrigger a : | |
[SELECT Body FROM ApexTrigger WHERE NamespacePrefix = null]) { | |
lines = a.Body.split('\n'); | |
triggerLines += lines.size(); | |
for (Integer i = 0; i < lines.size(); i++) { | |
line = lines[i].replaceAll('\t', '').trim(); | |
if (line.startsWith('//')) triggerComments++; | |
} | |
} | |
system.debug('Percent Comments: ' + | |
(Integer)(( | |
(Decimal)(classComments + triggerComments) / | |
(Decimal)(classLines + triggerLines)) | |
* 100) + '%'); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment