Tuesday, November 10, 2015

I tried to print my array but all I got was gobbledegook!

This is inspired by a post at /r/javahelp.

A user posted that they were trying to print a sorted array to the console with System.out,println, and that instead of anything that made sense they got [I@4969dd64...  So clearly something was wrong and the array was not being sorted.

But that's faulty troubleshooting, as one could tell by just trying to print the array before sorting.  You would see basically the same thing.

The reason for this is that when you do a System.out.println on any object, what you're actually doing is calling 'toString()'.  If you are printing your own objects for which you've created nice methods that override the default toString, you get nicely formatted output.  If you're using an ArrayList you get a nice, comma separated list enclosed in square brackets.

Arrays are more primitive than that.  Their default toString is just the one from Object, and it does nothing more than tell you where in the heap the object is located.  This is good for verifying that something isn't null, but useless for determining the content.

If you want to see the contents of the array, you've gotta do the work yourself, my friend.  And it's not difficult at all.  This, for instance, will work for any array of Objects that themselves support a reasonable version of 'toString':

public static String arrayToString(Object[] theArray) {
    StringBuilder output = new StringBuilder("[");
    for (Object o:theArray){
        if (output.toString().length() > 1) {
            output.append(",");
        }
        output.append(o);
    }
    output.append("]");
    return output.toString();
}

View code

Monday, November 9, 2015

Indentation and style: A mild rant.

People, people, people.  When you're writing your code, you don't indent it and use brackets consistently  in order to get brownie points.  You do it to avoid long, long hours spent fruitlessly searching your code for something that should have been obvious.  You don't do it so that your code looks pretty, you do it so that your code is clear.

This was brought on by a snippet of code I found on pastebin that I rather wish I hadn't even seen.  The indentation was all over the map and brackets were placed willy-nilly, and the guy wanted help.  I wish he'd seen fit to help himself first.  It's not like formatting code is even a difficult task.  Any decent IDE will do it for you in no time flat.  This can expose all sorts of issues, like that string of closing brackets down at the end that probably should have been interrupted by code that was, instead, nested at the deepest level.

Back in the day, we'd run our code through a 'pretty printer' program before checking it in to source control.  This didn't offer the same amenities we have now, but at least when someone checked out a module they didn't find themselves recoiling in horror until after they'd reviewed the logic.

When you format your code consistently, you're not doing me a favor.  You're doing yourself a favor.  Future you will thank you for that, even as he or she is still kinda cheesed off that you chose to eat so many Doritos and didn't hit the gym enough.