I like Lombok
It’s been a really long time since I’ve used the Java programming language. I’ve really only used it in an educational context: my high school AP Computer Science class and some college courses. Java was also the language that I first learned how to program in before high school. I remember getting the JDK set up and typing in all the code samples from a textbook by hand and squinting hard at what seemed like inscrutable messages whenever I encountered compiler errors. All of this was great for learning computer science concepts, but the basics in the classroom never really exposes you to a language’s ecosystem when working on much larger projects. A language’s frameworks, libraries, build systems and runtimes are all important components that make a programming language feel really nice to use, and as I’ve rediscovered Java while working on Atium, I’ve been pleasantly surprised with the Java ecosystem.
One of the libraries that I’ve really enjoyed lately is Lombok. It is a code generator that writes a lot of the boilerplate for classes like getters, setters, toString, and builder methods and more. Here’s a full list of features that it supports. I certainly don’t use all the things that are supported, but it’s made coding in Java a lot more streamlined than I originally thought it would be when I first picked up the language again. This trimmed down verbosity puts Java closer to C# which has things like property accessors built into the language, and even exceeds C# in some of the things that can be generated. Below is a simple example class that uses some of the Lombok annotations. I think that the annotations make the class a lot more clear than having to write the boilerplate by hand.
Lombok example
@AllArgsConstructor
@EqualsAndHashCode
@ToString
public class Example {
@Getter
private int m_count;
@Getter
private String m_name;
}
DeLombok example
public class Example {
private int m_count;
private String m_name;
public Example(int count, String name) {
this.m_count = count;
this.m_name = name;
}
public boolean equals(final Object o) {
if (o == this) return true;
if (!(o instanceof Example)) return false;
final Example other = (Example) o;
if (!other.canEqual((Object) this)) return false;
if (this.m_count != other.m_count) return false;
final Object this$m_name = this.m_name;
final Object other$m_name = other.m_name;
if (this$m_name == null ? other$m_name != null : !this$m_name.equals(other$m_name)) return false;
return true;
}
protected boolean canEqual(final Object other) {
return other instanceof Example;
}
public int hashCode() {
final int PRIME = 59;
int result = 1;
result = result * PRIME + this.m_count;
final Object $m_name = this.m_name;
result = result * PRIME + ($m_name == null ? 43 : $m_name.hashCode());
return result;
}
public String toString() {
return "Example(m_count=" + this.m_count + ", m_name=" + this.m_name + ")";
}
public int getCount() {
return this.m_count;
}
public String getName() {
return this.m_name;
}
}