Version 6 of Counting comments in a source

Updated 2006-01-16 17:50:35

In my humble opinion, quality control begins at the simple fact of evaluate how much comment lines proliferate in any program. Here is a simple GUI to perform this tasks. It prints :

  • The total line count
  • The comment line count
  • How many comment lines there are against 100 lines of code

-- Sarnold 13/01/2006

So, do you believe "more comments == high quality" or "fewer comments == high quality"? One can effectively argue both cases.

The real metric, IMO, is not how many comments, but how many useful comments. Sadly, there's no tool to give us that metric.


 # CommentCount.tcl - http://wiki.tcl.tk/15263 - Counting comments in a source
 # Simple GUI to counting comments in a source, it prints :
 #    * The total line count
 #    * count of lines classified as code & comment
 #    * How many comment lines there are against 100 lines of code

  proc LinesNComments {file} {
  #: Read file, count lines and comments
        set fd [open $file r]
        set comments 0
        set lines    0
        while {![eof $fd]} {
            gets $fd line
                  set line [string trimleft [string trimleft $line \t]]
                  if {[string equal $line ""]} {
                      continue
                  }
                  # skip comments lines with no words
                  if {[string index $line 0]=="#"} {
                if {![regexp {^\b*#+[a-zA-Z0-9]*} $line]} {continue}
                      incr comments
                  }
            incr lines
        }
        close $fd
        return [list $lines $comments]
  }

  proc Inspect {} {
  #: Inspect a file with TCL-source, show results in messagebox
         set file [tk_getOpenFile]
        if {![file exists $file]} {
        bell
        tk_messageBox -title "Error" -message "No such file: $file";return
      }
      foreach {lines comments} [LinesNComments $file] {}
        set code [expr {$lines-$comments}]
      if ($code==0) { 
        set percentage 0 
      } else {
        set percentage [expr {double($comments)*100/$code}] 
      }
      set    msg "File $file :\n"
      append msg "$lines lines of text: $code code lines and $comments comment lines,\n"
      append msg [format "for 100 lines of code there are %5.1f lines of comments." $percentage]
        tk_messageBox -title $::title -message $msg
  }

  #: Main :
  set title "CommentChecker"
  wm  title . $title
  label  .label1  -text "Check the use of comments in Tcl files"
  button .inspect -text "Inspect file" -command Inspect 
  button .quit    -text "Quit"         -command exit
  pack .label1 .inspect .quit

HJG After catching the divide-by-zero (e.g. for empty file), I also did some beautifications: titles for messageboxes, formatting of percentage, 'doc-strings' for the procedures...

Here is a small demo-program for testing this comment-counter:

  if 0 {
    This block never gets executed, so we can use it for comments:
    Demoprog #001
    Created 2006-01-16
    (c) by me
  }
  #
  catch {console show}  ;# When run from wish: open textmode-console
  puts "Hello World !"  ;# The traditional greeting
  # EOF #

This gives 10 text lines: 8 code lines and 2 comment lines --> 25% comments :-)


Category File