Count number of lines of code?

How would I count the number of lines if code in my app?

TiA

Markus

would you like your app to count its own lines of code or would it be an external app that reads your code?

Either would be fine. I just sometimes see claims like “50,000 lines of code” and wondered how people count their lines. I now have a midsized app and would actually like to know …

If you save your code as VCP format (or the new Xojo default format), you can open all the *.xojo_code files and count the lines of code.

If you save it as XML you can do something similar but have to weed out the non code from the XML before counting the lines.

Save as XML and count the lines starting with the tag. Easy to do in a few lines of code with Instr().

Be aware that what you’re counting is “user written code” which is not “all the code that goes into your app”.
That’s a very different number as the IDE takes a number of things you do visually and writes code from that visual layout.
Windows, menus, container controls, etc all have this behavior.
Some you could write by hand (menus) but it’s tedious to do so - but possible.
Some you could not (like a Window & Container Controls) because they require very specific set up both at the OS & framework level to make work in a x-platform way and the code is something better left to be generated automatically as there are many gotcha’s along the way.

So when you “count lines” understand you’re not counting ALL the lines of code that the compiler sees and compilers to create your executable.

[quote=128159:@Norman Palardy]Be aware that what you’re counting is “user written code” which is not “all the code that goes into your app”.
That’s a very different number as the IDE takes a number of things you do visually and writes code from that visual layout.
Windows, menus, container controls, etc all have this behavior.
Some you could write by hand (menus) but it’s tedious to do so - but possible.
Some you could not (like a Window & Container Controls) because they require very specific set up both at the OS & framework level to make work in a x-platform way and the code is something better left to be generated automatically as there are many gotcha’s along the way.

So when you “count lines” understand you’re not counting ALL the lines of code that the compiler sees and compilers to create your executable.[/quote]

So the Microsoft way, and for press releases, you count the total number of lines, multiply by the men/years and pretend your app has 100,000 lines of code.

For your own interest, it’s much less :wink:

Save your project as XML and run this on it :

[code] dim f as FolderItem = GetOpenFolderItem(“”)
dim ligne as string
dim linenumber as integer
If f <> Nil Then
If f.Exists Then
// Be aware that TextInputStream.Open coud raise an exception
Dim t As TextInputStream
Try
t = TextInputStream.Open(f)
t.Encoding = Encodings.MacRoman
while t.eof = false
ligne = t.ReadLine
if instr(ligne,“”)>0 then
linenumber = linenumber+1
end if
wend
Catch e As IOException
t.Close
MsgBox(“Error accessing file.”)
End Try
End If
End If

msgbox “There are “+str(linenumber)+” lines.”[/code]

My latest app has 84089 lines :slight_smile:

CountFields might be faster.

Indeed. Thank you.

[code] dim f as FolderItem = GetOpenFolderItem(“”)
dim ligne as string
dim linenumber as integer
If f <> Nil Then
If f.Exists Then
// Be aware that TextInputStream.Open coud raise an exception
Dim t As TextInputStream
Try
t = TextInputStream.Open(f)
t.Encoding = Encodings.MacRoman
ligne = t.ReadAll
Catch e As IOException
t.Close
MsgBox(“Error accessing file.”)
End Try
End If
End If

msgbox “There are “+str(Countfields(ligne,””))+" lines."[/code]

Much faster (instant), it find 84090 lines for my app, one line more than the previous one :wink:

Yes, you have to subtract one since it’s counting whatever is before the first SourceLine.

$ grep -n SourceLine myProj.xojo_xml_project

Or on Windows

C:\\> find /c SourceLine myProj.xojo_xml_project

will work also, pretty quickly, for those who don’t mind the command line.

One other thing to take into account is that these methods will include All code, and I’m not sure about everyone else’s projects, but I have a lot of helpers in mine that are shared all over the place. Third party code as well. It really isn’t fair to count that, if you are trying to determine how many lines of code you wrote at least.

[quote=128253:@Jeremy Cowgar]$ grep -n SourceLine myProj.xojo_xml_project
[/quote]

I think that option is supposed to be -c, not -n.

One other thing to take into account is that these methods will include All code, and I’m not sure about everyone else’s projects, but I have a lot of helpers in mine that are shared all over the place. Third party code as well. It really isn’t fair to count that, if you are trying to determine how many lines of code you wrote at least.[/quote]

Counting lines is an imprecise thing anyway. Beyond helpers, there is also the formatting style. I tend to clearly separate blocks with empty lines that can hardly be consodered code, for instance. By definition, code quality is much more important, but difficult to evaluate.

The example of using countfields is good here. Spaghetti code produces a lot of lines, yet it is not efficient.

I tend to regard the number of lines as a marketing argument more than anything else.

I am sad… my biggest and most valueable App just has 54.000 lines of code using grep -c

:frowning:

Well, if your coding goes like mine then you have deleted 95% of the code you wrote, so multiply by 20 to get how much you have done :slight_smile:

And multiply with 20 again because of all the work of Ruslan, Christian and Björn.

But surely they get it right first time :wink:

Stupid forum…

or congrats is in order because of your very efficient code!