views:

39

answers:

2

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.

+1  A: 
  1. No
  2. What if a logical line of code is wrapped?
tob
@tob: Exactly my point. But if I started checking this, like for example in Python to wrap long lines, if someone used a \, how would I go about it? This way it would be very complex to deal with my program. However I just checked with `sloccount` and it also treats lines wrapped as two lines.
sukhbir
So a line such as: `print '1stline' \ ' second line'` is treated as two lines.
sukhbir
Sorry, I am not a Python guru. I just tried to answer the conceptual aspect of your question.
tob
@tob: Aah no problem, I was just pointing it out :)
sukhbir
Thanks for your answer, btw.
sukhbir
You're welcome. Please accept it if it suited your needs.
tob
+2  A: 

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.

slugster
@slugster: Aah I see. There are lots of technicalities involved in this. I think if I make my project broad, then it won't be feasible. If I stick with a single language, I think I can make it more complete.
sukhbir

related questions