DEV Community

Cover image for Java Pitfalls: Misuse of types
scottshipp
scottshipp

Posted on

Java Pitfalls: Misuse of types

Photo by Gratisography from Pexels

Java is a great language but it can really bite you if you don't watch out! Here's the second installment of a series of helpful things to watch out for in Java!

😰 Today's Pitfall

Today's pitfall is misusing types. It includes a variety of poor coding practices that can range from blatantly outrageous to rather subtle, such as:

  • Stringly-typed code

  • A null value being used to mean something important

  • Using primitive types to represent complex objects

  • And more!

We'll cover examples in the "Where you'll see it" section below.

Because these practices are widespread, and they manifest in various ways, it can be hard to notice them!

Beware!

🤔 Why it happens

Generally, inexperienced developers are more prone to the misuse of types than experienced developers. But this is not always true. Sometimes you will find whole teams, or even companies, widely using poor practices in Java, due to having previously used another language.

👀 Where you'll see it

Here are just a few common examples of the misuse of types!

Stringly-typed code

Can a String hold an address? A car? A book? Yes...

How about JSON? A database record? A line from a CSV file? An entire CSV file?

Yes! All this and more!

Except...

Please! Don't do that as a matter of course. 🤦

Not only are there far better ways to represent any of these things or formats, but also code becomes quite prone to bugs like "wrong-order" bugs.

Here's a quick example with not one, but two, bugs!

A method signature:

public NotificationResult notifyUser(String userId, String address, String message, String type)
Enter fullscreen mode Exit fullscreen mode

And a buggy usage:

notifyUser("jdoe2021", "Your subscription is about to expire." "5551231212", "text")
Enter fullscreen mode Exit fullscreen mode

What are the bugs?

  • The first is a "wrong-order" bug where the supplied parameters are in (you guessed it) the wrong order.

  • The second is that the value supplied for message type, text, does not match the expected value for that type, SMS.

Onward! To yet more examples!

Null as a meaningful value

An ecommerce site needs to represent that items are:

  • currently in-stock
  • currently not in-stock
  • backordered

OK. Makes sense to use a Boolean, backordered, which means the following, right?

Value Meaning
true backordered
false in-stock now
null neither in-stock nor backordered

Wrong! The cringe is high. Please never do this (or anything like it).

Yet, it's amazing how frequent you will see code like this, even in public libraries.

Yikes.

Using simple types to represent complex objects

Final example so that we don't go too long. But keep in mind these examples are literally only three of the most common ways to misuse types. There are so many more!

This example is really too common: using floats to represent money. It is so common, that JavaPractices has a whole article about it. You can read what they have to say, and why its a problem, over there.

🛠️ How to fix it

There is an easy and reliable cure for the misuse of types in Java.

  • Complete the Java tutorials for all trails covering the basics.

  • Learn and know the standard libraries.

  • Read Effective Java. (This is a link to the publisher's site, and is not an affiliate link! I have no association to the publisher or the author. It's just a good, universally-respected book.)

Homework

I think the homework is everything just listed under "How to fix it" above.

But I have one more request:

I'd love to hear some of the "misuses of types" that you have experienced in the comments below.

Top comments (0)