In the heart of every new programmer, programming is a sacred art. They all want their code to be clean, readable, and fault tolerant. This article brings you seven tips and tricks to help you write clean, clean Java code.
1. Use IntelliJ IDEA as an integrated development environment (IDE)
Just press Alt + Enter and it will automatically start working for you. Most of the time, IntelliJ IDEA can give you a variety of smart and useful code suggestions. Of course, you can also learn different programming knowledge and techniques from it. To better obtain the service performance of IDEA, it is better to use solid-state drives (SSDS) on your own PC. In the case of my own old laptop, it can no longer run IDEA smoothly because it uses a traditional HDD. So I personally recommend using at least one 256GB SSD.
2. Use JDK 8 or later
JDK8 and later introduce things like: Many new features, such as Lambda expressions (anonymous functions), functional interfaces, and stream APIs (which provide a more readable way to implement source code), make it easier to write shorter, higher-performing Java code.
Of course, there is no need to memorize each of these functions, because the IDEA mentioned above will help implement these functions and services during the actual programming process. This is why IDEA is recommended 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 build and deploy your own projects.
If you already have a variety of reusable base libraries for many projects, you can introduce Nexus(a Maven repository manager) for internal use only. Otherwise, they should be deployed directly into Maven’s central repository.
4. Use Lombok
It’s time to say goodbye to setter/getter, hashCode /equals, and Constructors /toString boilerplate code with a single annotation: -@data -.
Lombok is a tool that helps developers simplify and eliminate Java code bloatiness through simple annotations (see more). Not only does it reduce the amount of code you write, it also helps take care of the bytecode that is generated.
5. Write unit tests
Testable code generally means more structured, cleaner code quality. It drives pre-management of relationships between classes, access levels for methods, and more. I’ve even found that even the smallest unit tests lead to a faster, easier development process, which in turn allows me to write shorter, flat, and faster Java code.
Of course, in the real world of development, there are always objections like “I don’t have time to write unit tests” or “the project is running out of time, so don’t waste time on unit tests.” This all sounds reasonable, but in my experience, in most cases, it’s not.
If there is no time to write unit tests, is there more time to fix visible and invisible bugs in your code? If unit tests are skipped, the hastily completed code will not guarantee stability. Especially for some new code changes, there is no way to know through timely feedback whether the newly generated code has hidden errors, whether it will cause unexpected exceptions in a particular scenario in the future.
In general, Junit and TestNG are two excellent Java application and unit testing frameworks. Personally, I prefer to use TestNG.
6. Refactoring: Common, but slow
Concise Java program code is never achieved overnight, it often needs to be repeatedly considered and improved. By refactoring code line by line and running various test cases, you can ensure that your changes don’t break the correct functionality of your existing code. Similarly, IDEA offers great support for code refactoring, including extract methods (splitting a large function into smaller ones), rename, inline, and more.
Of course, if you’re not familiar with what code Refactoring is and what it does, Martin Fowler’s classic book, Refactoring: Improving the Design of Existing Code (2nd edition), Refactoring: Improving the Design of Existing Code (2nd Edition) is definitely a must-have reference book.
7. Contact customers regularly to get their feedback
Last, and perhaps most important: customers pay you to write code that solves their problems, satisfies their needs, and addresses their pain points. However, you can unknowingly spend too much time implementing special features that you think are important but don’t matter to your customers, and lose sight of the overall robustness and maintainability of your code. So how can we detect this problem early? Keep in touch with your customers to get their feedback as early as possible.
Even experienced product managers may not be able to grasp the true meaning of requirements in a short time, let alone those who only focus on functional implementation of the “code farmers”?
Therefore, a practical suggestion is to try to communicate politely and frequently with the system’s product manager or operations personnel if you cannot reach the end user directly. Sharpener does not miscut wood workers, the investment of these time for the later time saving is absolutely worth it.