Monday, December 1, 2003

Commenting Java code the WebSphere way


By Dan Velasco

One of the first things a developer learns about Java is how to do comments. In Java, there are three basic types of comments: single line comments, multi-line comments and Javadoc comments. Learning the basic format of commenting is simple, so simple in fact you might forget that an IDE like WebSphere Studio can make it even easier. Well, WebSphere Studio can make commenting your code ever so much easier for you, and making it a little easier can go a long way toward improving your productivity and the readability of your code.

The basic way to do a single line comment in Java is to use two forward slashes, i.e. "//". It doesn't seem too hard to type just two characters, but when you have to do it many times a day or for multiple lines it can quickly add up to a pain in the patootie. The easier way to comment code using WebSphere Studio is to first select the code you want to comment (either a single line or multiple lines) and right-click and choose Comment from the menu. If you're commenting just a single line, you can click anywhere on the line to be commented and then right-click and select Source --> Comment. You can see how this is done in Figure A. If you're a keyboard king or queen, you can also comment code by pressing CTRL + / (forward slash).


You can comment a single line. (click for larger image)

OK, so you saved a few keystrokes and kept carpal tunnel at bay for a few more minutes of your life. The really nice part of using the WebSphere Studio commenting feature is that you can also uncomment your code as easily as you commented it. Simply click on the line to be uncommented or select a block of code and right-click and select Source --> Uncomment. Or, the keyboard way is to press CTRL + &#92 (backslash). This will restore your code to the exact same state it was before you commented it. You can see this in Figure B.


Sometimes, you just need to take back your comments. (click for larger image)

The subtle but important difference between commenting code out manually as opposed to using WebSphere Studio to do it is that WebSphere Studio both puts the commenting characters ("//") at the far left of the screen where they are easier to see and it also does not insert any extra spaces in a line. As you can see in the manual comment in Figure B above, there is a space between the "//" and the comment that makes it look odd when I uncomment the line using WebSphere Studio. If I had commented it with WebSphere in the first place it would be aligned with the other code when I uncommented it.