Thread: Finding number of lines of code

  1. #1

    Finding number of lines of code

    This is my scenario :

    I wan't to find total number of lines which Iam handling in my current project , excluding blank lines and cmments . My code base is written in C and Linux platform .

    This shell script counts the total number of lines, but how can i find tune this to knock out blank lines and comments ?
    -----------------------------------------------------------------------------------------
    #!/bin/sh
    # recursive/countlines.sh
    total=0
    for currfile in `find . -name "*.[c|h]" -print`
    do
    total=$[total+(`wc -l $currfile| awk '{print $1}'`)]
    echo 'total=' $total
    done

    -----------------------------------------------------------------------------------------

    Can anyone help me on this ?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    egrep -v '^$'

  3. #3
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    You will need to deal with both single-line comments and multi-line comments.

    Years ago, I had a Lex-based program that stripped comments and blank lines from an input file. Though I no longer have the program, I do recall that there were productions for the following:
    • whitespace
    • printing characters
    • line continuation (\<eol>)
    • end-of-line (<eol>)
    • literal quotes (\" \')
    • character literals ('c')
    • string literals ("whatever, which can include \" and \' in an unbalanced manner, and may span multiple lines")
    • C comments (/* .... */ -- may be multi-line)
    • C++ style comments (// to <eol>)
    • blank lines (might contain whitespace and comments)

    There were other productions as well. Though I'm sure there are some ways to write it all as a single regular expression, I doubt that it's worth the effort.

    I would then run the result of this through the indent program to make the line-count less style-dependent, and then run that through wc to get a count of actual source lines. You should check out the man page for indent -- it may have comment and whitespace stripping options, which would save you a lot of trouble.

    A google search of "strip comments" seems to give a few leads on source files to do the dirty work in both C and perl, and I just spent a few seconds looking over the thing.
    Insert obnoxious but pithy remark here

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Given that all metrics can be abused (and are generally meaningless to begin with), you may as well just take the output of 'wc'. By the time you've finished messing about with all the different ways of doing things, the average answer from all of them will be about the same.

    A blank line can be just as useful to understanding the code, as a non-blank line in terms of achieving functionality.

  5. #5
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    I quite agree with Salem about the metric "number of lines". It is actually a very poor metric of productivity. A few lines of well constructed code are worth much more than several dozen lines of loose or simplistic code that achieve the same thing. However, if you're stuck in a situation where # lines of code (not counting comments) is a metric of performance imposed by unenligtened higher-ups, the level of precision in the line count is probably not critical.
    Insert obnoxious but pithy remark here

  6. #6
    ex-DECcie
    Join Date
    Dec 2005
    Posts
    125
    Quote Originally Posted by filker0
    I quite agree with Salem about the metric "number of lines". It is actually a very poor metric of productivity. A few lines of well constructed code are worth much more than several dozen lines of loose or simplistic code that achieve the same thing. However, if you're stuck in a situation where # lines of code (not counting comments) is a metric of performance imposed by unenligtened higher-ups, the level of precision in the line count is probably not critical.
    I agree as well, regarding line count as a measure of productivity.

    However, in those cases where we need a fairly good count, I've used a tool called SLOCcount, by David Wheeler.

    You can find it at www.dwheeler.com

  7. #7
    !anExpert
    Join Date
    Mar 2005
    Location
    pa
    Posts
    155
    there are also plenty of utils for this purpose on freshmeat

  8. #8
    Registered User
    Join Date
    Sep 2003
    Posts
    224
    My software engineering professor told me they used to measure productivity by the number of lines of code. So, programmers used to have a lot of fluff in their code, like:
    Code:
    x++;
    x--;
    x++;
    x--;
    // etc.
    Those were the days.

  9. #9
    Registered User Jaqui's Avatar
    Join Date
    Feb 2005
    Posts
    416
    Measuring productivity by number of line of code was really only used with procedural programming, in languages that did not support multi token lines.
    [ one line, one token ]

    old languages like cobol, fortran were like that. C was the first language to actually support multiple tokens per line.
    Quote Originally Posted by Jeff Henager
    If the average user can put a CD in and boot the system and follow the prompts, he can install and use Linux. If he can't do that simple task, he doesn't need to be around technology.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Line Counting
    By 00Sven in forum C Programming
    Replies: 26
    Last Post: 04-02-2006, 08:59 PM
  2. Finding a number within a number
    By jeev2005 in forum C Programming
    Replies: 2
    Last Post: 01-10-2006, 08:57 PM
  3. Counting the number of lines in a text file - help
    By Erkan in forum C Programming
    Replies: 11
    Last Post: 11-12-2005, 05:12 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM