How to remove System.out.println's from codebase

amit picture amit · Feb 21, 2009 · Viewed 8k times · Source

We have a huge (old legacy java) code-base, where many files (around 5k) have System.out.println's. We are planning to remove them for cleanup/performance reasons. How can we write a script that will replace them without introducing any issues in the code? The script cannot blindly delete them as following case can be an issue:

if ()
  some.code...
else
  System.out.println(...);
DB.close();

I'm thinking of replacing them with ';'. That will take care of above case. Do you see any other issues? Any other suggestions?

Answer

OscarRyz picture OscarRyz · Feb 21, 2009

Have you consider the silly case:

System.out.println(" Print " +  object.changeState() );

I don't think it happen but chances are the println executes a method that is actually performing some action on which the system depends on and may introduce subtle bugs ( believe me or not, but I have witnessed this )

Probably replacing with a logger and disabling the loggers may do.

Or creating a null object using the NullObject pattern:

public final class DevNull { 
    public final static PrintStream out = new PrintStream(new OutputStream() {
        public void close() {}
        public void flush() {}
        public void write(byte[] b) {}
        public void write(byte[] b, int off, int len) {}
        public void write(int b) {}

    } );
}

And replacing

 System.out.println();

With

 DevNull.out.println();