Home > Apex > How many lines of Apex code do you have?

How many lines of Apex code do you have?

February 9th, 2010

I have always been curious how many lines of Apex code I have written. Is it a couple hundred (haha, ya right), several thousand, a gazillion million trillion? There was never really a good way to figure this out unless you pulled down all of the data and inspected it file by file. Lame.

Enter Spring 10 and the removal of limits on collection size. Dooo da doooooo (trumpet music)!!!!!!! The data to figure out the number of lines has always been available but it has been difficult to process. All apex class and trigger code is stored in two tables that can be accessed by SOQL. The first part is easy, simply query all of the data, and count the lines. Before Spring 10 there was no good way to count the lines. There is no apex method to count the number of string matches, ie: count number of ‘\n’ breaks. There is a split method that cuts up a string based on pattern matches. What we can do is populate a list of strings split by \n. This will create one entry in our list for each line. TADA! Before Spring 10 this would not have worked if a class had more than 1000 lines as a list could only hold 1000 elements.

Now I know lines of code is totally subjective. Some weirdos will write code like this, 7 lines:

public void doSomething()
{
	if(something)
	{
		//do awesomeness
	}
}

While the cool kids will do it like this, 5 lines:

public void doSomething(){
	if(something){
		//do awesomeness
	}
}

However your write code, the weirdo way or the cool kid way, it is just darn fun to see how many lines you have written.

Here is the code that will give you that answer. Simply copy and paste this to your system log and hit execute. Then dig through a giant debug log that is still horrible (rant on that later) for 3 simple debug lines.

13:35:17.470|USER_DEBUG|[14,1]|DEBUG|Apex Class lines: 21388
13:35:17.470|USER_DEBUG|[15,1]|DEBUG|Apex Trigger lines: 1425
13:35:17.470|USER_DEBUG|[16,1]|DEBUG|Apex Total lines: 22813

Integer classLines = 0;
Integer triggerLines = 0;
 
for(ApexClass a : [Select Body From ApexClass]){
	List<String> lines = a.Body.split('\n');
	classLines += lines.size();
}
 
for(ApexTrigger a : [Select Body From ApexTrigger]){
	List<String> lines = a.Body.split('\n');
	triggerLines += lines.size();
}
 
system.debug('Apex Class lines: ' + classLines);
system.debug('Apex Trigger lines: ' + triggerLines);
system.debug('Apex Total lines: ' + (classLines+ triggerLines));
Categories: Apex
  1. Richard
    February 9th, 2010 at 14:27 | #1

    16:26:06.636|USER_DEBUG|[14,1]|DEBUG|Apex Class lines: 80691
    16:26:06.636|USER_DEBUG|[15,1]|DEBUG|Apex Trigger lines: 2685
    16:26:06.636|USER_DEBUG|[16,1]|DEBUG|Apex Total lines: 83376

    Of course that is on our ent unlimited org so it isn’t all my code.

  2. Hannes
    March 15th, 2010 at 00:30 | #2

    9:28:34.414|USER_DEBUG|[14,1]|DEBUG|Apex Class lines: 3200
    9:28:34.414|USER_DEBUG|[15,1]|DEBUG|Apex Trigger lines: 624
    9:28:34.414|USER_DEBUG|[16,1]|DEBUG|Apex Total lines: 3824

    I am the only one writing code and on it since ~half a year.

  3. Ryan
    March 17th, 2010 at 15:49 | #3

    18:47:5.902|USER_DEBUG|[14,1]|INFO|Apex Class lines: 14683
    18:47:5.902|USER_DEBUG|[15,1]|INFO|Apex Trigger lines: 1267
    18:47:5.902|USER_DEBUG|[16,1]|INFO|Apex Total lines: 15950

    Solo developer – just over 9 months… we were well above this – raw code has been brought down through consolidating logic

  4. SydneyForce
    May 11th, 2010 at 20:39 | #4

    14:37:35.470|USER_DEBUG|[14,1]|DEBUG|Apex Class lines: 16933
    14:37:35.470|USER_DEBUG|[15,1]|DEBUG|Apex Trigger lines: 3670
    14:37:35.470|USER_DEBUG|[16,1]|DEBUG|Apex Total lines: 20603

    My org is up to 750k of 1 million characters. Anyone had any luck with extending their Apex character limit by contacting Salesforce?

    It sounds like some of you might be close with the number of total Apex lines you have.