Running on Java 22-ea+15-1134 (Preview)
Home of The JavaSpecialists' Newsletter

050Commenting out Your Code?

Author: Dr. Heinz M. KabutzDate: 2002-06-10Java Version: 1.1Category: Language
 

Abstract: Source control systems give us insight into how a method has changed over time. It is usually a bad idea to comment out code, especially without adequate comments why. It confuses the person maintaining the code.

 

Welcome to the 50th edition of The Java(tm) Specialists' Newsletter. Last week I thought that I was presenting my Design Patterns Course at "The Shuttleworth Foundation". I really thought I was. However, when I arrived at TSF, I discovered that I had gone to the wrong place :-( My booking had been with Thawte Consulting, the company that Mark Shuttleworth sold to Verisign, and TSF has nothing to do with Thawte. I had simply assumed, and assumption is one of the biggest failings of a programmer.

When I finally got there, I was amazed that all my students were there already, waiting for me. (The course was supposed to start at 8:30 and I got there at 8:20). The next day, when I arrived at 8:20 again (after missing my turnoff due to daydreaming), the students were *again* all there, waiting for me. WOW, I thought, these guys are *really* keen! The bubble burst when they told me that somehow they got the message that the course started at 8:00 instead of 8:30!

The course went extremely well, partly due to very bright students and partly because I am getting more comfortable presenting this new course. Last Monday at 2:00am I at long last *clicked* with the GoF Factory Method. If you think you understand the Factory Method (like I did for a few years), my bet is that you actually don't ;-) You might understand a different type of pattern commonly known as "Factory Method", but there are very few people who understand the Factory Method according to GoF.

javaspecialists.teachable.com: Please visit our new self-study course catalog to see how you can upskill your Java knowledge.

Commenting out Your Code?

Something I dislike even more than useless comments is code that is commented out. When I work with code, instead of commenting it out, I delete it. That way I don't have to remember why I commented out broken code, and it makes my code much easier to maintain. It's like telling the truth: you don't have to remember what you said. However, this newsletter is not a rant about commented out code, it is about how even commented out code can cause compiler failures, and how commented out code can end up as part of your class.

I want to thank Clark Updike from https://www.jhuapl.edu for the ideas that sparked this newsletter. He picked it up while studying for the Sun Certified Java Programmer (SCJP) Examination, which I did not author, by the way. I know the guys at Sun can be quite evil with the SCJP, but I don't think they would go this far. The SCJP seems to be getting more difficult to keep pace with the immense number of Java developers arriving on the scene. When I wrote it, you basically had to show up to get 91%.

I promised you last week that I would show you some code that could not compile, even though the problem code was commented out. Let's first look at the original program:

public class A1 {
  Character aChar = new Character('\u000d');
}

Try compile it, and you will get an error, such as:

A1.java:2: illegal line end in character literal
  Character aChar = new Character('\u000d');
                                  ^
1 error

Imagine sitting with this code, and not getting it to compile. What would you do? You would probably comment out the offending line to try get it to compile:

public class A2 {
  // Character aChar = new Character('\u000d');
}

You compile it, and what do you get: 2 errors instead of the 1 that you had without the comment!

A2.java:3: unclosed character literal
}
^
A2.java:3: <identifier> expected
}
  ^
2 errors

But we can get even more confusing, have a look at class A3:

public class A3 {
  // Character aChar = new Character('\u000d{System.out.println("Hello");}
}

Amazingly, it compiles, but does it do anything? Let's have a look at A3Test.java:

public class A3Test {
  public static void main(String[] args) {
    new A3();
  }
}

The test compiles, and when we run it, the output is:

Hello

Say what? I'm sure that you know what happens. The unicode character '\u000d' gets converted to a newline character as part of the "preparation for compile". At some point, before the class A2 actually gets compiled, it looks like this:

public class A2 {
  // Character aChar = new Character('
');
}

Obviously that does not compile! Similarly, at some point, A3 looks like this:

public class A3 {
  // Character aChar = new Character('
{System.out.println("Hello");}
}

I wish I could say: There, you should not comment out code! However, I can only say that you will get strange effects if you use '\u000a' or '\u000d' in your code.

Until the next newsletter ...

Heinz

 

Comments

We are always happy to receive comments from our readers. Feel free to send me a comment via email or discuss the newsletter in our JavaSpecialists Slack Channel (Get an invite here)

When you load these comments, you'll be connected to Disqus. Privacy Statement.

Related Articles

Browse the Newsletter Archive

About the Author

Heinz Kabutz Java Conference Speaker

Java Champion, author of the Javaspecialists Newsletter, conference speaking regular... About Heinz

Superpack '23

Superpack '23 Our entire Java Specialists Training in one huge bundle more...

Free Java Book

Dynamic Proxies in Java Book
Java Training

We deliver relevant courses, by top Java developers to produce more resourceful and efficient programmers within their organisations.

Java Consulting

We can help make your Java application run faster and trouble-shoot concurrency and performance bugs...