DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Java
  4. 7 Tips to Write Better Java Code You Should Know

7 Tips to Write Better Java Code You Should Know

Check out these tips and tricks that will help you write better Java code.

Nathanael Yang user avatar by
Nathanael Yang
·
Dec. 17, 18 · Presentation
Like (33)
Save
Tweet
Share
21.04K Views

Join the DZone community and get the full member experience.

Join For Free

Yes, you can write short, clean Java code following these seven tips and tricks. Some of them might surprise you, but trust me, they are proved practices — at least by me.

1. Use IntelliJ IDEA as Your IDE

I’ve been using Eclipse for six years and NetBeans for three years. I still use them sometimes, but most of the time, I use IntelliJ IDEA only. I don’t want to start the holy war of IDEs here, but I just want to tell you IDEA will remind you to write 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, IntelliJ IDEA gives you smart and practical advice; you can also learn a variety of new information 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 JDK 8 or Higher Version

From JDK 8 and later, many new features introduced will allow you to write shorter and more expressive code, including lambda expressions, functional interfaces, stream APIs, and more. 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, builds, and deployment for your projects. 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 them to the Maven central repository.

4. Use Lombok

Say goodbye to the boilerplate code of setter/getter, hashcode/equals, and constructors/toString. Just one annotation — @Data — will work. Lombok reduces the code you write, but it also takes 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 drives you to write shorter, cleaner, and better code.

However, you will always hear opposing opinions that “I don’t have time to write unit tests" or "it’s a waste of time as the deadline is approaching.” It sounds true, and sometimes, it is true. But most of the time, from my experience, I found it’s not. 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 the unit test, stability of code, and new changes that usually decrease, and sometimes, you may need to pray earnestly as you really don’t know what will happen or how many new bugs will be 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.

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 more.

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

7. Visit Customer Regularly and Get Feedback From Them

Honestly, this should be at the top of the list, but in this case, “the best will be last” here. You write code to resolve customer’s problems, to meet their requirements, and to remove their pain points. Sometimes, you wasted too much time implementing features and functions that are unnecessary. But how can you 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 message in a short time, even 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 alot of time.

I’ve found these seven tips extremely useful over the past few years, and I hope they will help you, too. Happy coding!

intellij Java (programming language) unit test

Published at DZone with permission of Nathanael Yang. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • What’s New in Flutter 3.7?
  • An End-to-End Guide to Vue.js Testing
  • Test Execution Tutorial: A Comprehensive Guide With Examples and Best Practices
  • Test Design Guidelines for Your CI/CD Pipeline

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: