Overview

Contracts for Java (C4J) is a Contracts (from Design by Contract, see Wikipedia DbC definition) framework for Java 1.6 and later. The primary goal for C4J is ease of use. Contracts are about design and quality, aspects of programming that a lot programmers don't spend enough time and energy on.
Therefore a Contracts framework must be simple and painless to use. At the same time the framework must be powerful.
C4J is simple and powerful.

We are not going to try to convince any readers that Contracts are indeed a very powerful design and quality assurance technique, so if you are not already convinced of that, please follow the links above and you may be convinced to try this tool out! These are our favorites though:

  • Contracts consist of preconditions and postconditions, which are systematically defined method by method.
  • The class invariant ensures the DRY principle (Don't Repeat Yourself) for contracts by defining all those assertions, which must be fulfilled at any visible state of an object, only once.
  • Contracts can be linked to classes and interfaces.
  • Contracts are inherited by extending a class or by implementing an interface which is guarded by a contract.
  • To be able to define meaningful contracts you are forced to split lengthy methods into small, well defined, methods with a single responsibility.
  • As your contracts are checked at runtime, your classes are validated against the real usage of your application, not against some test cases that may not even be real use cases.
  • If you are dealing with legacy code that you are afraid of refactoring, external contracts are perfect to add to existing code with no risk involved.