Types (in Java)

I found this great site on Java practices. One of the items in the page I found was something that I could not decide wether to agree or disagree. Here is the link.

I did however got the inspiration to write something here after a break. Types in Java and why not in other languages too. I do agree with the Java practice I linked somewhat. I personally like types in programming. This brings safety and tidiness to the code. Using strings can be very harmful and stringly typed is not something I would recommend to anybody. In fact I recommend using types.

Lets take an example from the Java practices and lets look at the credit card number. Although there probably isn’t credit cards around that have leading zeros I still think storing the value as string instead of integer is a good idea. But exposing it as a string to the rest of the system is not a good idea. I would see it as a type of its own. The type would be credit card! In Java I would create a class called CreditCard that would internally hold the credit card number as a string and some other data too, static factory method and all that stuff.

Why would I do this? Well because where ever the credit card is handled in the program it is guarded by the type. If you would pass around the number as a string you could end up in weird situations. Aint that right Mr. 1234 1234 4321 4321. I believe I have stolen your credit card number and I will expose it here: Reader. You might have gotten the idea by now. I know that YOU can prevent this from happening, I am sure of it. But what about the next guy? What about the person who maintains the code in five years? What about the guy you had to work with, you know the I-really-dont-care-about-the-code-I-write or the it-compiles-it-works guy?

Now this technique can be applied to other languages too, but at least with Java you have generics. Now using the CreditCard as a bound to some type parameter you can bring more type safety to the system and you clearly indicate to the maintainer that you are handling a credit card of some sort. I must admit that I do not know if there are generics type of typing in other non-jvm languages. C++ templates perhaps? Cant remember that far back in the past, my past that is I haven’t used templates for god knows who long.

Using strings and numbers instead of specific types are fast to code when you write the code. You just have to make sure you use them correctly. Loading a shotgun can be a fast and easy thing to do. You just have to make sure *BANG* Ouch my foot! I just shot myself in the foot! Taking the extra time to see if the data you are handling are in fact some kind of type, not a string, not a number. It might be hard at first to identify what the string, or number, actually represents. But it will get easier after some time like most things do. If it doesn’t then maybe you should consider changing careers.

You would probably save things as strings in the persistence storage and read them back as a string, but it would be a good idea to convert them to the types they actually are. Credit card is not a string. Alphanumeric generated id is not a string, Email is not a string. Your company’s business id is not a string.They are types that can be, and usually is at some point, represented as strings.

Think about the pay offs for a while. If you give some method/member function a business id as a string then compiler does not know that the given parameter is what it is supposed to be. Topys do happen. myInstance.setBusinessId(dataObject.getValueThatIsNotTheBusinessId());

If the information is wrapped inside some type class then if you try to feed some method a CreditCard instead of BusinessId the compiler will complain. It is much better to detect errors at compile time than during run time. This is one reason why generics was added to Java.

The point is to try and identify what type of thing is represented by the string. If you try to represent simply text like “Hello World” then that is a string. If you try to represent something else then that could be a type of its own. You can represent many things as a string, but the string is simply a textual representation of the type and useful for storing into the persistence storage or sending it trough HTTP. You can use helping tools to convert the type into a string and the string back into a type. Personally what I think is the most problematic for people is to try to think what does the string actually represent. And other thing that might be very difficult is to know where to draw the line. For example I would create a type Address that contains most of the information as a string. You can if you want to tighten the type safety by creating a type for street address, locality and postal code, but depending on the situation that could be overkill. It would be sufficient to just agree that the type Address is always used. Where to draw the line is something I cannot say or teach. I have my own personal flavours about where to draw the line. Also knowing what the language can offer and what libraries are available is a great help and reduces writing duplicate code. One good example is Apache Commons utilities.

I can tell you, and you can read this from Effective Java or million other places, how much fun it is to track down a bug which is caused by poor typing. Especially when the bug report is in form of “This doesn’t work” and the bug is actually a symptom of the actual problem. For example you get ClassCastException and you start to look at the stack trace and see the code where it happened. You might end up looking in the wrong place or you might not find the actual place where the real problem occurred. And you can read from nearly 40 years old book and see that it also warns about the fact that fixing only the symptom will not fix the problem. Man 40 years old that book is older than me and still today people haven’t learned. Will we ever learn? Learn what? Something from an ancient book that do not apply to this project,because this project is different. This project is different because I say so. If your sarcasm detector just went off then I can say that it is working.

Leave a Reply