Apr 11 2006

Class HighLite File

Many people might be familiar with the File class. You need a File object for many of Java’s IO operations. In this Class High Lite article I cover some of the File’s least known methods and fields.

Please note that in all cases be sure to read the Java documentation, as it is the best available source of information regarding the Java API. You can also always take a pick into the source code provided with the Sun JDK.

I often see “\\” in strings to separate directory and a file names. It is strongly suggested to not use operating dependent strings to separate directories and files. The backward character is a Windows specific way to separate a directory hierarchy, while Linux and Apple use a different character. To avoid using hard coded strings to concatenate a file path use the File’s separator field. The value of separator contains the correct file separator character for your operating system.

The listRoots method is a convenient way to to access all the available drives available in your system. This method might be helpful to enhance the default JFileChooser and have a drives drop down list. This method returns an array of File objects contain the root letter drives, your file system roots.

Some people procrastinate, but does their code procrastinate also? The deleteOnExit method waits until the JVM terminates to delete a file. Be sure to read the JavaDoc. According to the JavaDoc 1.4.1, “Deletion will be attempted only for normal termination of the virtual machine.” When your program exits, the file marked for deletion is removed, even if the program exits with an exception. As the JavaDoc states, the file will be deleted when the JVM exits normally, and an exception is normal termination of the JVM although not for your program.

Technorati Tags: , ,


Apr 10 2006

Sybase Transaction Log

One of the database vendors that my company supports is Sybase. We have a Sybase database which we hit from JUnit tests but every once in a while a Sybase specific JUnit test will just freeze. I found out that the JUnit test would freeze because the Sybase transaction log would fill up and need to be cleared.

To clear the transaction log open up Sybase SQL Advantage and log in. Once log in execute the following statement:

dump transaction <DATABASE NAME> with no_log

Once I run this command my JUnit tests continue without a hitch.

Technorati Tags: , ,


Apr 10 2006

Java Five-Oh #5: StringBuilder

If you are using Java 5 you should immediately replace StringBuffer with StringBuilder. By now everyone knows that you should never use the plus sign (+) to concatenate strings. As a rule of green thumb, if I concatenate more than five strings I do so using an instance of StringBuffer. When building a dynamic string StringBuffer is faster than plain old string concatenation. But with Java 5, StringBuffer got a performance boost in the the form of StringBuilder.

StringBuilder is a drop down plug and play replacement for StringBuffer except when working in an multiple treaded environment. StringBuilder is not synchronized while StringBuffer is. It takes longer to invoke a synchronized method than a non-synchronized one so I imagine that most of the StringBuilder optimization comes from the fact that it’s methods are not synchronized. This is one of the reasons why you should use an ArrayList instead of a Vector.

Just to put up some code with this post, here is how I would write a Hello World example using StringBuilder.

public String getGreetings(String name) {
   return new StringBuilder("Hello, ")
      .append(name)
      .toString();
}

Technorati Tags: , , ,


Apr 10 2006

Groovy Closures

I been writing some Groovy scripts recently. For the most part I have been writing small function that iterate over some list of maps. One great feature of Groovy is closures. Yeah, I know Ruby also has closures but for the following examples I will stick to Groovy. The following is a typical example of iterating through each element in a list using closures in Groovy:

[1, 2, 3].each { item ->
   println item;
}

In the above example the square brackets defines a literal list while the closure is composed within the curly braces. For each element in the list the code inside the closure will be executed. You could have said the same thing by using a for in loop as in the following:

for(item in [1, 2, 3]) {
   println item;
}

Think of closures akin to lambda or functor functions. Closures are useful because they are anonymous code blocks that can treated as objects. Here is how you create a closure object that prints the square of a numeric.

def prsq = { println it * it};
[1, 2, 3, 4].each prsq;

The variable prsq is an instance of Closure and you can treat it as any object and pass it to methods, such as each, that expects a closure as a parameter. Notice that the variable it is defined for you within the body of the closure. You can also invoke a closure like a method in the following fashion:

prsq(10);

Here is one last example for you to try out.

def greetings = {"Hello, ${it}"}
println greetings("World");

Technorati Tags: , , , ,


Mar 30 2006

Devgone

I wanted to continue with some of my diary entries which relate to programming. I don’t know what I was thinking when I wrote this one here dated the 8th of August of 2005:

A coworker moved on to greener code, before his last day we had a lot of knowledge transfer sessions. This guy was the process guy and a stickler for maintaining JUnit tests. During one of the knowledge transer sessions he said, “Testing saved my butt. I test often because I’m not that good of a developer.” I have to agree with his statement, testing saves your butt. Because he test so much he is a better developer. JUnit makes better developer out of us all. Do it early, do it often, like making love. If only sex had a green bar at the end to indicate your success! He also asked of all of us staying behind, “If you can’t write the unit test for the thing you are doing, how do you know what you are doing?” Again, like having sex! If you don’t have a JUnit for it, I don’t think you know what you are doing!

Technorati Tags: , , ,


Mar 19 2006

Test Plenty

The following is from an entry in my diary dated back to June of 2005:

Test yourself before you wreck your. One of the interview questions asked during my interview for my current position was, “what do you do when adding a new feature?” I went on about understanding the specification, writing a use case, writing a test case, implementing the code and writing some unit tests. That is a text book answer that you don’t necessarily learn in school. Like comments, unit testing is one of those good habits that fall by the waste side when developers feel the crunch. At my new job you won’t believe the testing involved. The testing of a feature takes me longer than the implementation of said feature. In addition to JUnit our company has a proprietary testing harness that essentially tests the complete application including system correctness by replaying user experiences. We do so much testing that I proposed we adopt the motto, Leave No Test Behind. Sometimes my comments are twice as long as code. My testing takes me triple the time of development. But you need to remember that the life time of the software is four times what is expected.