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.

 

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


ImageNet Training Record - 24 Minutes
21/09/2017

One of the problems with neural networks is how long it takes to train them. Researchers have just smashed the training barrier by reducing the time to train ResNet from 14 days to one hour and have c [ ... ]



The Discovery Of The Twitter Bursty Botnet
22/09/2017

There is no doubt that bots are a growing problem, but perhaps it is a bigger problem than any of us imagine. Researchers have discovered a Twitter botnet with over 500,000 bots, and it isn't a simple [ ... ]


More News

Last Updated ( Monday, 07 February 2011 )
 
 

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