all that jazz

james' blog about scala and all that jazz

JSR-666 Extension: Significant whitespace

In many software development teams that I've worked on, code reviews are done by printing out the code to be reviewed. This is in turn read and scribbled on with red ink by developers suffering from a delusion that their coding standard is better than mine. An example of some such code:

public boolean equals(Object o)
{
   try
   {
      if (Calendar.getInstance().get(Calendar.MONTH) == 1
            && Calendar.getInstance().get(Calendar.DATE) == 29)
      {
         return true;
      }
      else
      {
         Thread.sleep(10000);
         return new Random().nextBoolean();
      }
   }
   catch (Throwable t)
   {
      // Something must have gone wrong, try again
      return new Random().nextBoolean();
   }
}

When reading the above code, the obvious problem that jumps out is that it contains no less than ten curly braces. When printed for a code review, this is a waste of valuable ink. The squids of the world have been working overtime to provide the ink needed to print out all the code that needs to be reviewed, it is about time we gave them a break. I'd therefore like to propose to the JSR-666 expert group an extension that curly braces be replaced with significant whitespace.

The previous code example would become:

public boolean equals(Object o)
   try
      if (Calendar.getInstance().get(Calendar.MONTH) == 1
            && Calendar.getInstance().get(Calendar.DATE) == 29)
         return true;
      else
         Thread.sleep(10000);
         return new Random().nextBoolean();
   catch (Throwable t)
      // Something must have gone wrong, try again
      return new Random().nextBoolean();

A fine piece of code that would pass any code review.

This feature will also solve a number of other problems, for example, it will eliminate the age old argument over whether braces belong on a newline or not. It will also force developers to write neatly structured code.

This feature will not require any JVM changes, but will require compiler changes. Existing code may need to be changed, however, if the existing code was well formatted to begin with, it should suffice to simply run the following command in vim:

:%s/\{|\}//g
comments powered by Disqus

About

Hi! My name is James Roper, and I am a software developer with a particular interest in open source development and trying new things. I program in Scala, Java, PHP, Python and Javascript, and I work for Lightbend as a developer on Lagom. I also have a full life outside the world of IT, am a passionate Christian, enjoy playing a variety of musical instruments and sports, and currently I live in Canberra.

I also have a another blog called Roped In about when my wife and I lived in Berlin for a year to help a church reconnect with its city.