VOOZH about

URL: https://thenewstack.io/seven-java-mistakes-to-conquer/

⇱ 7 Java Mistakes to Conquer - The New Stack


TNS
SUBSCRIBE
Join our community of software engineering leaders and aspirational developers. Always stay in-the-know by getting the most important news and exclusive content delivered fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter in the past. Click the button below to open the re-subscribe form in a new tab. When you're done, simply close that tab and continue with this form to complete your subscription.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!

We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.

What’s next?

Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.

Follow TNS on your favorite social media networks.

Become a TNS follower on LinkedIn.

Check out the latest featured and trending stories while you wait for your first TNS newsletter.

PREV
1 of 2
NEXT
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
Thanks for your opinion! Subscribe below to get the final results, published exclusively in our TNS Update newsletter:
NEW! Try Stackie AI
From clobbered drafts to real-time sync
Apr 14th 2026 10:00am, by David Moore
TypeScript 6.0 RC arrives as a bridge to a faster future
Mar 14th 2026 9:00am, by Darryl K. Taft
Mastra empowers web devs to build AI agents in TypeScript
Jan 28th 2026 11:00am, by Loraine Lawson
2024-01-26 06:11:51
7 Java Mistakes to Conquer
sponsor-sonarsource,sponsored-post-contributed,
Software Development / Software Testing

7 Java Mistakes to Conquer

A dive into the most common errors in Java projects, from the more than 600 rules covering the language, taking quality and security into account.
Jan 26th, 2024 6:11am by Jonathan Vila Lopez
👁 Featued image for: 7 Java Mistakes to Conquer
Image from vchal on Shutterstock.
Sonar sponsored this post. Insight Partners is an investor in Sonar and TNS.

There’s no denying the necessity of clean code — code that is consistent, intentional, adaptable and responsible. Ask any developer and they’ll agree that it should be top of mind when creating a project. But code is only as good as its weakest link, and as seen in the telemetry data from SonarLint, there are still plenty of issues that appear in the giant list of analyzed projects.

Even though some of the problems can look trivial, they can have a significant impact on software when it comes to security, performance and maintenance. That’s why I’ve compiled a list of the most common errors we find in Java projects, from the more than 600 rules covering the language, taking quality and security into consideration. With these in mind, you can better set yourself up to create code that is continuously consistent, intentional, adaptable and responsible — all at a huge benefit and low level of effort.

1. Code Commented Out

Code commented out only presents a challenge to its readability, so it should be removed for clarity. This eliminates uncertainty for the reader, as it can be difficult to ascertain whether the code was commented out temporarily or should have just been removed. A helpful hint to follow for this: If it doesn’t apply to the submitted feature, take it out or uncomment it if it is a temporary disabling.

Here’s an example:

The good news is that if you later need the commented code, you can simply retrieve it from the version control system.

2. Neglected ‘TODO’ Tags

Leaving these comments in the source code, which will likely have a long lifespan, leads to incomplete code that can affect software on several fronts. For instance, when it comes to collaboration within a team, some members may not know which features will be included in the final release. These tags can also give the appearance of being able to be handled later, rather than implementing those parts now and reducing the chance of future bugs. Additionally, TODO blocks can lead to leaky performance in the future.

Here’s a real-life example of a project called Apache Camel with a TODO line introduced almost a decade ago.

To avoid these issues, don’t add new TODO blocks. Instead, implement the feature before submitting the final code — or record these tasks into the task manager so it’s clear how to tackle them in the future.

3. Duplicated String Literals

Duplicated strings lead to extra work or missing changes when those values must be changed to adjust to new conditions. Instead, use constants to store string literals. This makes refactoring easier and improves the consistency of the codebase.

An example of how to do this:

4. High Cognitive Complexity of Functions

More commonly, we hear about cyclomatic complexity, which measures how many paths are used in the code and helps determine the reading complexity for a given part of the code. But this concept can’t help figure out the real maintainability level that needs more considerations than just the number of conditionals or loops.

Reducing the code complexity is a critical piece in making refactoring, fixes and evolutions easier, as developers spend a lot more time reading code than they do writing it. Projects can often be hard to read or understand, and this issue makes it hard to know its intention and tackle both its maintenance and evolution. Developers should invest in refactoring code with a high level of cognitive complexity so the codebase becomes easier to understand and maintain in the long run.

With new code, it’s good to reference the complexity index and invest time in reducing it to the configured threshold that should be low enough.

5. Unused Elements

It’s easy for developers, as they code a new feature, to create elements of the code that eventually don’t have a purpose. These elements don’t cause runtime errors or failing tests, so they can be tricky to identify even though they need to be removed. But in the worst case, they can force us to rethink the code entirely.

These unused elements reduce the code’s readability, which makes it even harder to pinpoint the intention of the code and can cause a lack of confidence in its completion. Take them out. Check the unused code and remove whatever is no longer useful, or consider if they’re missing code that may use those elements. Take a look:

6. Raw Types

In Java, don’t use generic types without type parameters — it’ll avoid type checking and catching unsafe code during compilation, which makes everything visible during runtime. Instead, use specific types that allow users of those variables to understand what’s really expected and will eliminate surprises during that runtime. See below:

7. Thrown Generic Exceptions

Using generic exceptions prevents the calling methods from handling different system-generated exceptions and application-generated errors. To avoid this, create a custom system of exceptions that provides enough information to the caller so they can decide what to do, fully equipped with a detailed and differentiated list of caches.

Avoiding Mistakes Leads to Better Software

All developers go into coding with the intention of creating a final product that’s high quality, reliable, adaptable and secure. But it’s easy for these seemingly small, everyday mistakes to get in the way of that goal. Taking note of these issues moving forward and doing your best to avoid them will only set you up for the kind of clean code software that allows a business to thrive.

Sonar is the industry standard for code verification and automated code review, trusted by 75% of the Fortune 100. Its SonarQube platform analyzes over 750 billion lines of code daily, helping to prevent outages, reduce risk, lower technical debt, and ensure compliance.
Learn More
The latest from Sonar
Hear more from our sponsor
TRENDING STORIES
Jonathan Vila Lopez is developer advocate at Sonar. He is a Java Champion and cofounder of the conferences in Spain JBCNConf and DevBcn, an organizer at Barcelona Java Users Group (JUG), and a member of BarcelonaJUG. He has also been...
Read more from Jonathan Vila Lopez
Sonar sponsored this post. Insight Partners is an investor in Sonar and TNS.
SHARE THIS STORY
TRENDING STORIES
SHARE THIS STORY
TRENDING STORIES
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.