Tuesday, September 23, 2008

Stack

00. Purpose

To become familiar with the tools Ant, JUnit, PMD, FindBugs, and CheckStyle and apply it towards the Stack project found on the ICS 413 Schedule page. Inside the Stack project are errors that were purposely left unfixed and our job is to use the tools to find and eliminate them.

File Download

My completed distribution zip file can be downloaded here: stack-johnczly-6.0.925

01. Expectations: Trying to Say No to Fear

In regards to the "no fear" attitude, I wasn't exactly on board from the beginning of this assignment. I was actually a bit derailed and a bit concerned working with the Ant XML files. I thought we would need to create those ourselves, which struck some fear into me. I can understand most of the lines in the XML files but if I had to write one myself right now, I have a fair amount (or a lot... depending on your definition) of doubts and fear. But once I read the assignment, a lot of that went away and I got going at a decent pace.

02. Fixing Stack using Ant, JUnit, Checkstyle, FindBugs, and PMD

Majority of the problems were quick fixes (e.g. setting final to variables, fixing curly brackets, and javadocs, etc). There were two brought up by PMD that caused me to scratch my head. The (1) empty catch block and (2) the LooseCoupling: avoid using implementation types and use interface instead.

First was the empty catch block. I must admit I would have done the same thing Ronn had done. Never having any program in production at a professional level, I would have put a System.out.println() for two reasons.
(1) For the sake of putting something inside the empty statement, making the message go away.
(2) Because there is a fail message, it would make sense to have a pass message as well. By the way, it was awkward to test FOR a failure rather to test for no errors.
So I learned something new. Instead of purposely trying to catch an exception and throwing fail cases, we can use the new annotation in JUnit 4. I won’t attempt to summarize IBM’s explanation to avoid screwing it up. So quoted directly from IBM’s page,

In JUnit 4, you can now write the code that throws the exception and use an annotation to declare that the exception is expected:

@Test(expected=ArithmeticException.class)
public void divideByZero() {
int n = 2 / 0;
}
If the exception isn't thrown (or a different exception is thrown), the test will fail.

The second problem I ran into was labeled LooseCoupling. I was initially confused by LooseCoupling. I did not understand what they meant by using the interface instead of implementation types such as ArrayList. It took me awhile. I googled different things and finally came across a post about Interface Programming. It recommended using List (interface) instead of ArrayList, making it
List<Object> = new ArrayList<Object>();
Now the example provided in PMD makes so much more sense. It's one of those times where I look back and go, "D'OH!" In my opinion, they should have put the correct example and a incorrect example rather than just putting it in comments. Maybe I am just tired. Although I fixed the problem, I am unsure on why this is a better case. Hopefully someone can answer that question for me. I am guessing it is because it's more general, and the users can interchange if needed between LinkedList and ArrayList to utilize the different characteristics.

NOTE: It's weird however after eliminating the last problem found by pmd, the echo message didn't set a value for pmd.failure.count so the message I received when running ant -f pmd.build.xml resulted in
[echo] PMD found ${pmd.failure.count} problem(s).
I assume that is the same meaning as PMD found 0 problem(s).


03. Conclusion: Veni, Vidi, Vici

I was able to complete all the tasks and fixed all the errors present in the stack project. I would not have spotted a lot of the problems without the help of the these tools. Ant and all the other tools are tremendously useful. I feel much more comfortable using the tools and learning how to interpret the messages.

At the end of this assignment, most of the fear has gone away. I guess once I got going, it just didn't seem so scary.

Reminder to self:
(1) The plugins folder that FindBugs looks for when invoked does not exist. Do I need one?

No comments: