Why consider using Guava-like Libraries for API Contract Conditions Checks

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
//
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
Java/.NET Libraries for Pre/Post conditions Checks
Above considers some of the popular libraries which are mentioned below:
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:
  1. Check for validation of methods’ arguments.  Throws exception such as IllegalArgumantException or the equivalent upon failure.
  2. Check whether a specific value is not null.  Throws exception such as NullPointerException or the equivalent upon failure.
  3. Check state of the object.  Throws exception such as IllegalStateException or the equivalent upon failure
  4. 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
Ajitesh Kumar

I have been recently working in the area of Data analytics including Data Science and Machine Learning / Deep Learning. I am also passionate about different technologies including programming languages such as Java/JEE, Javascript, Python, R, Julia, etc, and technologies such as Blockchain, mobile computing, cloud-native technologies, application security, cloud computing platforms, big data, etc. I would love to connect with you on Linkedin. Check out my latest book titled as First Principles Thinking: Building winning products using first principles thinking.

Recent Posts

Large Language Models (LLMs): Four Critical Modeling Stages

Large language models (LLMs) have fundamentally transformed our digital landscape, powering everything from chatbots and…

1 day ago

Agentic Workflow Design Patterns Explained with Examples

As Large Language Models (LLMs) evolve into autonomous agents, understanding agentic workflow design patterns has…

2 days ago

What is Data Strategy?

In today's data-driven business landscape, organizations are constantly seeking ways to harness the power of…

3 days ago

Mathematics Topics for Machine Learning Beginners

In this blog, you would get to know the essential mathematical topics you need to…

1 month ago

Questions to Ask When Thinking Like a Product Leader

This blog represents a list of questions you can ask when thinking like a product…

1 month ago

Three Approaches to Creating AI Agents: Code Examples

AI agents are autonomous systems combining three core components: a reasoning engine (powered by LLM),…

1 month ago