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 is misusing types. It includes a variety of poor coding practices that can range from blatantly outrageous to rather subtle, such as:
A null value being used to mean something important
Using primitive types to represent complex objects
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!
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.
Here are just a few common examples of the misuse of types!
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!
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)
And a buggy usage:
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,
Onward! To yet more examples!
An ecommerce site needs to represent that items are:
- currently in-stock
- currently not in-stock
OK. Makes sense to use a Boolean,
backordered, which means the following, right?
|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.
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.
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.)
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.