This article represents a perspective on why one should consider using Pre/Post condition checks libraries rather than writing it down time and again in their code.
While doing code reviews, many a times, I have found people using preconditions checks such as “Not Null” in a casual manner rather than based on API contracts requiring pre and post conditions checks. Such checks, at times, do make code difficult to read, learn or understand. One may want to rather use home-grown or external (proven ones) libraries for doing such checks. Not only does it enhance code readability and learn-ability but also, it does make you aware of contracts you put in the code thereby including only necessary checks and, hence, cleaner code.
Sample Code representing usage of Libraries vis-a-vis Traditional Code Checks
Consider code snippets such as following:
//
// In traditional manner
//
// In traditional manner
//
if (id == null) {
throw new NullPointerException(“Id can’t be null”);
}
//
// Using Preconditions API;
//
Preconditions.checkNotNull(id, “Id can’t be null”); // Google Guava Preconditions API
Assert.notNull(id, “Id can’t be null”); // Spring framework Assert API
Validate.notNull(id, “Id can’t be null”); // Apache Commons Validate API
Contract.Requires<NullReferenceException>(id != null, “Id can’t be null”); // Code Contracts .NET framework
Assert.notNull(id, “Id can’t be null”); // Spring framework Assert API
Validate.notNull(id, “Id can’t be null”); // Apache Commons Validate API
Contract.Requires<NullReferenceException>(id != null, “Id can’t be null”); // Code Contracts .NET framework
Java/.NET Libraries for Pre/Post conditions Checks
Above considers some of the popular libraries which are mentioned below:
- Java Libraries vis-a-vis API
- Google Guava – Preconditions
- Spring framework – Assert
- Apache Commons – Validate
- .NET framework vis-a-vis API
- Code Contracts – Contract
Top 4 Code Checks for which Pre/Post Conditions Libraries could be used
Following are top 4 (most common ones) traditional checks found across source code. The code usability gets a booster if one makes use of pre/post conditions API:
- Check for validation of methods’ arguments. Throws exception such as IllegalArgumantException or the equivalent upon failure.
- Check whether a specific value is not null. Throws exception such as NullPointerException or the equivalent upon failure.
- Check state of the object. Throws exception such as IllegalStateException or the equivalent upon failure
- Check whether the index is a valid element or position index into a list, string, or array with the specified size. An element index may range from 0 inclusive to size exclusive. Throws exception such as IndexOutOfBoundsException or the equivalent upon failure
Latest posts by Ajitesh Kumar (see all)
- Agentic Reasoning Design Patterns in AI: Examples - October 18, 2024
- LLMs for Adaptive Learning & Personalized Education - October 8, 2024
- Sparse Mixture of Experts (MoE) Models: Examples - October 6, 2024
I found it very helpful. However the differences are not too understandable for me