The Java world starts to be boring…

Luca | February 28, 2007

Last week I’ve been to the JavaWUG London meeting, and , among the others topics, we discussed of Rife and Grails.

What are these ?

Grails is a sort of Java port of Ruby on Rails: take Groovy (the G of the framework name) and mix with the Rails ideas (scaffolding ,strong usage of name convention etc etc) and you obtain Grails.

Rife is a web framework that work on the similar concept of Rails: define your model somewhere, the framework will create the basic infrastructure for yoand then you can have fun adding some more sugar to your web application.

Both look like really interesting frameworks but…they’re just taking a smart idea (the RoR one) and apply it in JavaLand.

Where is the time when the innovation was coming from the Java community ? Probably gone.

Nowadays the language is mainstream and from my point of view is loosing that geek soul that transforms a technology in a catalyser for a community.

useful(?) comment

Luca | February 24, 2007

Yesterday while I was working on a legacy class I found this snippet:

//Fetch all required model data.
ProjectDetail[] projects = evolveProxy.getAllProjects();

WOW!!
This comment is sooo useful, the getAllProjects() method has a really awkward name!!

It’s still totally unclear to me why (part of) the IT industry thinks that such things are useful and (even worst) a tangible sign of quality in the delivered code.

Is Agitar learning from past errors ?

Luca | February 17, 2007

Piergiuliano Bossi wrote an interesting feedback about AgitarOne, the new product from Agitar Software.

I’d like to add some thoughts from my experience with Agitator, AgitarOne’s parent.

I had used Agitator for an year; what Agitator used to do is invoke one method several times(one hundred times or something like that…don’t remember the default setting) using random combination of the input parameters.
This way of “testing” a method had some implications:

  • if the method was fairly complex (mainly if the input parameters weren’t only primitive types and Strings but other objects) and the design wasn’t nearly perfect the configuration of Agitator was absolutely a pain and the obtained results weren’t really reliable (lot of uncovered snippets of code and some expected behaviours not tested).
  • I am often interested in testing an happy path e some well-known corner cases, not all the available combinations! Agitator instead tried every single combinations(unless you start trying to configure its “factories”, a sort of “trigger from hell” with a level of usability of 1 in a 1 to 10 range). In this scenario you test several useless combinations and this takes time, often a lot of time (yes, Agitator was quite greedy about RAM an CPU cycles).

During a whole year of (desperate) tentatives of using the tool, I spotted other goodies, like bugs in the way the tool determined the code coverage: in a word, the coverage changes due to comment/uncomment of a snippet of code that should be totally ininfluent for Agitator’s code strategy. Obviously every software has bugs, but things like that just reduced my confidence with a tool that was frustating my eagerness in using it in the proper way.

I had a quick look at the web demo available on the JUnitFactory website, and AgitarOne looks smarter than Agitator: AgitarOne tries to do some sort of syntactic analisys in order to understand what is really important to test and what is not.

I’d like to see it at work with some real word code, where the design is not perfect and the complexity of methods is a bit higher than paySalary(double basicSalary, int age).

A part from this pretty technical details, it’s also quite interesting to see how Agitar’s marketing on this product is drammatically changed: Agitator was the alternative to JUnit, and Agitar claimed that was possible to do TDD with that tool. AgitarOne generates unit tests, they’re highlighting the importance of the the tool specially for legacy code and they’re saying that the tool helps you in completing the suite of tests you obtained from TDD.

Quite a big change, uh ? ;-)

looks nice but…

Luca | February 5, 2007

A good design should follow (among the others) the Open-Closed Principle: you should be able to add new functionalities without amend the existing code.

In order to have a playground where we can continue this discussion ;-) , let’s define a scenario: we have several strategies to validate a string(param1) and we use a factory method in order to obtain the correct strategy implementation from time to time.

A common implementation of the factory method in this scenario can be:
if (type.equals(MyTypes.BOOLEAN)) {
return new BooleanValidator(param1);
} else if (type.equals(MyTypes.NUMBER)) {
return new NumberValidator(param1);
} else if (type.equals(MyTypes.DATE)) {
return new DateValidator(param1);
}

Obviously the if with multiple branches smells to us like a three-days old sandwich in our lunch-bag :-) , and we are also breaking the OCP!!
If we are in JavaLand(but this is also valid for other languages such -for example- C#) reflection can help us: if we define that all the classes that implement a specific behaviour(in our example the validation of a string) stay in a defined package (let’s say P), the factory method can simply(?)� go through the classes in P and instantiate what we need thanksto the� reflection mechanism.

But..is that so simple ??

final Map validators = new HashMap();
[some code that through reflection populate the map with all the classes within the package P and assign the right key for each entry]
[...]
try {
Class theClass = validators.get(type);
Constructor constructor = theClass.getConstructor(new Class[]{CelonaDataTypes.class, String.class});
ValidatorInterface validator = (ValidatorInterface)constructor.newInstance(new Object[]{param1);
return validator;
} catch (InvocationTargetException e) {
throw new MyException(e.getMessage());
} catch (NoSuchMethodException e) {
throw new MyException(e.getMessage());
} catch (InstantiationException e) {
throw new MyException(e.getMessage());
} catch (IllegalAccessException e) {
throw new MyException(e.getMessage());
}
The latter solution from a technical point of view is smart and elegant, but sounds to me terribly unclear.

The (annoying) necessity of managing all the checked expections in JavaLand increase the white noise around the code. Furthermore the reflection APIs in Java are not so clear: if I find this code while I’m trying to learn a new code base a big WTF will appear over my head :-) !!

The former solution instead is surely a bit tacky, but is probably more readable (but, yes, I’m breaking the OCP and therefore I’m out of law). The second moreover looks a bit like a candy for my ego: my geek side will be really happy….but what about customers and the other team members ?

I’m not totally sure if my point of view is reasonable(any feedback is appreciated ;-) ) or if I’m simply scared by the increased complexity of my code.

But while I can agree that the first is nearly simplicistic, I’m not sure that the second is enough simple.

And if it’s not simple, I really don’t like it…