Precision Java with Google Contracts
Precision Java with Google Contracts
Monday, 07 February 2011

Contract-oriented design has the potential to reduce programmer errors by enforcing conditions on class and interface alike. Now there's an open source way to add contracts to Java with cofoja.

Contract-oriented design has the potential to reduce programmer errors by enforcing conditions on class and interface alike. Now there is an easy, open source,  way to add contracts to Java with cofoja.

Google seems to be doing good with its 20% rule that allows employees to spend 20% of their employed time on projects of their own choice. The latest bounty to come from the freedom to have an enthusiasm for software is Contracts for Java, a potentially radical move.




You might not know about the contracts approach to programming because it's a methodology that hasn't had much publicity in recent times and it's a really only championed in practice by the Eiffel language - which while a fine language is hardly widely used and isn't even in the top fifty TIOBE language rankings. (The term "Design by Contract" is also a registered trademark of Eiffel Software.)




Contracts for Java is an open source tool that adds some  annotations that, with the help of a JVM argument, are checked at runtime to make sure that the code honours the contract. The new annotations are @Requires,  @Ensures, @ThrowEnsures and @Invariant complete with Boolean expressions that specify the condition. Contracts are inherited from interfaces and classes which makes them more persistent. 

To show you what a contract looks like the example from the Contracts for Java web page does the job well:

interface Time {
"result >= 0",
"result <= 23"
int getHour();

"h >= 0",
"h <= 23"
@Ensures("getHour() == h")
void setHour(int h);

Notice that it is fairly obvious what this all means and what the contract enforces. However, you do need to look up some of the philosophy of design by contract because this isn't the same as runtime data validation - which should be built into the code. Any difference in behavior between running the code with and without runtime contracts is in itself regarded as a bug.

There are other annotation schemes to help with similar problems in Java (JSR 305 for example) and contract implementations in other languages. The biggest problems with this Contracts for Java is its abbreviated name cofoja, which sounds like a medical complaint, and the fact that it doesn' have any IDE support built in.  How to use it with Eclipse has been described in detail on Fabian Steeg's blog (Using Contracts with Eclipse) but a simple-to-install extension would be a logical next step.

More Information

Contracts for Java

Design by contract (Wikipedia)


JSR 305



TJBot - Using Raspberry Pi With Watson

In order to get makers and tinkerers to get involved with its Watson services, IBM has come up with an open source project to create a cute cardboard or 3D printed bot, named TJ after IBM's first ever [ ... ]

A 4-Bit Adder Made Of Cardboard!

Even if you know it, the fact that computation can be achieved in an amazing range of different ways still comes as a surprise. Programmer lapinozz decided to prove the point by building a full adder  [ ... ]

More News

Last Updated ( Monday, 07 February 2011 )

RSS feed of news items only
I Programmer News
Copyright © 2016 All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.