Large software projects and icons – it depends on the style!

Have you come across similar conversations in the context of large software projects?

Developer: I need an icon for a button.
Boss: You have MS-Paint on your computer. Why don’t you draw it yourself?
Developer: No… I am not a graphic designer… Why don’t we purchase the icons?
Boss: Hmm…We have got an intern, haven’t we? He can do it!
Developer: But he is no graphic designer either… And there are great low-cost icon libraries available to download.
Boss: I wouldn’t know how to book that… The intern will do it!

Sometimes I wonder what makes large companies tick…They spend millions on software and skimp on a few hundred dollars for nice graphics. Unfortunately, the completed applications often look as if an intern has designed the icons.

A leading supplier of icons offers his entire catalogue (approx. 250 sets with around 850,000 icons) for 399 USD. Commissions for individual icons in addition to an existing set cost approx. 50-75 USD per icon. In return you will get designs which are visually pleasing to the end user who interacts with the finished piece of software.

Anyone who allows his developers or interns to design icons, must be crazy.

The Specification Pattern

I recently stumbled across an older article from Greg Young about something called the Specification Pattern. I haven’t heard about it before but the problem sounds familiar to me as I already implemented something similar.

Digging deeper I found an interesting paper from Eric Evans and Martin Fowler explaining the idea in depth: Specifications.

There is also a small Java implementation of the pattern available at Google Code that unforunately seems to be inactive since 2008.

Contracts for Java

I personally like the idea of Design By Contract very much. The bad news is that Java does not support this natively.

The good news is that there are several libraries out there that try to implement DBC for Java:

http://jcontractor.sourceforge.net/
http://c4j.sourceforge.net/
http://oval.sourceforge.net/
http://springcontracts.sourceforge.net/
http://custos.dev.java.net/
http://code.google.com/p/javadbc/
http://www.contract4j.org/

Another bad news is that most of these projects are inactive or don’t support all aspects of DBC.

Most feature complete is the Java Modeling Language (JML). There are many tools based on this behavioral interface specification language. I checked some of them but they all seem to be very Academic and not very handy to use.

Now there is a more practical approach that uses annotations to express pre conditions, post conditions and invariants:

A detailed report that introduces the ideas behind the framework can be found here.

This looks very promising and is definitely worth giving it a try!

Parsing a Maven POM II

Due to popular demand here is the code that does the parsing described in this article some time ago:

http://www.fuin.org/files/MavenPomReader.zip

It’s just a quick hack but it works fine for all projects at fuin.org – The dependency information as on this page is actually parsed using the above Maven Pom Reader.

Here is a short example how to use it:


// Path to your local Maven repository
final File repositoryDir = new File("/data/maven/repository");

// Read the model for an artifact and a given version
final Model model = MavenPomReader.readModel(repositoryDir, "org.fuin", "serialver4j", "0.4.2");

// Print the dependencies on the console
final List<Dependency> dependencies = model.getDependencies();
for (int i = 0; i < dependencies.size(); i++) {
    final Dependency dependency = dependencies.get(i);
    System.out.println(dependency.getGroupId() + " / " + dependency.getArtifactId() + " / "
        + dependency.getVersion() + " / " + dependency.getScope());
}

Output:

com.thoughtworks.xstream / xstream / 1.3.1 / null
com.caucho / hessian / 3.1.5 / null
org.reflections / reflections / 0.9.5-RC2 / null
junit / junit / 4.8.1 / test
org.easytesting / fest-assert / 1.3 / test
org.fuin / units4j / 0.2.3 / test
org.fuin / utils4j / 0.6.7 / test

Axon Auction Example (CQRS / Command Query Responsiblity Segregation)

After a very busy period I’m finally back 😉 This is just a short note that I started creating an example project for the CQRS Axon Framework: Axon Auction Example.

Axon Framework helps to build scalable, extensible and maintainable applications by supporting developers apply the Command Query Responsiblity Segregation (CQRS) architectural pattern.

Parsing a Maven POM

Some days ago I needed some information from a Maven “pom.xml” file. First I thought about reading the information from that file using XPATH. But, after digging around with Google, I found it’s much simpler to use Maven’s own capabilities for that task.

You need only two JAR files:

  • maven-model-2.0.9.jar
  • plexus-utils-1.5.1.jar

And a small piece of code:

Reader reader = new FileReader(pomXmlFile);
try {
    MavenXpp3Reader xpp3Reader = new MavenXpp3Reader();
    Model model = xpp3Reader.read(reader);
} finally {
    reader.close();
}

The only thing that took a bit more time was to include an attached “parent POM”. If you’re interested in that, just let me know.

Add JAR files programmatically to the classpath

Everyone knows that starting a Java application from the command line is, to say the least, annoying. Ever got an application that contained several JAR files but no batch file or shell script to start the application?

You may have ended up creating a batch file like this:
java -classpath lib/abc.jar;lib/def.jar ...

A better approach is to collect the necessary JAR files with batch files or a shell script like this: http://www.fuin.org/files/create-classpath-scripts.zip (Thanks to the guys from iReport for the above code.)

If you are a lucky one, you may have had the opportunity to use Java 6, which introduced a wildcard option: java -classpath lib\* ...

But what if you create your own application and you have to load some libraries “on the fly”. At first this may seem difficult or impossible but, in fact, it’s easy:

  1. Start your application with a single main JAR file:
    java -classpath mymain.jar ...
  2. Add more JAR files programmatically:
    Utils4J.addToClasspath("file:///c:/myapp/lib/abc.jar");
    

Simple, isn’t it?

You can find the Utils4J library here.

Good source commenting is a kind of art

It’s my firm conviction that good inline source commenting is a kind of art. It’s not easy to find the right balance between too much and too little commenting in your code. And it’s even harder to write useful comments.

I recently came across the following code snippet:

// Number of pages
doc.setNoOfPages(t.getPageCount());
// State
doc.setState(t.getState());
// Text
doc.setText(t.getText().trim());
// Code
doc.setCode(t.getCode());
// GroupCode
doc.setGroupCode(t.getGroupCode());

This is a good example of completely useless comments. Even worse, the comments bloat the code!

I personally think most comments should be avoided in favor of good method names. In my experience, inline comments are often hints for possible code refactorings.

Another example from the same code:

// Rotate (Convert marker into boolean)
char rotate= t.getRotationMarker().charAt(0);
if (rotate == '1') {
    doc.setRotate(Boolean.TRUE);
} else {
    doc.setRotate(Boolean.FALSE);
}

Here we have, in fact, two separate operations and the comment states exactly that: “Rotate” means “setting the rotate property” (and is another example of a useless comment) and “Convert marker into Boolean”.

What about creating a small conversion method?

/**
* Converts a charcter value into a boolean.
*
* @param rotate Character to convert. A value of "1" will be
* interpreted as TRUE and all other values as FALSE.
*/
private Boolean toBoolean(char rotate) {
    return Boolean.valueOf(rotate == '1');
}

The calling code is then as follows:

boolean rotate = toBoolean(t.getRotationMarker().charAt(0));
doc.setRotate(rotate);

Now you have a simple JavaDoc comment and a separate setter with no comment.

Does your provider manipulate your Internet access?

Did you ever wonder why a search engine page is shown when you enter a wrong URL? This may be because your Internet access provider manipulates the results returned to your computer! My Internet provider Alice for example returns an AOL search result if a “HTTP/1.1 302” status is found.

You can check your own connection with a nice tool called Netalysr (developed at the University of California).

Never change a running system . . .

In addition to using an Internet security package, I always try to be up-to-date with my software installations to avoid security holes. So being a “good user”, I recently tried to install Windows XP Service Pack 3 and got a setup error “Access is denied”. What’s that? I never had problems installing Windows service packs so far! Checking Google for that error message returned more than 26,600 entries—Seems as if I’m not the only one with that issue . . .

Several hours and unsuccessful trials later, this is how I finally solved it:

I restored the default security settings with Microsoft’s Fix-It wizard.

It took some time for the tool to finish cleaning the settings, but after that the SP3 was successfully completed.