Sunday, January 27, 2013

Java and Lombok

If you are an Indonesian, it's pretty easy to make a connection between Java and Lombok. However, this post is not about that Java and Lombok, but rather for the Java programming language and Project Lombok.

One of the drawback of Java language is its verbosity. One very easy example can be found in any typical Java POJO.

public class Order{

  private long id;
  private String name;
  private int size;

  public Order(){
  }

  public Order(long id){
    this.id = id;
  }

  public long getId(){
    return id;
  }

  public void setId(long id){
    this.id = id;
  }

  public String getName(){
    return name;
  }

  public void setName(String name){
    this.name = name;
  }

  public int getSize(){
    return size();
  }

  public void setSize(int size){
    this.size = size;
  }

  @Override
  public String toString(){
    return String.format("%s %d %d", id, name, size);
  }
}


Already that much code just for a very simple structure with constructors, getters, setters, and toString method. This is where Lombok comes to save the day! Instead of writing that much code, you can have something like this:

@Data
public class Order{

  private long id;
  private String name;
  private int size;
}

...and that's it! A clean, nice code that is easy to maintain. The @Data annotation will tell Lombok to generate the constructor, getters and setters, toString method (and even equals and hashCode method!) all during compilation time! (instead of hiding the code in other file ala AspectJ). Since Lombok can be integrated with your IDE, you will not lose the nice content assist or any other feature that you currently enjoying.

There are several other features provided by Lombok (see: Lombok features), although personally for me, this one is going to be the one I use the most. I am not too scared of introducing Lombok dependency to my project since Lombok itself presented a nice way to stop using Lombok dependency with a tool called "delombok". By using delombok, all magically generated code will be written to the source code and the dependency to Lombok library will be removed.

The only drawback I can think of is whenever I rename one of the field name and the rest of code which refer to the getter/setter will also need to be updated manually (instead of having them automatically updated using a refactor tool in your IDE), but it is a very small price to pay (I guess).

17 comments:

  1. Agree. Lombok is cool. Why it is not a plugin for Eclipse???? :(

    ReplyDelete
  2. Anonymous10:45 AM

    I could not refrain from commenting. Well written!

    Here is my web-site: White Kidney Bean Diet

    ReplyDelete
  3. Anonymous11:37 PM

    What's up to every single one, it's really a
    nice for me to pay a visit this site, it contains helpful Information.


    Feel free to surf to my web site; bawash.com

    ReplyDelete
  4. Anonymous7:34 PM

    Thank you for the auspicious writeup. It actually was once a amusement account it.

    Glance complicated to more brought agreeable from you!

    However, how could we keep in touch?

    Here is my webpage :: Recycling facts

    ReplyDelete
  5. Anonymous8:16 AM

    Hey there! I just wanted to ask if you ever have
    any issues with hackers? My last blog (wordpress) was hacked and I ended up losing several weeks of hard work
    due to no back up. Do you have any methods to stop hackers?



    Feel free to visit my web page :: Permanent hair straightening

    ReplyDelete
  6. Anonymous8:01 AM

    Thank you for the auspicious writeup. It in fact was a amusement account it.
    Look advanced to far added agreeable from you! By the way, how can we
    communicate?

    Also visit my web site; Diarrhea Remedies

    ReplyDelete
  7. Thank you for sharing such kind of information. Your information is always useful for me, keep it up and you are subscribed.
    __________
    Buy Online LTO6 Tape

    ReplyDelete
  8. Anonymous11:37 AM

    That is very fascinating, You're an excessively skilled blogger. I've joined your
    feed and sit up for in quest of extra of your wonderful post.
    Additionally, I've shared your web site in my social networks

    Feel free to surf to my web page - review

    ReplyDelete
  9. Anonymous6:50 AM

    Hey! Do you use Twitter? I'd like to follow you if that would be okay. I'm undoubtedly enjoying your blog and look forward to new posts.


    Feel free to visit my website: http://greencoffeediets.org/

    ReplyDelete
  10. Anonymous4:54 PM

    Wow! This blog looks just like my old one! It's on a completely different topic but it has pretty much the same page layout and design. Outstanding choice of colors!

    Also visit my web site: Library Relocation

    ReplyDelete
  11. Anonymous12:42 AM

    Just desire to say your article is as surprising. The
    clarity in your post is simply spectacular and
    i can assume you're an expert on this subject. Well with your permission allow me to grab your feed to keep updated with forthcoming post. Thanks a million and please keep up the rewarding work.

    my web-site ... Raspberry ketone Plus Reviews

    ReplyDelete
  12. Anonymous2:46 AM

    This blog was... how do you say it? Relevant!! Finally I've found something which helped me. Thank you!

    Also visit my web site - Test Force Muscle Testosterone Booster

    ReplyDelete
  13. Anonymous3:29 PM

    Article writing is also a excitement, if you know then you can write otherwise it is complicated to write.


    Take a look at my blog post ... Beta Force And Max Thermo Burn

    ReplyDelete
  14. Anonymous1:45 AM

    Wow that was odd. I just wrote an extremely long comment but
    after I clicked submit my comment didn't appear. Grrrr... well I'm not writing all that
    over again. Anyhow, just wanted to say great blog!


    my website; Instant Payday Loans Online

    ReplyDelete
  15. Anonymous5:34 AM

    Hi it's me, I am also visiting this site on a regular basis, this site is truly nice and the users are truly sharing nice thoughts.

    Here is my blog ... Muscle X

    ReplyDelete
  16. Anonymous9:53 AM

    Everyone loves what you guys tend to be up too. This kind of clever work and exposure!
    Keep up the fantastic works guys I've added you guys to my blogroll.

    Feel free to surf to my web blog - Veluminous Skin Care

    ReplyDelete