简体   繁体   English

为什么不将System.out重命名为PrintWriter?

[英]Why not rename System.out as PrintWriter?

For simple laziness reasons, I get annoyed having to type the full System.out.println("..."); 出于简单的懒惰原因,我不得不输入完整的System.out.println("..."); in Java code. 在Java代码中。 Why would it be bad programming practice (if at all) to do something like this? 为什么做这样的事情是不好的编程习惯(如果有的话)?

import java.io.PrintWriter;
public class RenameSystemOut{
    public static void main(String[] args){
        PrintWriter w = new PrintWriter(System.out);
        w.println("Hello, world");
        w.close();
    }
}

My guess is that it risks the programmer forgetting to close() the resource. 我的猜测是,这有可能使程序员忘记close()资源。 But I imagine that in a long program with many print statements, you could save yourself a lot of trouble. 但是我想像一下,在一个包含许多打印语句的长程序中,您可以省去很多麻烦。

Why not use an import static : 为什么不使用import static

import static java.lang.System.out;

public class App {

    public static void main(final String[] args) throws Exception {
        out.println("TEST");
    }
}

If you use a decent IDE, like one of the big 3, then you can get it to type for you, in NetBeans: 如果您使用不错的IDE(例如3大规格之一),则可以在NetBeans中为其键入内容:

sout Tab TEST sout 标签测试

Will produce: 将产生:

System.out.println("TEST");

Incidentally, I'm not sure why you need to wrap the PrintWriter System.out in another PrintWriter ; 顺便说一句,我不确定为什么需要将PrintWriter System.out包装在另一个PrintWriter you can just do: 您可以这样做:

PrintWriter w = System.out;

And you shouldn't call close() on stdout... 而且你不应该在标准输出上调用close() ...

You could also import java.lang.System statically: 您还可以静态导入java.lang.System

import  static java.lang.System.*;

Which would then allow you to do this: 然后,您可以执行以下操作:

import  static java.lang.System.*;
public class RenameSystemOut{
    public static void main(String[] args){
        out.println("Hello, world");
    }
}

My solution to annoying "System.out.println" calls is to put this method somewhere: 我讨厌的“ System.out.println”调用的解决方案是将此方法放在某个地方:

public static void print(Object o) { System.out.println(o); }

Since primitives will be boxed, using Object will let it print any expression. 由于将对原语进行装箱,因此使用Object可以打印任何表达式。

You can of course name the method whatever you want, such as p , which is great for quick debugging statements, although be mindful of the balance between ease-of-writing and ease-of-reading. 当然,尽管要注意易写性和易读性之间的平衡,但是您可以随意命名该方法,例如p ,这对于快速调试语句很有用。

You don't need that... 你不需要那个

Just type sout and press Tab and your problem is solved in Intellij IDEA. 只需键入sout并按Tab ,您的问题即可在Intellij IDEA中解决。

You should not rename it. 您不应该重命名它。 There is a good reason that the method is named System.out.println(); 有充分的理由将该方法命名为System.out.println(); - we know that it is a System method. -我们知道这是一种System方法。 Without this information, anyone who uses your PrintWriter method might assume that it is from a different import or that it is a different function than System.out.println(); 没有此信息,使用您的PrintWriter方法的任何人都可能会认为它来自不同的导入,或者它与System.out.println();功能不同System.out.println(); . It's better coding style to leave it as is. 保持原样是更好的编码风格。

However, copy-pasting the line or using autocompletion will make it faster to type in, solving your original issue. 但是,复制粘贴该行或使用自动补全将使键入速度更快,从而解决了原始问题。

Why would it be bad programming practice (if at all) to do something like this? 为什么做这样的事情是不好的编程习惯(如果有的话)?

It's bad practice because it forces anyone looking at your code later to have to understand why you needed to define 'w'. 这是不好的做法,因为它迫使以后查看您的代码的任何人都必须理解为什么需要定义“ w”。 And when the answer is "because I was lazy", now you've just paid for your laziness for someone else's time. 当答案是“因为我很懒”时,现在您已经为别人的懒惰付出了代价。 That's not only rude, but expensive. 那不仅不礼貌,而且昂贵。

The cost of maintaining code is much higher than the cost of developing it. 维护代码的成本比开发代码的成本高得多。 After functionality, your main concern when writing code should ALWAYS be how easily it can be maintained. 使用功能之后,编写代码时主要要考虑的是如何轻松维护代码。

Making code short by using clever "tricks" may lead to interesting stackoverflow questions and answers, but they almost always work against you in the long run. 使用巧妙的“技巧”使代码简短可能会引起有趣的stackoverflow问题和答案,但是从长远来看,它们几乎总是对您不利。

From the Javadoc for PrintWriter: 从Javadoc for PrintWriter:

From Prints formatted representations of objects to a text-output stream. 从将对象的格式化表示形式打印到文本输出流。 This class implements all of the print methods found in PrintStream. 此类实现PrintStream中找到的所有打印方法。 It does not contain methods for writing raw bytes, for which a program should use unencoded byte streams. 它不包含写入原始字节的方法,程序应为此使用未编码的字节流。 Unlike the PrintStream class, if automatic flushing is enabled it will be done only when one of the println, printf, or format methods is invoked, rather than whenever a newline character happens to be output. 与PrintStream类不同,如果启用了自动刷新,则仅在调用println,printf或format方法之一时才执行此操作,而不是在输出换行符时才执行。 These methods use the platform's own notion of line separator rather than the newline character. 这些方法使用平台自己的行分隔符概念,而不是换行符。

I recommend that use System.out.println(data) or use a variable like this: 我建议使用System.out.println(data)或使用如下变量:

PrintStream out = System.out;
out.println("This is my output");

By another hand, think about if you wants to create a Console application or you want to log some messages or application status. 另一方面,请考虑要创建控制台应用程序还是要记录一些消息或应用程序状态。 In the second case, I think that you need to use a logging framework like log4j, Java Logging API or SLF4J. 在第二种情况下,我认为您需要使用log4j,Java Logging API或SLF4J之类的日志记录框架。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM