Question

As a hobby project and as a learning exercise, I decided to implement a software lines of code measurement script in Python.

However, I have a question:

  1. Are comments included in the measurement?
  2. The approach I have followed is: open the file, read it from start to end, count the number of lines. If comments are to be ignored, skip that line, else continue and increment counter. Is this how it is done?

Please note that I am aware many tools exist out there and perhaps better than mine, (sloccount is one example), however I am doing this as a completely hobbyist program.

Was it helpful?

Solution

You wouldn't normally count the comments as a line of code - but that can be a useful metric by itself, so maybe you should keep a count of them as you parse through the file.

You are better off checking for lines that are not whitespace, and end with a CRLF with no line continuation char. In regex speak that would mean you want to avoid lines like this (assuming the backslash is your line continuation char):

\\\s*\n\r

if you find a line like that, don't increment the counter. Of course that regex may differ depending upon which language (engine) you are using, and using a regex may not even be the most appropriate way to do it - a simple state engine may be better.

OTHER TIPS

  1. No
  2. What if a logical line of code is wrapped?

Wasn't that possible using a simple bash command, use that bash command in your Python script, import os and command :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top