7 Tips to Write Better Java Code You Should Know

Yes, you can write short, clean Java code following 7 tips introduced in this article. Some of them might make you surprised, but trust me, they are proved practices - at least by me.

1. Use IntelliJ IDEA as your IDE

I’ve been using eclipse for 6 years and NetBeans for 3 years. I still use them sometimes, but most of the time I use IDEA only. I don’t want to start the holy war of IDEs here, but just want to tell you IDEA will remind you writing shorter, better, cleaner code based on its integrated best practices. You just press ALT+Enter and it will do the job for you, most of the time IDEA give smart and practical advice, you can also learn much new stuff from them.

To make good use of IDEA you’d better get an SSD, at least I did - my old laptop cannot run IDEA smoothly. Just a 256GB Samsung SSD will make your life much better. It’s a worthy investment if you are still using HDD.

2. Use JDK8 or higher version

From JDK8 on, many new features introduced will allow you to write shorter and more expressive code. Lambda Expressions, Functional Interfaces, Stream API and etc. You don’t need to remember them actually, as IDEA will assist you to use this features, that’s another reason why you should use IDEA. “Java 8 in Action” might be of some help to you.

3. Use Maven / Gradle

Use Maven or Gradle to manage dependencies of your projects, to build and deploy them. If you have built many fundamental libraries to be reused among many projects, you’d better introduce Nexus if these libraries will be used internally only, otherwise, you can deploy to maven central repository.

4. Use Lombok

Say goodbye to the boilerplate code of setter/getter, hashcode/equals, constructors/toString. Just one annotation @Data will work, it reduces the code you write, but take care of the generated bytecode.

5. Write Unit Tests

What? Are you serious?

Yes, I am. Testable code is usually organized better and cleaner, as it will drive you to manage the relationship of classes, the access level of methods and other stuff well beforehand. I’ve found that even minimum unit tests will make development much faster and easier, which always drive you to write shorter, cleaner and better code finally.

However, you will always hear honest opinions that “I don’t have time to write unit tests, it’s a waste of time as the deadline is approaching”. It sounds true and sometimes it’s true. But most of the time, from my experience I found it’s not true. If you don’t have time to write unit tests, you will “have” more time to fix bugs visible or invisible, without the quick feedback of unit test, the stability of code and new change usually decreases, and sometimes you may need to pray earnestly as you really don’t know what will happen or how many new bugs are introduced.

Maybe some genius programmers can write bug-free code without unit tests. But I am not and you probably are not, too. So just do it. Trust me, you will get the reward and love it soon. JUnit and TestNG would both work, though I prefer TestNG.

6. Refactoring: Frequently but Slowly

Shorter, cleaner code cannot be done at once, it needs iteratively improvements. Refactor little by little, and run test cases to make sure your change didn’t break the right behavior of the code, things will get better and better. IDEA provides great refactoring support, such as extract method, rename, inline and etc.

Martin Flower’s classic book “Refactoring: Improving the Design of Existing Code (2nd Edition)” is a must-have if you have no idea what refactoring is.

7. Visit customer regularly and get feedback from them

Honestly speaking this should be the first, but “the first will be last” here. You write code to resolve customer’s problems, to meet their requirements, to remove their pain points. Sometimes you just wasted too much time to implement features and functions that are unnecessary. But how to know that earlier? Keep in touch with customers regularly so that you can get their feedback as early as possible. However, this is not as easy as you think, even experienced product managers cannot get the point in a short time, less than programmers who mainly focus on implementing.

A practical suggestion is, if you cannot reach customer directly, you should connect to your product owner frequently and talk about your concern clearly but also politely, this will save all your time.

I’ve found these 7 tips useful to me in the past years and I hope it will also help you. Happy Coding!

PS: This article was approved and published at DZone on Dec 17, 2018 with slight changes from the Editor. Click to Read