How is SLOC counted by Delphi IDE?

774 views Asked by At

You see pretty often here people saying that they have a x million of lines of code project. How is this measured? Is this number, the number shown under 'Information' menu? The manual says that only the compilable lines are counted (so, without comments and empty lines):

Source compiled -> Displays total number of lines compiled.

But the manual doesn't explain how a piece of code as if/then/else is counted:

if B=true
then 
   for i:= 0 to 100 
    do Stuff
else ;
  1. Is every line that has a blue dot is a "compiled line"?
  2. The Embarcadero code (the RTL and VCL code) and 3rd party libraries are also included into the count?
  3. (The conclusion) What does it mean when somebody says about a Delphi program that it has 1 million lines?
1

There are 1 answers

14
Jerry Dodge On BEST ANSWER

The Total lines the compiler tells you is counting the number of lines in the unit(s), regardless of what code is (or isn't) there. It even counts blank lines. Start a new project. Compile it and note the number of lines it says (mine says 42). Then, add just one line break somewhere, and compile again. It will claim there is one more line of code (43). So it does not seem the compiler takes any code into consideration for this number - only the actual line breaks.

In fact, if you add the total number of lines in the main form's unit (new project) as well as the project's main file, it will total to 2 less than what the compiler tells you (40 out of 42). So I wouldn't trust this number to mean much other than a rough estimate.

Libraries such as VCL, RTL, and Indy are not included in this count because those are pre-compiled. It is possible that your project might refer to a library or external unit which needs to be compiled, thus it will also include those into the count.

As far as your mention of how it counts if..then..else blocks, keep in mind that your 5 lines of code can be combined into just 1 line of code (stripping line breaks) and it will still compile, and the compiler will count only 1 line, not 5 lines.

EDIT

Years later, and I'd like to add a trick that I've learned to get a more accurate count of YOUR lines of code...

  1. Do a full build of the project.
  2. Open and make a small change to each and every one of YOUR units (such as just adding a space somewhere), and save changes.
  3. Do a compile (not build) of the project.
  4. Observe the lines of code it reports.

Since other units already have their DCU, and no changes have been made to them, the compiler doesn't recompile them. It only compiles those units which have actually changed since the last compile.

Build forcibly compiles everything in your project no matter what - and that might include third-party units that your project just happens to use. Unless the package has been marked for "Explicit Rebuild".

By default, new packages in Delphi are marked as "Rebuild as needed", meaning it will rebuild any units you use from such package. But when releasing a package, it's common to change this "Build control" setting to "Explicit rebuild", that way it will not be rebuilt with your project (only linked). And thus not contributing to your LOC.