In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "Why give up using Lombok". In daily operation, I believe many people have doubts about why they give up using Lombok. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the question of "why give up using Lombok"! Next, please follow the editor to study!
In the face of the many "mind-wandering" provided by Lombok, you won't mind adding a plug-in to IDE. For IntelliJ IDEA players, simply search "Lombok Plugin" to find the artifact and install it. Falling in love with Lombok begins with the installation of the Lombok plug-in, and hate has sprouted ever since.
Before using Lombok, our source code looked like this:
Public class MyObject {private Long id; private String name; private int age; private int gender; 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 getAge () {return age } public void setAge (int age) {this.age = age;} public int getGender () {return gender;} public void setGender (int gender) {this.gender = gender;} @ Override public boolean equals (Object o) {if (this = = o) {return true } if (o = = null | | getClass ()! = o.getClass ()) {return false;} MyObject obj = (MyObject) o; return age = obj.age & & gender = obj.gender & & Objects.equals (id,obj.id) & & Objects.queals (name,obj.name) } @ Override public int hashCode () {return Objects.hash (id,name,age,gender);} @ Override public String toString () {return "MyObject {" + "id=" + id+ "name=" + name+ "age=" + age+ "gender=" + gander+ "};}}
Every JavaBean is filled with template code like getter,setter,equals,hashCode and toString above, which looks like a fat person (admittedly, Java is a flawed programming language).
When we install the Lombok plug-in, IDE will recognize its cool annotations, and after using Lombok's @ Getter and @ Setter annotations, the code will look slim like this:
@ Getter @ Setter public class MyObject {private Long id; private String name; private int age; private int gender; @ Override public boolean equals (Object o) {if (this = = o) {return true;} if (o = = null | | getClass ()! = o.getClass ()) {return false;} MyObject obj = (MyObject) o Return age = obj.age & & gender = obj.gender & & Objects.equals (id,obj.id) & & Objects.queals (name,obj.name);} @ Override public int hashCode () {return Objects.hash (id,name,age,gender) @ Override public String toString () {return "MyObject {" + "id=" + id+ "name=" + name+ "age=" + age+ "gender=" + gander+ "}";}}
Does the code look much better now? But this is not the best time yet. Now that all the other methods have been replaced, let's remove the toString method as well. As you wish, you can use the @ ToString annotation to remove the correct method:
@ Getter @ Setter @ EqualsAndHashCode public class MyObject {private Long id; private String name; private int age; private int gender; @ Override public String toString () {return "MyObject {" + "id=" + id+ "name=" + name+ "age=" + age+ "gender=" + gander+ "}";}
After the Lombok trick, doesn't it look cool, slim and sexy compared to the original code? You think it's over?
Much more than that, you will find that a big annotation on the class name looks awkward. Lombok provides a combined annotation @ Data to replace the Xiang-like thing on the class name:
@ Data public class MyObject {private Long id; private String name; private int age; private int gender;}
Now, does Lombok make your object perfect in your mind? The devil's "figure" is cool and refined.
Lombok also has some other annotations, such as @ Slf4j,@NoArgsConstructor,@AllArgsConstructor, etc., and introducing the usage of Lombok is not the focus of this article.
The above changes in the number of lines of code may be the main reason why countless programmers fall in love with Lombok, which is like a fat person gradually becoming a slim person.
At the same time, it also shows you a phenomenon: do you think programmers are lazy? Sometimes they are lazier than you think. At the same time, it also planted a curse for the code.
Distorted aesthetics, the hidden danger of love
The distorted aesthetics leads to the sub-health of the object being examined. After using the Lombok plug-in, our code is also in a "sub-health" state.
Let's go back to the sentence at the beginning: all the source code is often used for reading and very little time for execution.
In essence, we all seek to reduce the boilerplate code in the program to make its code more concise and concise, so as to improve the readability and maintainability of the code.
But Lombok does not achieve the vision we pursue, it just takes advantage of the Java language in the compile time gap, using a very clever way to inject (write) the methods we need into the current class, this process is much like our code in hack, it's just a cool trick.
This trick is not smart and secure, but destroys the existing features of Java code and the readability of the code.
Now, combined with my own experience after using Lombok, I will talk about some of the major pain points brought about by Lombok.
JDK version issu
When I wanted to upgrade the JDK of my existing project from Java 8 to Java 11:00, I found that Lombok was not working properly.
So I had to remove all Lombok annotations from the project source code and use the features that come with IDE to generate getter/setter,equals,hashCode,toString and constructors.
You can also use the Delombok tool to do this, but it will eventually take a lot of your time.
Coercive use
When you use Lombok in your source code, and your code happens to be used by other people, then people who rely on your code must also install the Lombok plug-in (whether they like it or not) and take the time to understand the use of Lombok annotations, if not, the code will not work properly. After using Lombok, I found that this is a very rogue behavior.
Poor readability
Lombok hides the details of JavaBean encapsulation, and if you use the @ AllArgsConstructor annotation, it provides a giant constructor that gives outsiders the opportunity to modify all properties in the class when initializing the object.
First of all, this is extremely insecure because we do not want to modify a family property in a class; in addition, if there are dozens of properties in a class, a constructor containing dozens of parameters will be injected into the class by Lombo k, which is irrational behavior.
Second, the order of constructor parameters is completely controlled by Lombok, which we can't control. Only when you need to debug do you find a strange "Xiao Qiang" waiting for you.
Finally, before running the code, you can only imagine what all the methods in JavaBean look like, you can't see them.
Increased code coupling
When you use Lombok to write code for a module, the rest of the code that depends on this module needs to introduce Lombok dependencies, and you also need to install Lombok plug-ins in IDE.
Although the dependency package of Lombok is not large, just because Lombok is used in one of the places, all the other relying parties have to be forced to join Lombok's Jar package, which is an intrusive coupling, which will be a disaster if there is a problem with the JDK version.
the loss outweighs the gain
Using Lombok feels good for a while, but it pollutes your code, destroys the integrity, readability and security of Java code, and increases the team's technical debt, which is an operation that does more harm than good.
If you really want to refine your code while taking into account both readability and coding efficiency, you might as well use the mainstream JVM-based language Scala or Kotlin.
At this point, the study of "Why give up using Lombok" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.