• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

LOC Counter

xbonez

New Member
Joined
Nov 29, 2010
Messages
1,182 (0.24/day)
Location
Philly, PA (US)
System Name Winter
Processor AMD Phenom II x4 965 BE @ 4.0Ghz
Motherboard MSI 790FX-GD70
Cooling Corsair H50 Liquid Cooling
Memory 2 x 2Gb Gskill Ripjaws 1600Mhz (7-7-7-24@1.6V)
Video Card(s) Asus GTX 470 @ Stock (Zalman VF3000 cooler)
Storage 2 x Samsung Spinpoint F3 500GB (RAID 0)
Display(s) Hanns G 28" @ 1920x1200
Case Antec 1200
Audio Device(s) Onboard -- TosLink --> Z5500
Power Supply Corsair 850TX 850W PSU
Software Win 7 64-bit Ultimate










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






Any comments, suggestions, possible feature additions etc., please let me know.
 
Last edited:

Kreij

Senior Monkey Moderator
Joined
Feb 6, 2007
Messages
13,817 (2.20/day)
Location
Cheeseland (Wisconsin, USA)
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?
Code:
private void MyMethod()
{
    if (SomethingIsTrue) DoSomething; else DoSomethingElse;
    return result;
}

VS.

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

    return result;
}
 
Last edited:

Easy Rhino

Linux Advocate
Staff member
Joined
Nov 13, 2006
Messages
15,448 (2.42/day)
Location
Mid-Atlantic
System Name Desktop
Processor i5 13600KF
Motherboard AsRock B760M Steel Legend Wifi
Cooling Noctua NH-U9S
Memory 4x 16 Gb Gskill S5 DDR5 @6000
Video Card(s) Gigabyte Gaming OC 6750 XT 12GB
Storage WD_BLACK 4TB SN850x
Display(s) Gigabye M32U
Case Corsair Carbide 400C
Audio Device(s) On Board
Power Supply EVGA Supernova 650 P2
Mouse MX Master 3s
Keyboard Logitech G915 Wireless Clicky
Software The Matrix
it really is incredible that amount of different programming languages available today. sort of overwhelming.
 

Kreij

Senior Monkey Moderator
Joined
Feb 6, 2007
Messages
13,817 (2.20/day)
Location
Cheeseland (Wisconsin, USA)
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

Linux Advocate
Staff member
Joined
Nov 13, 2006
Messages
15,448 (2.42/day)
Location
Mid-Atlantic
System Name Desktop
Processor i5 13600KF
Motherboard AsRock B760M Steel Legend Wifi
Cooling Noctua NH-U9S
Memory 4x 16 Gb Gskill S5 DDR5 @6000
Video Card(s) Gigabyte Gaming OC 6750 XT 12GB
Storage WD_BLACK 4TB SN850x
Display(s) Gigabye M32U
Case Corsair Carbide 400C
Audio Device(s) On Board
Power Supply EVGA Supernova 650 P2
Mouse MX Master 3s
Keyboard Logitech G915 Wireless Clicky
Software The Matrix
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

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,259 (4.63/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
Code:
private void MyMethod()1
{2
    if (SomethingIsTrue) DoSomething; else DoSomethingElse;3
    return result;4
}

VS.

private void MyMethod()1
{2
    if (SomthingIsTrue)3
    {4
        DoSomething;5
    }6
    else7
    {8
        DoSomethingElse;9
    }10
11
    return result;12
}
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

New Member
Joined
Nov 29, 2010
Messages
1,182 (0.24/day)
Location
Philly, PA (US)
System Name Winter
Processor AMD Phenom II x4 965 BE @ 4.0Ghz
Motherboard MSI 790FX-GD70
Cooling Corsair H50 Liquid Cooling
Memory 2 x 2Gb Gskill Ripjaws 1600Mhz (7-7-7-24@1.6V)
Video Card(s) Asus GTX 470 @ Stock (Zalman VF3000 cooler)
Storage 2 x Samsung Spinpoint F3 500GB (RAID 0)
Display(s) Hanns G 28" @ 1920x1200
Case Antec 1200
Audio Device(s) Onboard -- TosLink --> Z5500
Power Supply Corsair 850TX 850W PSU
Software Win 7 64-bit Ultimate
@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

Linux Advocate
Staff member
Joined
Nov 13, 2006
Messages
15,448 (2.42/day)
Location
Mid-Atlantic
System Name Desktop
Processor i5 13600KF
Motherboard AsRock B760M Steel Legend Wifi
Cooling Noctua NH-U9S
Memory 4x 16 Gb Gskill S5 DDR5 @6000
Video Card(s) Gigabyte Gaming OC 6750 XT 12GB
Storage WD_BLACK 4TB SN850x
Display(s) Gigabye M32U
Case Corsair Carbide 400C
Audio Device(s) On Board
Power Supply EVGA Supernova 650 P2
Mouse MX Master 3s
Keyboard Logitech G915 Wireless Clicky
Software The Matrix
to check logical loc could you have it read each line for characters and if none exist return 0 ?
 

xbonez

New Member
Joined
Nov 29, 2010
Messages
1,182 (0.24/day)
Location
Philly, PA (US)
System Name Winter
Processor AMD Phenom II x4 965 BE @ 4.0Ghz
Motherboard MSI 790FX-GD70
Cooling Corsair H50 Liquid Cooling
Memory 2 x 2Gb Gskill Ripjaws 1600Mhz (7-7-7-24@1.6V)
Video Card(s) Asus GTX 470 @ Stock (Zalman VF3000 cooler)
Storage 2 x Samsung Spinpoint F3 500GB (RAID 0)
Display(s) Hanns G 28" @ 1920x1200
Case Antec 1200
Audio Device(s) Onboard -- TosLink --> Z5500
Power Supply Corsair 850TX 850W PSU
Software Win 7 64-bit Ultimate
@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:

Code:
if (something) { do something; } else { do something else; }

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

Kreij

Senior Monkey Moderator
Joined
Feb 6, 2007
Messages
13,817 (2.20/day)
Location
Cheeseland (Wisconsin, USA)
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.
Code:
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. :D
 

xbonez

New Member
Joined
Nov 29, 2010
Messages
1,182 (0.24/day)
Location
Philly, PA (US)
System Name Winter
Processor AMD Phenom II x4 965 BE @ 4.0Ghz
Motherboard MSI 790FX-GD70
Cooling Corsair H50 Liquid Cooling
Memory 2 x 2Gb Gskill Ripjaws 1600Mhz (7-7-7-24@1.6V)
Video Card(s) Asus GTX 470 @ Stock (Zalman VF3000 cooler)
Storage 2 x Samsung Spinpoint F3 500GB (RAID 0)
Display(s) Hanns G 28" @ 1920x1200
Case Antec 1200
Audio Device(s) Onboard -- TosLink --> Z5500
Power Supply Corsair 850TX 850W PSU
Software Win 7 64-bit Ultimate
Haha, I wish I got paid by the line!
 

Kreij

Senior Monkey Moderator
Joined
Feb 6, 2007
Messages
13,817 (2.20/day)
Location
Cheeseland (Wisconsin, USA)
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. :laugh:

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

xbonez

New Member
Joined
Nov 29, 2010
Messages
1,182 (0.24/day)
Location
Philly, PA (US)
System Name Winter
Processor AMD Phenom II x4 965 BE @ 4.0Ghz
Motherboard MSI 790FX-GD70
Cooling Corsair H50 Liquid Cooling
Memory 2 x 2Gb Gskill Ripjaws 1600Mhz (7-7-7-24@1.6V)
Video Card(s) Asus GTX 470 @ Stock (Zalman VF3000 cooler)
Storage 2 x Samsung Spinpoint F3 500GB (RAID 0)
Display(s) Hanns G 28" @ 1920x1200
Case Antec 1200
Audio Device(s) Onboard -- TosLink --> Z5500
Power Supply Corsair 850TX 850W PSU
Software Win 7 64-bit Ultimate
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.


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.
 
Last edited:

Kreij

Senior Monkey Moderator
Joined
Feb 6, 2007
Messages
13,817 (2.20/day)
Location
Cheeseland (Wisconsin, USA)
Well, Xbones, the obvious next step is to write a full blown, adjustable rule, text parser and tokenizer. :p
 
Top