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.

 

Banner

 

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.)

 

contract

 

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 {
...
@Ensures({
"result >= 0",
"result <= 23"
})
int getHour();

@Requires({
"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)

Eiffel

JSR 305

 

Banner


GR00T Could Be The Robot You Have Always Wanted
27/03/2024

We may not have flying cars, but we could well soon have robots that match up to predictions for the 21st century. Nvidia has announced GR00T, a cleverly named project to build robots using foundation [ ... ]



Google Introduces JPEG Coding Library
15/04/2024

Google has introduced Jpegli, an advanced JPEG coding library that maintains high backward compatibility while offering enhanced capabilities and a 35% compression ratio improvement at high quality co [ ... ]


More News

Last Updated ( Monday, 07 February 2011 )