Archive for October, 2005

Eclipse Tool Tip #3: Code Perspective

Even though I have two monitors, I need to utilize every possible dpi of IDE real estate. Eclipse has provides for different perspectives such as Debug and Java but it does not matter what perspective I am using, I am most interested in the code. To focus on the code select the source file you are interested and hit ctrl+m. Ctrl+m will maximize the currently selected tab. To switch back to your perspective hit the ctrl+m again.

Put JavaScript To Sleep

Every Java programmer will tell you that you can pause a program using the sleep static method of the Thread class.

Thread.sleep(milliseconds);

JavaScript does not have a wait or sleep method but instead provides the setTimeout and setInterval functions. Here is some JavaScript code to demonstrate the setTimeout function:

setTimeout('alert("Hello, World")', 4000);
alert('Hello, Web');

The Hello, World message will display after 4 seconds, and after the Hello, Web message. In essence, this function treats a piece of JavaScript code as a child and gives it a ‘time out.’ In contrast, the setInterval method will repeatedly execute a piece of code at a given time interval in milliseconds until you stop it. When compare side by side, I rather use the setTimeout function. Here is a recursive-like example using the setTimeout function that mimics setInterval.

var myColors = new Array(
    '#000000',
    '#FF0000',
    '#00FF00',
    '#0000FF',
    '#FFFFFF'
    );

function myFunx(index) {
    if(!index || index > myColors.length) {
        index = 0;
    }
    setTimeout('myFunx('+(index+1)+')', 2000);
    document.bgColor=myColors[index];
}

myFunx();

The example above updates the background color every other second. Don’t try this at home unless you are a trained professional or are writing trivial JavaScript examples.

Hard To Debug

Whenever I start debugging the struts based web application I am working on the server freezes. I am using JBoss/Tomcat with Struts 1.1 and Eclipse 3.1. Since I can’t fire up the debugger, mark breakpoints, and set expressions I fell back to the tried and tested ‘printf method of debugging’. I call it ‘printf’ because I learned to debug this way back in my C programming days in college. Since I couldn’t fire up the debugger and examine the strack trace I throw my own debugging exceptions and print out the last few lines method calls in the stack.

    try {
        // TODO: Remove/debug
        throw new Exception("DEBUG EXCEPTION");
    }catch (Exception ex) {
        Object[] stack = ex.getStackTrace();
        for(int i=0; i < 4; i++) {
            System.out.println(stack[i]);
        }
    }

I should probably look more deeply into why the server freezes when I start up the debugger. On the other hand, I have managed to come up with a work around to get the job done.

Yes Comments

There is more than one way to say it. A rose in any other name is still a rose, or better yet a comment in any other language is still a comment. Every C++ and Java programmer will tell you that end of line comments start with two forward slashes such as:

// This is a C/C++/Java comment.

For some reason, many script languages such as Perl, Ruby, and Jython choose the pound/bang sign for end of line comments.

# This is a script comment.

C inspired languages, such as C++ and Java have comment blocks. Comment blocks start with /* and end with */ and everything in between will be treated as comments such as:

/*
 * This is a C/C++/Java
 *    comment
 *       block
 */

Of course, Java can produce online documentation if you use the /** variation of the comment block. You can also place comments in HTML/XML documents, for example:

<!-- This is an HTML/XML comment -->

If you work with XSLT files, which are just XML files, and want to produce a comment you need to place your comments inside xsl:comment tags, such as:

<xsl:comment>
   This is not a comment.
   This will produce a HTML/XML comment.
</xsl:comment>

And of course, you can comment a JSP page using the HTML comment construct but if that comment is JSP specific and is not supposed to be sent to the client you can use the following comment construct:

<%--
   This is a JSP comment,
      won’t be sent to the client.
--%>

And like everything else that Microsoft does, Visual Basic comments are not based on any of the constructs listed here. Visual Basic end of line comments start with the single quote character. The following is a Visual Basic comment:

' This is a VB comment.

Just remember that developers can speak in code but not everyone that reads your code is a developer. So whatever you’re preferred language is please comment your code, at a minimum fill in the pre/post conditions.

Random Shell Script

Welcome to the random shell script of the week! This shell script is trivial but useful, especially if you don’t write this kind of stuff every other day. The following shell script will iterate over every file in the current directory and print the filename out.

#!/bin/sh

for file in ./*
do
   echo "$file"
done

You can modify the ./* to ./*jar to loop through all jar files.

Ruby On Rails, J2EE On Dope

I remember those ‘this is your brain on drugs’ commercials from back in the day when they showed an egg in a frying pan. Well, having worked on a J2EE project and following programming developments I feel that many developers feel like this after a big J2EE project. There is a paradigm shift in the enterprise way of thinking. There is a move to lightweight containers such as Spring and PicoContainer. With the growing popularity of Ruby on Rails, convention is being favored to XML configuration. In the Java space, the frameworks to follow are Trails, Karma MVC, and Rife.

Next Page »