Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8174145

Improve Java programmer productivity by shortening print statements.

XMLWordPrintable

    • generic
    • generic

      A DESCRIPTION OF THE REQUEST :
      This is not a huge problem, but think about how much time(in total) is spent typing out System.out.println() or similar output methods for debugging/testing. How much less time is spent writing print() in Python, for instance? And which is more fun to type out?

      It's easy enough to write a wrapper method, here is the shortest possible(minimized) one, I believe:

      void p(String s){System.out.println(s);}

      Unfortunately, using a shortened hand-written version is not something that everyone does. So typing the full version out consumes some significant time when you consider all Java programmers together, and it's also a very slight annoyance each time one needs to do it.

      In my opinion, wrapper methods print() and println() should be included by default without even importing a package. This could be done without breaking existing code by allowing it to be overridden, so no changes would be needed to files with existing methods defined with the signature void print(String s). Since this improvement only comes into play when writing new code, there isn't really a problem with it not affecting existing code.

      The main risk I can think of is a potential limitation of Java, which I am not intimately familiar with enough to know whether it is an actual issue or not. The question is whether Java could have a built-in print() method without including it in a package in the standard library.

      If that is a current limitation, it's possible that Java could be extended to have one special package(named something like System.DefaultPackage) which is included by default, without any import statements or having to call as System.DefaultPackage.print(). Some changes to Java would possibly be needed to call these undeclared default methods without including the package/namespace. This would also leave the door open for easily shortening other long and frequently-used core methods. Perhaps something like this already exists and the print() methods could be included there?

      The other(minor) downside of this is one additional call on the stack for each call to the new print() methods, as it then calls System.out.print(), unless there is a way we could eliminate that. But this is unlikely to affect production code for System.out.print()/println() since it is not commonly included in final code (at least, it generally doesn't need to be).

      The last issue is if a developer is working with unfamiliar code which has a method overriding the new print()/println() methods, but doesn't realize it, he/she may get unexpected behavior, but this would be a pretty easy thing to figure out.

      This is such a simple idea that it's tough to think what else could go wrong with this, and whether it could even introduce an application failure. I'm sure it's a possibility, but I think it would be unlikely.

      The main open design issue is where exactly this should be implemented. This proposal includes generally what needs to be done but needs more information on where.

      JUSTIFICATION :
      This would make just about every Java developer's life just a little bit better. It seems like a low risk, low implementation effort, nice reward type of improvement.

      Success could be measured in time saved by developers. To do this, we could measure the amount of time that it takes to type out each method along with the # of times each is included in a source code file.

      This could demonstrate both the average frequency of these methods being used, and also the average time savings, which together could demonstrate whether this is a high impact change.

      We could also measure improved developer happiness, via a survey or comments from the Java community.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      When a programmer types print() or println(), programmers would like to see Java print to standard output.
      ACTUAL -
      Unless these methods are declared/defined, the program will not compile. We are losing bits of coding time by many Java developers every day, which in sum is a significant amount of time and minor inconvenience.

      CUSTOMER SUBMITTED WORKAROUND :
      Any programmer can define a wrapper method(such as the one above) in each source code file.

            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: