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


China Plans To Oust Windows
01/09/2014

China is making progress with its own operating system designed to replace Windows, Android and iOS. Should we be worried? 



Microsoft Research Shows How To Turn Any Camera Into A Depth Camera
13/08/2014

It's SIGGRAPH so you expect a lot of amazing graphics, but Microsoft seems to cornering the market in wow. In this case, take any ordinary video camera and, with a small change, turn it into a really  [ ... ]


More News

Last Updated ( Monday, 07 February 2011 )
 
 

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