JSR 305 and Design by Contract
Static Analysis tools have become common place among many Java projects. Their practicality is unquestionable, and if you aren’t currently making use of them within your projects you definitely should take a look at doing so.
The purpose of this post however is not to dictate to my readers how they should be running their software projects, it is more intended to give a brief overview of a newly proposed Java Specification Request. JSR 305’s purpose is to offer a set of annotations to the Java Developer in order to make Static Analysis tools smarter.
As an aside, if you haven’t heard of Static Analysis tools like FindBugs, or even JSR305 and are interested in learning more about it before reading this post I suggest you checkout William Pugh’s (Creator of FindBugs) Google Tech Talk on JSR 305. This presentation will help clarify what this JSR is trying to accomplish and may familiarize you more with what Static Analysis tools are trying to accomplish.
Continuing, why do Static Analysis tools need help in anyway? Aren’t these tools supposed to be sophisticated enough to determine what is and isn’t a bug? Although these tools are quite powerful they are far from perfect and commonly call question to code that is perfectly correct. This JSR is an attempt to help solve those issues and it is my feeling that this JSR will also help introduce the ability for developers to define contracts for their implementers to follow.
Many languages, like Eiffel, are built on top of the theory of Design by Contract. Design by Contract is an approach to designing software that allows developers to strictly define a set of invariants and specifications for classes and objects. If you interested in this topic I will refer you to Bertrand Meyer’s (the man behind Design by Contract and the Eiffel Programming Language) amazing book Object-Oriented Software Construction.
It is my opinion that any project can benefit from adapting some or all the ideas from Design by Contract and it does not necessarily require a lot of work to do.
Now that I have digressed from my original point, let’s take a brief look at what JSR305 brings in terms of annotations. I’m assuming that you are already familiar with JSR305 and annotations in Java.
The current proposal, remember this is still a work in progress, includes annotations for Nullness, Sign, Return type & Purity, Taint, Language, and Threading. Each of these annotations should help the developer clearly define valid states for methods and variables in their classes so that tools like FindBugs can spot violations with certainty.
These annotations also work to bring automatic software documentation directly from Design by Contract, as they help the developer clearly define a set of valid states that implementers can refer to when using their code. In theory these annotations could also be used by IDE’s in order to warn developer and implementers if they are violating any of these states.
Let’s look at a piece of Eiffel code as an example of how Design by Contract actually works, and how JSR305 will help achieve something similar in Java. The following example is a simple counter class that allows you to increment, decrement, and reset a counter. The counter class however has a set of valid states for which it must maintain.
First the counter must never be less then zero. This is important as an implementer could create an instance of this class and call the decrement function leading to odd behaviours. Second the increment and decrement must change the state of the counter in some way either by decrementing it or incrementing it. The decrement function also has a third requirement that the counter not be zero before calling this function.
indexing description: "Counter increment by one, decrement, and reset" class interface COUNTER feature -- Access item: INTEGER --Counter's value feature -- Element change increment is -- Increase counter by one ensure count_increased: item = old item + 1 decrement is -- Decrease counter by one. require count_not_zero: item > 0 ensure count_decreased: item = old item - 1 reset is -- Reset counter to zero. ensure counter_is_zero: item = 0 invariant positive_count: item >= 0 end
Even if you are not familiar with Eiffel programming, just by looking at the invariants you could easily tell what this class does, and exactly how to use it, a beautiful property of Automatic Software Documentation.
Now compare this to a similar Java class, which offers the same functionality but minus the contract between the developer and implementer.
/**
* A Simple Example of a counter.
*
* @author Chris Boersma
*/
public class Counter {
private Integer item;
public void Counter() {}
public void create() {
item = new Integer(0);
}
public void increment() {
item++;
}
public void decrement() {
item--;
}
public void reset() {
item= new Integer(0);
}
}
You can quickly see that you loose sense of what is a valid state for this class, and exactly you are guaranteed of in terms of functionality.
Now put yourself in the seat of a static analysis tool. What is a bug here? Are there any bugs? In reality there probably aren’t. It is quite possible that this class functions exactly as it was intended, perhaps the developer wanted a Counter that could be decremented to a value less then zero. It is extremely difficult to determine without a doubt that this code is “bug” free, and this is just a simple counter, imagine having to determine whether a complex class of over 500 lines of code is indeed “bug” free.
Now take that 500 lined class, and integrate it with other components of a system and you quickly realize how difficult a task static analysis tools actually have, and why JSR305 is really quite important.
I could now quickly delve into an example illustrating how JSR305 will make this code 100% fools proof, but I don’t really see a point in doing so. Right now the JSR hasn’t even clearly defined what set of annotations it will support so anything I type here could quickly become obsolete and irrelevant.
My purpose here is merely to exercise your mind, and show you that Static Analysis tools like FindBugs combined with JSR305 could bring a whole new world of programming to the Java community. I mean how many of us would argue that writing cleaner, safer code is really all that bad?
At this point I could delve into the different views regarding these new annotations, but I’ll save that for another discussion as this JSR develops into something more concrete. I personally think it’s far to early to pick sides, and I personally feel that there valid points being made as to whether this JSR makes sense or whether it is completely useless to the Java community.
I suggest everyone keep their eye on this JSR and if you feel strongly about something open up a discussion over at the JSR’s google groups page http://groups.google.com/group/jsr-305/ It’s better to have your voice heard and introduce some meaningful discussion with the JSR creators then to sit on the fence (or your blog) and call foul.
Finally a new PC!
Well I finally broke down and decided it was time to upgrade my old PC. To be honest I couldn’t of asked for a better computer, I’ve had it since 2004 and it’s cost me nothing but a new hard drive once. To me this is prove of why I always tell people the best computer money can buy is the one you build yourself.
Sure you can walk into your local Futureshop or PC Shop and get a pre manufactured/assembled PC, but you have zero guarantee as to what is inside of that PC. In most cases large manufacturers use subpar components in order to keep prices competitive. Small PC Shops are notorious for advertising certain components but giving you low cost alternatives in the final product, not to mention that their computers are usually more expensive then just buying the parts yourself.
As I’ve always built my own computer, this time was really no different. I had been toying around with the idea of buying a Mac Pro, but I just couldn’t justify the cost, not to mention that I’ve been successfully running OSx86 for months on my PC. In order to continue doing so I had a couple of requirements for my new PC which seems to be a theme of mine as of late.
First my new PC had to be power efficient. I find myself becoming a lot more eco-friendly, and I feel that having a PC that is efficient can really help to reduce everyones carbon foot print. This leads to my second requirement, which was my number one requirement in my previous PC, the computer had to be QUIET.
If there’s one thing I hate more then anything else, it’s having to listen to the hum of 5 fans churning away inside someone’s PC. I don’t understand how people live with this. I spend a good amount of time next to my computer, and the last thing I want is to have to listen to fans.
My previous PC utilized a lot of power efficient components that I could passively cool, in order to minimize noise. I also made use of one of my favourite cases the Antec Sonata II this quiet case included a lot of ducting to help air flow and minimize the number of fans needed to cool your components. It also came with hard drive shock absorber mounts, and a heavy aluminium construction to eliminiate vibration noise. Unfortunately the interior ducting was also the most annoying part of this case, adding new components was a pain, and tucking wires became an issue as the ducting really got in the way.
My third requirement was that my new PC be built with components that I knew would last for at least the next couple of years. I’m a firm believer of paying money up front on solid top of the line components that I know will not be obsolete in a few months, and that are well manufactured.
With these three requirements in place I spent some time wrestling with what components I wanted to include, and where I was going to get the best price for these components.
One of my first problems was whether or not to splurge on a quad core cpu. I had really considered the Intel Quad Core Q6600, but after doing some reading (Especially this article over at Coding Horror) I really came to the conclusion that the Quard Cores would probably go to waste on me. Not to mention that the Quad Core uses a 65nm process, and definitely violates my power efficiency requirement.
After eliminating the quad core from my list of potential CPUs I had to decide what dual core processor I wanted and to be honest this wasn’t a difficult pick for me. The new Wolfdale based Core2Duo processors from Intel are power efficient and use a 45nm design which allows them to be overclocked to insane levels. These CPUs are only marginally more expensive then their 65nm counterparts, and since my local PC shop was having a sale on the Core 2 Duo 8400 I decided to pick it up for $190.
My next problem was choosing a suitable video card. I knew first that I had to choose something from Nvidia, as driver support for ATI based cards is abysmal in Linux, and OSx86. This somewhat limited my choices, and with so many high end gamer cards made me wonder if I’d ever find something to suit my needs. I really wanted something that was passively cooled, since in most PCs the bulk of noise originates from the Video Cards cheap fan. After doing some thinking and reading I decided that the highend 8800 series was out as it was difficult to passively cool and definitely violated my power efficiency requirement.
My next card was the 9600GT which I really did consider thoroughly. Gigabyte has a new passively cooled version of the 9600GT that I thought would make a perfect fit, however I couldn’t justify the $200 it was going to cost me. I am not an avid gamer, in fact that last game I ever played on my PC was Command and Conquer Tiberium Wars, not the most graphic intensive video game.
With these cards eliminated I stumbled across an ASUSTek Silent 8600GT, which seemed to fit my needs quite well. This card is passively cooled and does not require and extra power connection. This card was also on sale at my local PC shop, so I managed to score it for $75.
I now had to find a suitable motherboard to connect all this stuff too. I’ve always been fond of Asus motherboards and have been using them exclusively in my personal computers since 1998. I’ve yet to really ever have an issue with them, so it was obvious to me that I would be putting one in my new computer as well. My first job though was to verify what Asus boards worked best with OSx86. After doing some hunting around the web I found that the P5k-E series of motherboards had good compatability with OSx86, and were solid performing motherboards based on Intel’s P35 architecture. I found a P5K-E on sale at my local pc shop for $160.00.
Having my main components in hand, I decided that I’d have to get a new case to house my new components. I decided on the Antec P182. This case was designed with the help of SilentPCReview there are tons of reviews of this case on the net, so I won’t go into full details as to why I chose the case, but it definitely fit my needs, and fit my budget as well. I fitted the P182 with an Enermax Modu+82 525w powersupply. Unforutnately I couldn’t find a lower wattage powersupply. I would of prefered the 425W Modu+ 82, but things don’t always work out for the best. I’ll be keeping my eye out for something a little lighter in the future and will probably make the switch. I do have to say though, this power supply is incredibly quiet. Usually power supplies are always humming along, but with this one I barely even notice when the fan kicks in.
I filled the rest of my PC build up with the usual components, 4gb of ram (A definite requirement for anyone who does any type of software development), a DVDRW drive, and a Seagate NCQ 500GB hard drive.
Overall the new PC is everything I dreamed it to be. Finally my PC isn’t struggling to run Tomcat, MySQL, and Eclipse at the same time, finally Itunes loads in a reasonable amount of time, and most of all finally it doesn’t take me hours on end to Rip a DVD.
Here’s a final spec list for the build including prices for all the items
- Intel Core 2 Duo 8400 $198.00
- Asus P5K-E $145.00
- G-Skill 4GB DDR2 PC8000 $94.99
- Asus 8600gt Silent 512mb $105.00
- Seagate 500gb 32mb cache HDD $78.99
- Samsung SH-S203B DVDRW Drive $26.99
- Antec P182 $109.00
- Enermax Modu+82 525w $115.00
Bringing the grand total to $873.96!!!!
When you consider that the same PC from Dell or HP would cost you a lot more then that, it’s pretty satisfying to know that not only did you save yourself money, but you enjoyed yourself while doing it.
Back from New York
Well this post is probably a little late, but I recently took a trip with some friends to New York City. This was my first time in the big apple and I have to say that I fell in love with the place, like so many do. This city has everything anyone could ever want and then some. I was only there for a short period, but in that short period I got the feeling like New York was the type of place I could see myself living, if only for a little bit.
My favourite part of the trip had to be the when my friends and I ventured down to Canal St, and China town. I have never experienced anything like that in my life, the amount of people and little shops on the street selling knock off everything was unbelievable. Looking for a follex? perhaps some Gucci sunglasses, they have it all and are willing to negotiate on price
We happened to be in New York around the same time as Chinese New Year so the streets of China town were packed full of people celebrating and dancing around. At every corner there were people dressed as ritualistic dragons dancing and store owners presenting offerings for prosperous New Years.
Our best meal of the trip in my opinion was at this little Vegetarian Dim Sum place, where I got to sample of Simulated Shark Fin, Spinach, and Simulated Shrimp Dumplings which were all very yummy. We got the “real” tour of China Town thanks to my friends friend who actually lives in New York and worked for a long time as a designer in the China Town area. It was great because he knew all the cool places to checkout, and even took us on a little tour of Little Italy. Little Italy was great as well, but by that point the sun had set and I was beginning to freeze, so I have to say I probably didn’t enjoy it as much as I normally would.
My friends and I decided to get a NY Pass which got us into so many places for free and with line bypass. Our first use of this pass was a trip up the Empire State Building. It’s funny for such an amazing building the inside has to be thoroughly disappointing. Except for the main lobby where the architecture is breathetaking, the rest of the building is either falling apart or currently under “remodeling”. Once you get up to the top though, everything about the interior is forgotten. You are presented with a birds eye view of the entire city, and you really get a feeling for how big New York City is, and at the same time how small it is considering how many people actually live there. We were lucky since the day we went was a clear sunny day that allowed us to see the entire city.
Our pass also got us into all of the amazing museums in New York. We took our second day to tour around MOMA, Natural Science Museum, and the Guggenheim. I’m usually not a huge fan of Museums but I really did enjoy all of these, except for the Natural Science museum which I found somewhat boring. My favourite of the bunch was definitely the Guggenheim, it is a beautifully designed building and the exhibits were different from anything I had seen before. On display was a famous Chinese artist you used gun powder to create absolutely amazing “paintings”.
We also spent a lot of time walking around Central park as we made our way to the different museums, and I have to say I’ve never experienced something of that scale before in such a densely populated area. It was amazing to see all the beautiful apartments looking into Central Park, but even more amazing was that I felt like I wasn’t in the city anymore. It was like being transported into a beautiful natural wooded area, while still being steps away from the core of New York City. It really was quite an amazing feeling and I could definitely see why many New Yorkers spend so much time walking around Central Park.
I could go on and on about all the things we did and saw but I’d quickly fill up my entire blog, not to mention I just don’t have the patience to retell the entire story here.
I have to say that New York gave me this feeling that no other city has managed to give me. I felt like the city was alive, it gave me this energy that no other city has ever given me. There’s always something going on in New York, always places to go and see. Not to mention the history that engulfs the entire city. I loved the fact that you could just walk anywhere you wanted to go, there was no need to worry about getting into your car, just throw your favourite pair of shoes on and walk. Nothing was really that far and if it was it was only a short subway ride to get you there.
I really have to thank my friends for planning such an amazing trip, I really did enjoy myself and I can’t wait to go back. Hopefully next time I go it will be the summer time so I can enjoy more of the city without having to worry about freezing my butt off. I have also included the pictures from our trip.
I don’t know how many of you guys are following the primaries going on in the US but I have been watching it closely over the past couple of weeks, and came across some interesting information regarding the New Hampshire primaries that were held last night.
It seems as though Diebold, you know the guys who make the electronic voting machines that even a child could hack, has been tinkering with some of the polling stations throughout New Hampshire in order to give Hilary Clinton a little boost in the polls. Although the data is not 100% conclusive it does offer some interesting looks into what may be happening in the upcoming election.
Anyway this is a good read for anyone following what’s going on in Politics in the states or who generally follows Politics.
Also I found these rather humorous images about Diebold.
This is definitely my favourite

Arch Robison (of Intel) writes an interesting piece on his blog about why Linked Lists are incompatible with Parallel Programming, and even gives some reasons as to why to avoid them in your code.
Quite an interesting article, I’ll definitely be looking at some of my code that includes linked lists and looking for some types of alternatives.
