JavaRush /Java Blog /Random EN /Coffee break #70. How to write Java code faster with Lomb...

Coffee break #70. How to write Java code faster with Lombok. Training list for beginner programmers

Published in the Random EN group

How to Speed ​​up Writing Java Code with Lombok

Source: Dev.to We write the same boilerplate code in every application. For example:
  • Getters;
  • Setters;
  • Designers;
  • Builder design pattern;
  • and much more…
Wouldn't it be nice if this routine work could be entrusted to someone? And this is where Lombok can help us.Coffee break #70.  How to write Java code faster with Lombok.  My training list for beginner programmers - 1

What is he doing?

It generates byte code for these common tasks (getters, setters, etc.) and puts them into a .class, making them usable in the code we write.

How does this happen?

You need to add the Lombok dependency to your Maven build. Then you need to annotate the desired classes, fields with some Lombok annotations. Let's look at the code! Without Lombok:
public class Human {
    private int id;
    private String name;
    private int ageInYears;

    public Human() { }

    public Human(int id, String name, int ageInYears) {
        this.id = id;
        this.name = name;
        this.ageInYears = ageInYears;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public int getAgeInYears() {
        return ageInYears;
    }

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

    public void setAgeInYears(int ageInYears) {
        this.ageInYears = ageInYears;
    }

    // Builder pattern
  public Human id(int id){
        this.id = id;
        return this;
    }
    public Human name(String name){
        this.name = name;
        return this;
    }
    public Human ageInYears(int ageInYears){
        this.ageInYears = ageInYears;
        return this;
    }

    @Override
  public String toString(){
        return String.format("Human(id=%s, name=%s, ageInYears=%s)",
                this.id, this.name, this.ageInYears);
    }
}
Now let's use Lombok:
import lombok.*;
@Getter @Setter
@AllArgsConstructor @NoArgsConstructor
@Builder @ToString
public class Human {
    @Setter(AccessLevel.NONE)
    private int id;
    private String name;
    private int ageInYears;
}
The Maven dependency looks like this:
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.16</version>
    <scope>provided</scope>
</dependency>
Note: The version part will change depending on the latest Lombok release.

What have we done?

We used annotations to create getters, setters, constructors, the Builder design pattern, and toString implementation. We can also make changes to some annotations, such as the setter for the id field. We set its access level to None: this means we don't create a setter for it.

Was it worth doing?

We wrote 52 lines when we weren't using Lombok. We wrote 8 lines using Lombok. Lombok helped us reduce the code size by almost 4 times . This score can be improved if we have more variables in our classrooms. Lombok has a lot more annotations with lots of useful information. To view them, visit the website .

Conclusion

Lombok helps you focus on your business code and not worry about the small details (getters/setters/constructors/general design patterns and other Java constructs).

Training list for beginner programmers

Source: DZone My friend has a son who will soon graduate from high school. He knows a little programming and is wondering whether he should become a programmer. Recently he asked me: “What should I teach next?” Coffee break #70.  How to write Java code faster with Lombok.  My training list for beginner programmers - 2When I first started learning programming, I assumed that the answer to the question “What should I learn next” would be a new programming technology, a new language, a new library, or something like that. As I progressed in my career, I changed my mind. In addition to honing your programming skills and learning new languages, aspiring programmers should study in related fields (I'll explain this below). In this text, I want to collect a list of skills that I would recommend programmers to master. My goal is not to list all current knowledge or provide links to tutorials. Instead, I want to help newbies become familiar with a set of tools, techniques, and skills that can help them write programs.

Command line

I guess I'm already considered "old" by some standards. In my day, using a computer meant sitting in DOS and typing commands. A lot has changed since then. It is very important to learn how to use the command line. Many tools only provide a command line interface. In some cases, you can work faster on the command line. You should be able to do basic things like:
  • Directory traversal.
  • File management (copy/move/delete).
  • Compiling and/or running source code.
If you're on Windows, I'd recommend getting familiar with Linux, perhaps using WSL2 instead of a full virtual machine. If you're on a Mac, you can use the shell built into the OS. You can also get a good experience by installing a Linux virtual machine.

Version control

You need to learn how to track your source code using version control. Version control software allows you to keep track of your project's revision history, which can be vital for debugging. It also makes it easier to collaborate on the codebase. GitHub and GitLab are two popular sites for hosting open source projects. They both use Git (as I guess the names suggest). Git is the most popular tool these days, so I would recommend getting to grips with using Git on the command line with GitHub or GitLab as your repository.

Networks

Most programs today require at least some level of networking. Understanding how networks work is important. At the very least, you should understand the basics such as IP addresses, ports, and protocols. Learning about firewalls, load balancing, proxies will pay off later on. And not just in your programming career. Agree, it’s great to understand the question “why Wi-Fi doesn’t work?” I would also recommend learning the basics of cloud services such as AWS and Azure. Trying to set up an AWS Virtual Private Cloud with subnets, login rules, gateways will give you networking experience.

Testing

An integral part of writing good software is learning how to test software. Many of us learn to program and then “test” our code. Unit and integration testing are essential skills that can be applied to almost any software you work on. Although testing may seem tedious at first, this skill will pay off and allow you to work faster and with more confidence.

Continuous Integration

Continuous integration, or CI, combines testing and version control. Every time you create a new iteration of code, you have a set of tests that run automatically. CI tools have improved significantly over the past 10 years. GitHub and GitLab have built-in CI solutions (GitHub Actions and GitLab CI, respectively) and are easy to get started with. As with testing, getting started with CI will be difficult. But once you learn the basics, you will find problems much easier and faster. I would recommend looking for a detailed tutorial with examples for the programming language you are using.

Writing skills

This is probably the area that I most underestimated when I started working as a programmer. I'm not exaggerating: I now believe that the most important skill a programmer can add to their arsenal is writing. Good writing skills mean that you can clearly explain an idea using a minimum number of words. When you learn to program, you usually work on your own, so writing isn't necessary. But when you start working in teams, you will need to write:
  • Documentation;
  • Bug reports;
  • Feature requests;
  • Customer suggestions;
  • Documents with requirements;
  • Emails (lots of emails!);
  • Text messages;
  • Blog posts (possibly);
Learn to write. Exercise. Take a writing course. It will pay dividends for the rest of your life. Also: learn to read well. When I first started professional development, I was intimidated by requirements documents. I now realize that taking a few hours to carefully study this information will save you months of wasted building the wrong thing.

Other programming languages

Nowadays, it is not enough to know just one programming language. Almost every programmer should know at least a few. It is important to study other languages ​​to learn new techniques. I recommend learning a few different categories of languages. In descending order of priority: Functional programming. Most schools still don't teach functional programming (FP). FP is a powerful approach that makes writing many types of code easier. I'm biased, but I would recommend Haskell as the best language to learn as it will help you understand FP better than many other languages. It is also useful to learn a language from the LISP family. Learning functional programming will help you write better code in almost any language. System Programming. System languages ​​belong to a lower level and allow better control over the operation of the program. By studying them, you will learn how the program works on the system, which can be very useful for understanding and debugging problems in other languages. I recommend learning Rust, then C and C++. Object-oriented languages. Java and C# are the two most popular object-oriented languages ​​in this family. (Yes, Python and C++ are also popular, but I'll highlight them separately). OOP introduced many new paradigms and is probably still the most popular programming approach, although I personally prefer functional programming approaches. However, there is a lot to learn from object-oriented programming, and chances are you'll end up writing object-oriented code in your career. Script writing. Python and Ruby are two popular scripting languages ​​in the family of object-oriented applications. In particular, Python is widely used in related fields such as DevOps and data science. Additionally, it is a simple language that is quite easy to get started with.

Programming Methods

No matter what programming language you use, it's worth familiarizing yourself with some additional techniques that go beyond your specific language, including:
  • Database programming. I highly recommend learning SQL. SQLite and PostgreSQL are two open source DBMSs to explore.
  • Parallelism and asynchronous programming. This is becoming increasingly important today.
  • Network programming , especially creating HTTP servers and clients.
  • Creating a web interface using HTML/CSS/JavaScript.
  • Serialize data in formats such as JSON, YAML and binary files.

Conclusion

The information above may seem complicated. You don't have to think that you need to learn all this to become a programmer. This is wrong. Becoming a great programmer takes time and professional practice. If you haven't started programming yet, I would recommend starting with some pet projects. Consider contributing to open source projects. This will help you get comfortable and learn from experienced coders. Many programmers love to share their knowledge with beginners.
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION