Simplicity in development or syntactic sugar

Computing is a pretty big field so it is not always possible to stay on top of every little (or big) framework or enhancement that comes down the line.  I am happy to be involved in software development so I can get a quick heads up when something really cool comes down the line.

Unfortunately I found out about a new thing (for me) in the worst possible way.  I had just joined a new project and was coming up to speed on some of the technologies that was different from my last project.

  • Java (that isn’t end of life)
  • Git
  • Spring Toolset
  • Hibernate
  • New data model
  • Different business area

In short there were no limit to things that I needed to spend my time coming up to speed on when I did the fateful command.

git pull

Yup, all I did was to refresh my workspace with the most recent code from our server.

Oh, sure, the code compiled once I installed the tool Lombok into my IDE and added it to my classpath.  The surprise was that the code that compiled no longer ran.

The culprit

What does Lombok do?  It is actually a really interesting use of the annotations that were added all those years ago in java 1.5. The purpose of Lombok it to reduce the amount of boilerplate code that needs to be manually entered that is added to all (well a lot) of projects by virtually everyone.  Not only that, it is possible to use the methods that Lombok creates even though they are not physically stored in the source code file.

Basically, this allows you to see and focus on the really interesting parts of your code that add value and not be distracted by some of the less interesting boilerplate code that you also added.

I am not going to cover everything that Lombok supports as that is all nicely documented.  I will just focus on a few of the annotations.

  • @Getter
  • @Setter
  • @Data

The first two actually do pretty much what they sound like.  If you add this annotation before your variable then Lombok will automatically generate the getter and setter for you.  Not only that but it will add these methods to the outline tab in your editor.

On the left is my own code and on the right is the outline showing the methods and variables for that class.  If you look closely enough you can see that no “get” method has been created for the iq or age variables and no “get” or “set” have been created for the internalvalue variable.

This code is obviously much smaller and would allow the programmer to be more focused on the real logic.   So this new project actually does add value.

If the Java object is really just an abstract object then there is an easier way than adding all of these “@Setter” and “@Getter” annotations it is possible to add a single @Data annotation.

@NoArgsConstructor
public <strong>@Data</strong> class ComplicatedObject 
{
	private String First;
	private String Last;
	private int age;
	private String Address;
	private String City;
	private String State;
	private int zipcode;
	private int iq;
	private int internalvalue;

	public static void main(String[] args) 
	{
		ComplicatedObject person = new ComplicatedObject();
		
		person.setFirst("Max");
		person.setLast("Musterman");
		person.setAddress("123 Main street");
		person.setCity("Smalltown");
		person.setState("IA");
		person.setZipcode(12345);
		person.setIq(125);
		
		System.out.println(person.toString());
	}
}

Installation

The installation process is actually super simple.  Simply run the lombok.jar file and it will open up a small installer.  First it searches for all installed IDE’s and presents them as a list of locations to install lombok.

Once the IDE has been selected it takes hardly a second to finish the setup of lombok.  In order to use it you simply need to restart the IDE and add the lombok.jar file to any projects that you wish to use this new functionality.

 

The good

The software both installs very simply and is limited to a single jar file. This makes it really easy to add to a project and to later remove if necessary.

 

The bad

I don’t really have any “hard proof” that Lombok itself was problematic to my work project.  I downloaded lombok and did a small test at home.  I didn’t have any difficulties and the functionality is pretty neat.

Thus there is nothing bad to report but this functionality is mainly to simplify the definitions of getters and setters for some plain old java class that is just an abstract data type.

If this is the extent of what is necessary you could go back to the olden days and just have an object and address each field directly.

package com.company.de;

public class ComplicatedObject2 
{
	String First;
	String Last;
	int age;
	String Address;
	String City;
	String State;
	int zipcode;
	int iq;

	public String toString()
	{
		return First + " " + Last + " " + Address + " " + City + " " + State + " " + zipcode + " " + iq;
	}
	public static void main(String[] args) 
	{
		ComplicatedObject2 person = new ComplicatedObject2();

		person.First = "Max";
		person.Last = "Musterman";
		person.Address = "123 Main street";
		person.City = "Smalltown";
		person.State = "IA";
		person.zipcode = 12345;
		person.iq = 125;

		System.out.println(person.toString());
	}
}

I am not saying you should do this but this is essentially what adding all getters and setters to a simple java class does.

The ugly

We did have a few problems with Lombok in our project but I think that this might have been due to a misconfiguration.  We are still looking into that.

This entry was posted in programming and tagged . Bookmark the permalink.