如何从代码库中删除 System.out.println [英] How to remove System.out.println's from codebase

查看:22
本文介绍了如何从代码库中删除 System.out.println的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个巨大的(旧的 Java)代码库,其中许多文件(大约 5k)都有 System.out.println 的.我们计划出于清理/性能原因删除它们.我们如何编写一个脚本来替换它们而不在代码中引入任何问题?脚本不能盲目删除它们,因为以下情况可能是一个问题:

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?

推荐答案

你有没有考虑过这种愚蠢的情况:

Have you consider the silly case:

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

我认为这不会发生,但是 println 可能会执行一个方法,该方法实际上正在执行系统所依赖的某些操作,并且可能会引入细微的错误(信不信由你,但我已经目睹了这一点)

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.

或者使用 NullObject 模式创建一个空对象:

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) {}

    } );
}

并替换

 System.out.println();

 DevNull.out.println();

这篇关于如何从代码库中删除 System.out.println的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆