# LOC Counter



## xbonez (Dec 1, 2011)

The other day, I finished working on a web development project which had code scattered across numerous PHP, JS, HTML, CS and JS files and I was curious as to how many lines of code I must have written.

That gave me the idea to write an application that can take in a project directory and calculate the total number of lines of code. I had been meaning to learn WPF for a while, so I wrote LOC Counter as a WPF application. 






Without a doubt, a similar application already exists. The purpose of me coding LOC Counter wasn't so much to achieve a particular task as it was to learn. Developing LOC Counter taught me the following:

- WPF design principles - XAML code and Expression Blend
- Threading: Most of the work is done on a background thread keeping the UI thread unoccupied, which in turn, keeps the UI responsive.
- Use of AmCharts in WPF. AmCharts is an incredible and free charting library with support for WPF, Flash and HTML5/JS charts.






Being a WPF application, LOC Counter will have to be run in a windows environment with the .NET framework installed.








*Counts all lines of code in a specific project directory*





Begin by pointing the application to the root folder of your project. Once you click 'Start' the application reads through all files that contain source code and counts the number of lines of code.

* File extensions whitelist*





To ensure your final result is as accurate as possible, LOC Counter maintains a list of file extensions that contain source code.




You also have the option to include custom file extensions to the whitelist, and save the changes for future uses.


*Code Distribution*





Shows an interactive Pie chart of your code distribution across different file extensions. 

*Cancel Program Run*​

Because the bulk of the work is done by a background thread, the UI remains responsive for the user. At any point, you may hit the Stop button which will kill the background thread instantly and end the program run.

*Turbo*​

For extremely large projects ( > 100K lines of code), you may select the 'Turbo' option. This option makes the program run a lot faster at the expense of reduced visual animations.








Winrar SFX

*Video*
http://youtu.be/iG1YQ1Nx_dc​







*Any comments, suggestions, possible feature additions etc., please let me know.*​


----------



## Kreij (Dec 1, 2011)

Nice program. Works very slick.

One minor bug. 
After any given run, if you hit start again it does not clear the counter and you get 2x the number of code lines for the same project. Then 3x, 4x etc., if you keep hitting the start button.

Also a quick question. How many lines of code does you program see for each of the following?

```
private void MyMethod()
{
    if (SomethingIsTrue) DoSomething; else DoSomethingElse;
    return result;
}

VS.

private void MyMethod()
{
    if (SomthingIsTrue)
    {
        DoSomething;
    }
    else
    {
        DoSomethingElse;
    }

    return result;
}
```


----------



## Easy Rhino (Dec 1, 2011)

it really is incredible that amount of different programming languages available today. sort of overwhelming.


----------



## Kreij (Dec 1, 2011)

WPF (Windows Presentation Foundation) is more of a paradigm change than a language.
It incorporates XAML (similar to XML/HTML in structure) and uses C# for the code behind.
The paradigm change is to try to create a consistent separation between the UI and the business logic of the application.
It also dropped GDI and uses DirectX so it had 3D capability and utilized graphics hardware acceleration.
I really VERY powerful, but a little strange to get used to after coding in regular .Net languages.


----------



## Easy Rhino (Dec 1, 2011)

i only know how to program in java so to learn about incorporating xml style languages into an enterprise application seems like a lot more work. i actually just bought a java servlets and jsp book so that i can learn more about web based applications using java.


----------



## FordGT90Concept (Dec 1, 2011)

Kreij said:


> ```
> private void MyMethod()1
> {2
> if (SomethingIsTrue) DoSomething; else DoSomethingElse;3
> ...


4 and 12.  I put the number where the new line character appears.  All line break counts should only count 0x0D0A, 0x0A, or 0x0D in that order.

I wrote my own a long time ago that's just a drag and drop console program.


----------



## xbonez (Dec 1, 2011)

@Kreji: I remember seeing the same bug myself and am almost certain I fixed it. Must have undone the fix at some point. Its as simple as resetting the count to 0 everytime the start button is hit. I'll fix it.

Also, it counts Physical LOC and not Logical LOC (that would be quite a challenge), so it would count 4 and 12 as Ford posted.


----------



## Easy Rhino (Dec 1, 2011)

to check logical loc could you have it read each line for characters and if none exist return 0 ?


----------



## xbonez (Dec 1, 2011)

@Easy Rhino:

That would make sure empty lines are not counted (which isn't too difficult to fix), but it would still count this as 1 line:


```
if (something) { do something; } else { do something else; }
```

The above example would be 1 physical LOC but 4 logical LOCs


----------



## Kreij (Dec 1, 2011)

I've read a few discussions on LOC counters and there are some varying opinions.
Some people believe that empty lines and single braces should not count as LOC.

```
for (i = 0; i < 10; i++)
{

    a = i;

}
```

So the above would be two lines of "actual" code.
The people who get payed by the line for their code completely disagree.


----------



## xbonez (Dec 1, 2011)

Haha, I wish I got paid by the line!


----------



## Kreij (Dec 1, 2011)

Actually in your above example, xbones (post #9) there are people who feel that is exacly the oposite of what you stated.

They consider it 4 physical LOC, but since its a simple, complete expression (if-then-else) it only consititues one logical line of code.

It will always be debated and never agreed on as it's like the "should I use the goto command in my code", or the argument for and against declaring a variable of type "var" and letting the compiler determine the Type. I, of course, have my own opions on these two also but don't want to hijack your thread. 

Again, nice job on the program especially since you are just diving into WPF.


----------



## xbonez (Dec 1, 2011)

Yeah, the school of thought on LOCs is quite opinionated.

In the case of this application, it is meant to be used for projects scattered across numerous files, so I feel a ballpark figure is good enough.

In a later version, I could probably delve more into different ways to count lines, and possibly allow the user to choose.




Easy Rhino said:


> i only know how to program in java so to learn about incorporating xml style languages into an enterprise application seems like a lot more work. i actually just bought a java servlets and jsp book so that i can learn more about web based applications using java.



My experience with Java is limited to Android development (which I only recently got my hands dirty with), but isn't the UI for a Java application designed XML-style as well?

As a sidenote: I recently picked up Android dev, web development (PHP, HTML, CSS, JS etc.) and WPF and have never read a book in my life. Of course, different people have different ways they learn the best. For me, I've found reading tutorials online and throwing together code simultaneously works the best. The upside is you see results quick. The downside is it is an iterative process. the first iteration of your application will probably work, but won't incorporate a lot of the 'best practices'. You can iterate multiple times over the same project improving it as you learn.


----------



## Kreij (Dec 2, 2011)

Well, Xbones, the obvious next step is to write a full blown, adjustable rule, text parser and tokenizer.


----------

