1

I have to maintain a large old project project where my forerunners (for lack of version control) have turned large portions of code into comments. Now that we have everything in version control, I feel we no longer need these comments. The code in those comments has rotten anyway.

I would like to find the longest of those comments. It would be nice to have a way that gives me all files that have, let's say, more than 20 consecutive comment lines. As far as I can tell only // has been used. Finding /* */ is not necessary (probably even harmful because it'd find all the rotten javadoc... sigh).

bbuser
  • 918
  • 11
  • 33
  • "[Is there a quick way to find all the commented-out code across java files in Eclipse?](http://stackoverflow.com/questions/4144213/is-there-a-quick-way-to-find-all-the-commented-out-code-across-java-files-in-ecl)" is a similar question. I link [this answer](http://stackoverflow.com/questions/4144213/is-there-a-quick-way-to-find-all-the-commented-out-code-across-java-files-in-ecl/7677471#7677471). – bbuser Oct 20 '11 at 11:37

3 Answers3

1

Howabout this:

grep -ERn '^[[:space:]]*//' root_directory

The -E uses 'extended' regexes

The -R makes it recurse directories

The -n prints the filnames with line numbers

Then you can easily process this list to look for consecutive matches - just look for a block where the line number increases by one per line of output.

This assumes you don't want to match lines like this:

int x = foo; // some comment

But only lines that are completely comments:

// this is a comment

Update:

Here's a short Python script to process the output of the above grep:

import sys

filenames_of_interest = set()
filename = None
prev_line_num = 0
comment_count = 0
for line in sys.stdin.readlines():
        this_filename, line_num, _ = line.split(':',2)
        line_num = int(line_num)
        if this_filename != filename:
                comment_count = 0
                filename = this_filename
        elif line_num != prev_line_num + 1:
                comment_count = 0
        prev_line_num = line_num
        comment_count += 1
        if comment_count > 20:
                filenames_of_interest.add(filename)

print "files with blocks of comments:"
for i in filenames_of_interest:
        print ' ', i

It won't treat files with colons in their name correctly, but shouldn't be hard to modify to deal with that if it's an issue.

jwd
  • 10,837
  • 3
  • 43
  • 67
0

I have not tried it yet but Sonar looks promising.

EDIT: Seems that installation is a pain.

bbuser
  • 918
  • 11
  • 33
0

I would do like this:

Whenever I am updating a specific file , I would remove the comments before checking in. In that way "I will leave the campground cleaner than you found it" one by one.

Believe me!! the code will be cleaner in 3 months.

java_mouse
  • 2,069
  • 4
  • 21
  • 30