Design By Contract and Annotations

Leave a comment

July 7, 2012 by huionn

From my experience, Design By Contract is one of the key for maintainable codes. JavaDoc is useful for description, but too verbose in practice. Although Design By Contract is not natively supported by Java, its intention can be achieved through Java Annotation.

There is JSR 305: Annotations for Software Defect Detection (, unfortunately it is not getting much traction and is in dormant status now.

For simplicity, so I come out myself a few of annotations that are useful for self-documented codes:

  1. Immutable
  2. MayBeModified: the provided parameter may be modified in the method
  3. NotNull
  4. Nullable
  5. ThreadSafe
  6. ThreadUnsafe
  7. Asynchronous: a method (for a process requiring long time to complete) which return immediately in non-blocking manner
  8. Blocking: a synchronous method which blocks until the processing complete

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: