|
The Java Specialists' Newsletter
Issue 050 2002-06-10
Category:
Language
Java version: Commenting out your code?by Dr. Heinz M. Kabutz
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.
Would you like to really understand Java concurrency? Join us for an
in-depth study of how threading works in Java. During the course,
you will learn how to write correct and fast multi-threaded Java code.
Please
click here if you would like to learn more. 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
Language Articles
Related Java Course
Discuss at The Java Specialist Club
|