Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write elegant Java code

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to write elegant Java code". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to write elegant Java code.

1. Use IntelliJ IDEA as your integrated development environment (IDE)

Although I've been using Eclipse for six years and NetBeans for three years, and I still use them occasionally, most of the time now, I only use IntelliJ IDEA. I don't want to have a big discussion about IDE here, but I just want to tell you that IDEA can constantly prompt you to write shorter, better, and more concise code based on its own best integration practices standards.

All you have to do is press Alt + Enter and it will automatically start working for you. Most of the time, IntelliJ IDEA can provide you with a variety of intelligent and practical code suggestions. Of course, you can also learn different programming knowledge and skills from it.

In addition, it is recommended to use IDEA shortcuts to speed up coding. IDEA has many convenient shortcuts and functions, so you will be familiar with it if you use more!

In order to better obtain the service performance of IDEA, you'd better use solid state hard drive (SSD) on your computer. In the case of my own old laptop, it can no longer run IDEA smoothly because it uses a traditional hard drive (HDD). So I personally recommend that you use at least one solid state drive of 256 GB size.

two。 Use JDK 8 or later

JDK 8 and later have introduced many new features such as lambda expressions (lambda expression, an anonymous function), functional interfaces, and streaming interfaces (Stream APIs, which provide a more readable source code implementation), all of which help you write shorter, higher-performance Java code.

Of course, you don't need to remember these features one by one, because the IDEA mentioned earlier will help you implement these functions and services in the actual programming process. That's why I suggested IDEA to you in the first place.

3. Use Maven/Gradle

Use Maven (an open source project management tool written in pure Java, see) or Gradle (a project automation build tool based on Apache Ant and Maven, see) to manage dependencies in your code and to build and deploy your own projects.

Most Java back-end developers use Maven, perhaps because almost all Java developers are familiar with XML. Let's see what Maven can do for us.

What can Maven do for us?

We can easily build the project using maven.

We can use maven's help to easily add jar and other dependencies for the project.

Maven provides project information (log documents, dependency lists, unit test reports, etc.)

Maven is very helpful to the project when updating the central repository of JAR and other dependencies.

With the help of Maven, we can build any number of projects as output types, such as JAR,WAR, and so on, without any scripting.

With Maven, we can easily integrate our project with a source control system such as Subversion or Git.

4. Use Lombok

It's time to say goodbye to boilerplate code (boilerplate code) such as setter/getter, ashcode/equals, and constructors/toString, and all you need is an annotation: @ Data.

Lombok is a tool that can help developers simplify and eliminate bloated Java code through simple annotations (see for details). It can not only reduce the amount of code you write, but also help you take care of the generated bytecode.

5. Write unit tests

Testable code usually means more reasonable and concise code quality in the organizational structure. Because it drives you to manage the relationships between classes, the access levels of various methods, and other aspects in advance. I even found that even the smallest unit tests can promote a faster and easier development process, which in turn allows me to write shorter, flatter, faster Java code.

Of course, in real development work, you always hear objections such as "I don't have time to write unit tests at all" or "the project time node is coming, so don't waste your time on unit tests." This sounds reasonable, but in my experience, in most cases, this is not the case.

If you don't have time to write unit tests, do you have more time to fix visible or invisible bug in your code? If you skip the unit test, the hastily completed code will not guarantee stability. Especially for some new code changes, you have no way to know whether there are hidden errors in the new code through timely feedback, and whether it will cause unpredictable exception problems in a particular scenario that will run in the future.

Generally speaking, Junit and TestNG are two excellent Java applications and unit testing frameworks. Personally, I prefer to use TestNG.

6. Refactoring: common, but also slow

Concise and capable Java program code is never achieved overnight, it often requires you to ponder and improve over and over again. By refactoring the code line by line and running various test cases, you can ensure that your changes do not break the correct functionality of your existing code.

Similarly, IDEA provides great support for code refactoring, including extraction methods (extract method, splitting a large function into small functions), renaming, inline, and so on.

Of course, if you don't know much about what code refactoring is and what it does, Martin Fowler's classic "refactoring: improving the Design of existing Code (version 2), Refactoring: Improving the Design of Existing Code (2nd Edition)" is definitely a must-have reference book.

7. Pay attention to code specification

Develop good coding habits from the first day you learn programming. Naming packages, classes, and methods is the most basic.

Recommended reading:

8. Contact customers regularly to get their feedback

Last but not least: customers pay you to solve their problems, meet their needs, and solve their pain points by writing code. However, you may unwittingly spend too much time implementing special features that you think are important but insignificant to your customers, ignoring the overall robustness and maintainability of your code. So how can we find the problem as soon as possible? Please keep in touch with customers regularly to get their feedback as soon as possible.

In other words, easier said than done, even experienced product managers may not be able to understand the true meaning of requirements in a short time, let alone those "programmers" who only focus on functional implementation.

Therefore, a practical suggestion is: if you cannot contact the end user directly, please try to communicate politely and frequently with the product manager or operation and maintenance staff of the system. Sharpening the knife without mistakenly chopping firewood, these time investment is definitely worth saving time in the later stage.

Thank you for your reading, the above is the content of "how to write elegant Java code". After the study of this article, I believe you have a deeper understanding of how to write elegant Java code, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report