Home of The JavaSpecialists' Newsletter

050Commenting out your code?

Posted: 2002-06-10Category: LanguageJava Version: Dr. Heinz M. Kabutz
 

Abstract: 

 

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.

Dr Heinz's Extreme Java Courses: We offer our courses as In-house and Self-Study options. In-house is best for large teams of Java experts. Self-study is suitable for individual programmers w ho want to advance their careers. If you have any questions, please simply send me an email or fill in our enquiry form :-)

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 http://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

 

Related Articles

Browse the Newsletter Archive

About the Author

demo

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

Java Training

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

Java Consulting

Nobody ever wants to call a Java performance consultant, but with first-hand experience repairing and improving commercial Java applications - JavaSpecialists are a good place to start...

Threading Emergency?

If your system is down, we will review it for 15 minutes and give you our findings for just 1 € without any obligation.