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


Amazon Mobile Analytics
14/07/2014

AWS customers can now use Amazon Mobile Analytics to monitor app usage and revenue from apps within the time frame of around an hour to get started. With its pay-as-you-go model and free startup tier  [ ... ]



Oracle Drops Java Support For XP
08/07/2014

The next versions of the Jave runtime which will ship in the middle of July will not run on Windows XP. How will this impact developers?


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.