Java Pitfalls: Misuse of types
Tags: java, tutorial
Published: 2021-12-07 20:25:46

---
title: Java Pitfalls: Misuse of types
published: true
description: Yes, there's a lot of bad code out there!
series: Java Pitfalls
tags: java,tutorial
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8qjky30jz2b9mu95q0pi.jpg
---

> 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](https://blog.frankel.ch/coping-stringly-typed/)

- 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:

```java
public NotificationResult notifyUser(String userId, String address, String message, String type)
```

And a buggy usage:

```java
notifyUser("jdoe2021", "Your subscription is about to expire." "5551231212", "text")
```

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](http://www.javapractices.com/topic/TopicAction.do?Id=13). 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](https://docs.oracle.com/javase/tutorial/) for all trails covering the basics.

- Learn and know the standard libraries.

- Read [Effective Java](https://www.pearson.com/store/p/effective-java/P100000841083/9780134685991). (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.