Terminally Incoherent

Utterly random, incoherent and disjointed rants and ramblings...

Friday, April 21, 2006

Arcane Java Operators

I previously posted about the nifty ?: Java operator that I personally love, but that I never see in the code. And then I realized that there are quite a few instances of Java specific stuff that no one really knows or cares about. It seems that most Java instructors these days are C++ people who read a Java book - and thus they rely heavily on their C++ experience.

And that's fine, you can really survive in Java world with a C++ like mindset (as long as you don't try to pass integers by reference). But you miss out on all nifty little language nuggets and syntactic sugar that Java has to offer.

For example, Let's say you have an ArrayList, a Vector or another Collection class. At some point you want to extract something out of it. Since you populate your collection class you know what should be in it. But what happens if someone sticks in an Integer into a Vector that should contain only Strings?

You can always do this:

try
{
    Sting foo = (String) vector.get(i);
}
catch(ClassCastException e)
{
    // error handling
}


This is IMHO a PITA. Of course Java has a much more elegant solution to a problem like that. It is the instanceof operator:

if(vector.get(i) instanceof String)
    // do something
else
    // error handling


Java will gladly let you know, what type of object you are dealing with, as long as you ask it the right way. Granted that this example is trivial, but in the long run, instanceof can save you allot of catching and handling runtime exceptions.

Operators such as instanceof or ?: do not exist in C like languages. So most C oriented Java instructors ignore them completely.

Allot of programmers do not understand the this keyword. For example most people do this:

public class Foobar
{
    private int foo, bar;

    public Foobar(int a, int b) { foo = a; bar = b; }
    public Foobar(int a) { foo = a; bar = 0; }
    public Foobar() { foo = 0; bar = 0; }
}


This is an ok code, but imagine having 5 or 8 fields to handle. It becomes real messy, really quick. What if you have 12 constructors need to change the name of bar to something else? What if you decide to initialize values to 1 instead of 0?

What you shour really do is this:

public class Foobar
{
    private final DEFAULT =0;
    private int foo, bar;

    public Foobar(int a, int b) { foo = a; bar = b; }
    public Foobar(int a) { this(a, DEFAULT); }
    public Foobar() { this(DEFAULT); }
}


This is a good coding practice from the get-go. If you ever rename your fields, or change the default initialization value, all you need to do is to edit the top constructor. If your code is written well, you rarely need ractoring tools for minor stuff like this.

I think there is simply a fundamental issue with teaching the students to use the correct tools for the right job.

I cannot tell you how many times I have seen people implementing XOR like so:

if(foo)
    foobar();
else if(bar)
    foobar();
else
    barfoo();


But Java has a perfectly good boolean exclusive or operator:

if(foo ^ bar)
    foobar();
else
    barfoo();


There is a xor operator in C, so I just see no reason why most college level programmers only use && and ||.

This is kinda like using the break on the default case of the switch statement. Code that is not wrong, but obviously redundant, or unnecessarily convoluted annoys me.

1 Comments:

  • At Fri Apr 21, 09:55:00 PM, Anonymous Anonymous said…

    I think you have a great point here--and I think you are right about why people are taught a specific way of coding. It is often related to something they already know, e.g. the infamous "Java for programmers" class that many schools have. Instead of teaching you cool things that Java could do, they attempt to show you that Java is similar to that which you already know.

    What I really liked about your simple examples is that the code is so much more readable and understandable. And isn't that the point of good coding?

    Nice blog.

     

Post a Comment

<< Home