0

For simple laziness reasons, I get annoyed having to type the full System.out.println("..."); in Java code. 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. But I imagine that in a long program with many print statements, you could save yourself a lot of trouble.

4
  • On Eclipse (and I guess for others IDE), you can type sysout and use the autocompletion.
    – Alexis C.
    Commented Mar 30, 2014 at 16:45
  • 1
    "My guess is that it risks the programmer forgetting to close() the resource." But actually you shouldn't be closing the standard output stream. Commented Mar 30, 2014 at 16:53
  • If you use eclipse, you can type 'syso' then press ctrl + enter. Commented Mar 30, 2014 at 17:14
  • Off-topic, but use new PrintWriter(System.out, true) to wrap, which enables auto-flush; otherwise you'll have to explicitly call flush() every so often to get the printed text actually show up on stdout.
    – bdkosher
    Commented Mar 16, 2017 at 15:53

7 Answers 7

3

Why not use an 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:

soutTabTEST

Will produce:

System.out.println("TEST");

Incidentally, I'm not sure why you need to wrap the PrintWriter System.out in another PrintWriter; you can just do:

PrintWriter w = System.out;

And you shouldn't call close() on stdout...

4
  • +1 for auto-complete comment. What's the OP's point, reducing the final source-code, or reducing the amount of code you have to type? Commented Mar 30, 2014 at 16:48
  • @aliteralmind it's sad how many people don't know half of the short cuts their IDE is capable of. Commented Mar 30, 2014 at 16:49
  • I use PhraseExpress to roll my own auto completes. I work in TextPad. Never used an IDE. I type sys, and I get four options, one of which is if(...) { System.out.println(...); } Sweet. Commented Mar 30, 2014 at 16:51
  • @aliteralmind, nifty! NetBeans allows FreeMarker based autocomplete templates - all sorts of funky things are possible... Commented Mar 30, 2014 at 16:53
1

You could also import java.lang.System statically:

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");
    }
}
2
  • Boris is the FGITW :)
    – Blue Ice
    Commented Mar 30, 2014 at 16:46
  • 1
    But I'm in the east. So I win. Time zone is everything. Commented Mar 30, 2014 at 16:47
1

My solution to annoying "System.out.println" calls is to put this method somewhere:

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

Since primitives will be boxed, using Object will let it print any expression.

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.

0

You don't need that...

Just type sout and press Tab and your problem is solved in Intellij IDEA.

0
0

You should not rename it. There is a good reason that the method is named System.out.println();- we know that it is a System method. 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();. 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.

0

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'. 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.

0

From the 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. 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. 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:

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.