Lines of Code Counter

Purpose

The purpose of this application is to compute the size of a source code file by counting the "logical lines of code" in the file.  The application is written in Java and has been tested under JRE 1.3 on Windows.

Download

Creative Commons License
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License.

Version 2.1     5-20-2002
    Download  LOC.jar 
 

Instructions for Use

If jar file associations were setup when you installed Sun's JVM, you can double-click on the LOC.jar file to launch it.  Otherwise, from the DOS prompt issue:
java -jar LOC.jar

 

1) Click the "Choose File" button to open a standard file chooser dialog and then navigate to the desired file.  When you select a file, the filename and the line count will appear in the display window.

2) Uncheck the check box if you don't want comments included in your line count.
 

Command Line Usage (Windows)

3) Count lines in a single file:
java -cp LOC.jar textui.LOC sourcefile.java

4) Don't count comments:
java -cp LOC.jar textui.LOC -n sourcefile.java

5) Count lines in a file in specified directory
java -cp LOC.jar textui.LOC  -d C:\somedir\ sourcefile.java

6) Count lines in multiple files from stdin
dir /b | java -cp LOC.jar textui.LOC

7) Count lines in multiple files in specified directory. Note: the directory name must end with a backslash.
dir /b C:\myproject | java -cp LOC.jar textui.LOC -d C:\myproject\
 

Counting rules

8) The rule is simple: we count any line that has more than 3 non-blank characters.

This rule means that a single brace or a comment line containing only double slashes won't count.

This rule places some responsibility on the programmer to format his or her code in a sensible fashion.
If you do silly things like this
 public static
    void main
      (Strings args[])
each line will be counted, which probably is cheating. The best way to manage this is to follow an established coding standard.
 

If you elect to NOT count comments, your source code needs to follow certain conventions to be counted properly:
 

// Lines that start with two slashes or
-- two dashes are considered comments

/* C-style comments can end on the same line like this */

/* A "block" comment may continue across
lines like this, as long as you started the first
line with the slash-star */

But if you start the line with code you
can't continue across lines /* by starting
                                at the end of a line
                                like this */

Because you should // use trailing comments
                   // like this
                   // instead

/* Similarly, don't put code
you want counted */ after a close block comment like this

We can't deal with nested block comments -- the first star-slash will
terminate the block (as do most compilers).
 

/*
Since there must be 4 or more non-blank characters on the
line to be counted, the above
line won't count (even if you are counting comments).
Nor will the following:
*/
 

Known  Anomalies

The HTML character &nbsp is treated like a character, not a blank. This can cause miscounts if you created your source by cutting and pasting from an HTML editor.
 

(Windows) If you specify a directory name on the command line that contains embedded blanks, you must omit the trailing quote. For example:

C:\>dir /b "C:\Try Me" | java -cp LOC.jar textui.LOC -d "C:\Try Me\

00403 C:\Try Me\Rational.java
00394 C:\Try Me\RationalDriver.java
00123 C:\Try Me\NaturalDriver.java
00231 C:\Try Me\Natural.java
-----
01151

Test Data File
Here is the file we used as test data to verify the software: LOCtestcase1.txt