• Home
  • Testimonials
  • Blog
  • Contact Us

At a Glance of a Key

Crafting Dreams into Ventures, Code into Excellence: Your Journey to Success

  • Home
  • Testimonials
  • Blog
  • Contact Us

Marking field as required when using Lombok Builder

2018-03-28 Development Tips & Tricks 4 Comments 2 minute read

The builder pattern is nice and useful, and using Lombok @Builder annotation instead of implementing it each time can save a lot of time.
However, there is one problem with this. You can’t really define a required field.

For example, Let’s assume we have this class:

1
2
3
4
5
6
@Value
@Builder
public class MyClass {
    String name;
    String email;
}

The usage will be like this:

1
2
MyClass obj1 = MyClass.builder().name("Name").email("Email").build();
MyClass obj2 = MyClass.builder().build();

In this example, I have built 2 objects: obj1 and obj2.
obj1 was provided with all the arguments but obj2 was built without providing any argument, means both name and email will remain null.

One solution is to add a unit test for the places that are using this class and make sure we got an error in case something is not provided, but this solution is not scalable. Maybe we are covering all the cases today, but another field may be added tomorrow and we can easily forget to update it somewhere.

What I found to be really useful is the @NonNull annotation (which is part of Lombok as well).
By adding the @NonNull annotation to the required fields, in case a field is not supplied, a NullPointerException will be thrown.

So, if we’ll change our class to this:

1
2
3
4
5
6
@Value
@Builder
public class MyClass {
    @NonNull String name;
    @NonNull String email;
}

And take a look at our example, obj1 will be built as expected, but the builder of obj2 (which try to call the private constructor of MyClass with a null value) will throw a NullPointerException.
Using this technique, we can catch missing stuff early during the run of our unit tests instead of in some specific cases after deployment and it’s good for documentation as well ๐Ÿ™‚

More information about the @NonNull annotation can be found on Lombok website.

– Alexander

Oh hi there ๐Ÿ‘‹
Itโ€™s nice to meet you.

Sign up to receive a notification when new posts are published!

We donโ€™t spam!

Check your inbox or spam folder to confirm your subscription.

JavaTesting

Simple item versioning with DynamoDB

The Mossad Challenge 2018 - Entry Riddle

4 thoughts on “Marking field as required when using Lombok Builder”
  1. jachu
    2018-12-06 at 2:05 PM

    But we still need to remember to have relevant unit test.
    It would be better to not allow compile a class.

    Reply
    • Alexander Sirotin
      2018-12-10 at 7:48 PM

      You are right, you still need to have unit tests to cover it.
      If you can share a better way that will identify this during compilation time,
      I am willing to learn ๐Ÿ™‚

      Reply
  2. Andrey Murgin
    2019-07-23 at 8:39 AM

    IMHO, bad approach.
    Your client don’t know the required fields until exact developer takes a look to the builder code.

    Reply
    • Tom
      2019-10-15 at 12:28 PM

      Yes, client should not able to build my class object until they provide all required field.
      As I am owner of this class I know better how to create this class in terms of all required filed.

      Reply
Leave a Reply Cancel reply

About Me

Principal Software Engineer and an industry leader with startup and FAANG experience. I specialize in distributed systems, storage, data protection services and payment processors.

Beyond technical expertise, I am passionate about supporting fellow engineers in their careers. Through approachable blogs and hands-on guidance, I help navigate the ever-evolving landscape of technology, empowering individuals to thrive in their professional journeys.

Open LinkedIn

Recent Posts

  • Building a Delayed Message System with Redis and FastAPI
  • Go Concurrency, Practical Example
  • Using GORM – Part 3: Models and Idempotency
  • Using GORM – Part 2: Transactions and Save Points
  • Using GORM – Part 1: Introduction

Archives

  • January 2025
  • December 2024
  • March 2023
  • February 2023
  • September 2022
  • July 2022
  • July 2021
  • June 2021
  • February 2021
  • April 2018
  • March 2018
  • January 2018
  • July 2017
  • June 2017
  • May 2017

Categories

  • AWS
  • Career Growth
  • Cyber Security
  • Debugging
  • Development
  • Storage
  • Tips & Tricks

Tags

API AWS Azure Bash Brainfuck C++ Challenge Cloud Cloud Bursting Concurrency Database DevOps Disassembly DLL Documentation DynamoDB Go Golang Guice Java Jenkins Mossad NoSQL OOP Performance Programming Python Redis Security Serverless Singleton Streams Testing Unit Tests WebService

All Rights Reserved 2025 ยฉ Sirotin Enterprises Inc.
Proudly powered by WordPress | Theme: Doo by ThemeVS.