Michael Flanakin's Web Log

Comments and complaints on software and technology in general

  Home  |   Contact  |   Syndication    |   Login
  159 Posts | 18 Stories | 89 Comments | 530 Trackbacks

News

This weblog is no longer being maintained. For the latest, check out www.michaelflanakin.com!

Article Categories

Archives

Post Categories

Image Galleries

Miscellaneous

Coming up in the next release of Java, J2SE 5 (aka J2SE 1.5), there will be a new feature: static imports. I thought this was kind of interesting. I'll give an example, but before I do, let me explain something... Like C#'s using and VB's Import statements, Java has a similar import statement; however, Java's import statement can [but does not have to] specify a class name instead of a namespace (or package, in Java terms). I don't really know what the benefit of this is, but it's possible. I guess one thing is that it lets you see at a glance what external classes are being used. Beyond that, however, I don't really know of any reason to do it that way. Here's an example:

ServiceLocations.java

package com.indigo.examples;
public class IfUtil {
    public static object decode(object ifThis, object equalsThis, object returnThis)
    { ... }

    public static object nullValue(object ifNull, object returnThis)
    { ... }
}

This class provides generic conditional logic capabilities. The decode() method takes in two values, compares them, and if they are equal, returns the third value; if not, the first (ifThis) value is returned. There are usually overloads for this to provide more extensive options, but I'll not worry about that right now. The nullValue() method checks to see if a value is null and if it is, returns the second value; otherwise, the original value is returned.

And, here's the follow-on implementation of the static import:

Foo.java

import static com.indigo.examples.IfUtil;
public class Foo {
    public static void main(String[] args) {
        // outputs: 1
        System.out.println( decode(1, 2, 3) );

        // outputs: 3
        System.out.println( decode(1, 1, 3) );

        // outputs: "No value specified"
        System.out.println( nullValue(null, "No value specified") );
    }
}

So, I'm sure you can see how this is trying to add functions (no, not "methods") back to Java. They are "functions" because "methods" are actions taken on or by instantiated objects.

And, more to why I started this post, I'd like to see something like this in .NET (or, at least C#). Assuming a similar IfUtil class in C#, the import could look something like this:

Foo.cs

using static Indigo.Examples.IfUtil;
public class Foo
{
    public static void main(string[] args)
    {
        // outputs: 1
        MessageBox.Show( Decode(1, 2, 3) );

        // outputs: 3
        MessageBox.Show( Decode(1, 1, 3) );

        // outputs: "No value specified"
        MessageBox.Show( NullValue(null, "No value specified") );
    }
}

I think that this would be a nice way to implement static utility methods that some people seem to be missing (1, 2). Personally, I don't know how much I'd use it (except for in examples like the above). The only problem is that, as a new programmer maintaining this code, you may not realize that Decode() belongs to the IfUtil class. And, to make matters worse, what happens if you have 3 or 4 static imports? That will just make the whole thing confusing as hell. The only solution I have to that is to register specific methods. Perhaps one of the following:

// specify an exact method instance
using static Indigo.Examples.IfUtil.Decode(object, object, object);

// specify all method instances
using static Indigo.Examples.IfUtil.Decode(*);

// specify all static members
using static Indigo.Examples.IfUtil.*;

This may or may not ease the readability and maintainability of it, but for those who are begging for similar functionality, it could be an option.

posted on Friday, July 23, 2004 5:28 AM