How is SLOC counted by Delphi IDE?
Asked Answered
P

1

2

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?
Peatroy answered 18/5, 2014 at 12:44 Comment(6)
Looks like Embarcadero code is not counted. This is gooood.Peatroy
It is trivially easy for you to work this out for yourselfWinkler
I don't think I've ever seen someone on Stack Overflow talk about how many millions of lines of code they have. Where have you been reading? If you're unclear what someone meant, why didn't you directly ask that person to clarify? And where does the manual say that only compilable lines are counted? (And what makes you think comments and blank lines aren't compilable, anyway? The compiler obviously accepts them.)Polybasite
@RobKennedy-Hi Rob. You can actually see the "I have x million lines of code program" quite often here on stack overflow. Here is an example by Mason Wheeler: #2188979Peatroy
Here is another example: #2645473Peatroy
"And what makes you think comments and blank lines aren't compilable, anyway?" - If the compiler compiles blank lines, what kind of ASM code does it generate for them? :)Peatroy
A
4

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.

Ablative answered 18/5, 2014 at 14:16 Comment(14)
It depends on whether the source of those libraries needs to be compiled or not. VCL and RTL are certainly not counted, because they're already compiled.Ablative
Interesting find. There seems to be 2 lines coming up from somewhere else however, the smaller I can make is program Project1; begin end. which counts as 3 lines. The smallest blank new VCL forms application is 6 lines, because there's an additional unit.Croft
I wonder how one could be able to come up with such incredibly useless idea of counting loc. And put it in a product which is sold around the world.Croft
.. in which case I'll note this question. There might be some alternatives there with some intellect.Croft
Moral of the story: you can't always trust information, even if it's given to you by a very credible source.Ablative
@SertacAkyuz, the only metric which makes some sense is decimal order of magnitude. Exact count is completely useless for anything besides code golf.Griselgriselda
@JerryDodge, no, DFM is binary resource stored in the text format for your convenience, *PROJ is Microsoft replacement for makefile (or build.xml if you wish).Griselgriselda
Anyway, LoC number is not about "weight", it is about approximate program code complexity, eg: if INT(log10(SLOC(Program1))) > INT(log10(SLOC(Program2))) then you can say with certain confidence what amongst two similar programs, Program1 is more complex than Program2. That's all.Griselgriselda
@JerryDodge, because you were totally wrong with those ideas, yet were trying to argue. Nothing is unrelated here because the question itself is incredibly unspecific.Griselgriselda
@FreeConsulting I already confirmed that you were right and I was wrong, yet you continued to comment about it. Anyway I'm deleting all my comments in this conversation, no need to keep going on and on.Ablative
So when somebody says that his Delphi program is 1 million lines, it literary means nothing else than 'My program compiles'?Peatroy
@Altar Essentially it's probably actually something like 700k lines, but imagine if the developer has a habit of putting something like 10 line breaks in-between every function and winds up with more line breaks than actual code...Ablative
But how do you know if the program is actually 10K and the rest are 3rd party libraries? Jedi is pretty big :)Peatroy
@Altar It all depends on how the library was designed and installed, usually large libraries like that are pre-compiled and don't need to be compiled again, but it is still possible that a library can be designed where it's compiled along with the app. One example I can think of is SuperObject which is a JSON unit, and needs to be compiled with the app.Ablative

© 2022 - 2024 — McMap. All rights reserved.