Sep 18 2009

Extreme Refactoring or Is Guard clause Considered harmful?

Category: agile,javagiordano scalzo @ 4:56 pm

During the JDave try, the focus was on Bdd side of the exercise, but, nonetheless, the StringTemplater’s code was yelling “Refactor me! Refactor me!” at me :-) .

Actually, that I like least, it’s the Guard Clause, I used to split a marker in key-value form:

private String name(String[] pair) {
		if (pair.length != 2)
			return "";
		return "$" + pair[0].trim();

Usually I like that pattern, it’s a well know way to avoid nested conditional, but Anti-If Campaign, promoted by Francesco Cirillo made me think.

The first try didn’t satisfied me:

private String name(String[] pair) {
		return  (pair.length != 2) ? "" : "$" + pair[0].trim();

Indeed, I believe it’s more cryptic and ugly than the original one.

Looking the code more carefully, I saw another thing: I was violating the principle “Ask, don’t tell“, handling some data that could be tied together.

Creating the new class, I thought another thing:
Why am I using the ‘IF?”
I did for avoid a runtime exception… but is an exception evil?
It isn’t if we can handle it!

So at the end this is my final product:

package biz.scalzo.kata.stringtemplater.jdave;

import java.util.Arrays;
import java.util.Iterator;
import java.util.NoSuchElementException;

public class StringTemplater {

	public String replace(String stringToReplace) {
		return replace(stringToReplace, "");

	public String replace(String stringToReplace, String markers) {
		return replaceEmptyMarkers(replace(stringToReplace, initialIterator(markers)));

	private String replaceEmptyMarkers(String stringToReplace) {
		return stringToReplace.replaceAll("\\$\\w+", "");

	private Iterator initialIterator(String markers) {
		return Arrays.asList(markers.split(",")).iterator();

	private String replace(String stringToReplace, Iterator markers) {
		try {
			return replaceMarker(stringToReplace, markers);
		} catch (NoSuchElementException e) {
			return stringToReplace;

	private String replaceMarker(String stringToReplace,
			Iterator markers) {
		Pair pair = new Pair(;
		String newString = stringToReplace.replace(, pair.value);
		return replace(newString, markers);

	private class Pair {
		private String name;
		private String value;

		private Pair(String nameAndValue) {

		private void init(String[] nameAndValue) {
			try {
				name = "$" + nameAndValue[0].trim();
				value = nameAndValue[1].trim();
			} catch (IndexOutOfBoundsException e) {
				name = "";
				value = "";

I think a I could refactor more, but I was enough satisfied: I reached my goal to avoid IF and I’ve start to think the exception in different way.

Technorati Tags: , ,

Tags: , ,

Sep 09 2009

Infinitest: an autotest for Eclipse

Category: agile,bdd,javagiordano scalzo @ 7:00 pm

In my experimental trips in Ruby world, other than the elegance of the language, the Bdd as default and the lot of new exciting things created by the community, a thing I liked a lot was autotest , a little gem that launches our testsuite when we change a source file.
It could seem a silly utility, but I can assure it’s a really time saver and can take us in a high productive mental flow: I really missed it in Java world.

Of course, I’ve heard about Junit Max, the Kent Beck’s attempt to create a product, but my laziness delayed me to try it, until Kent got bored and dismiss the project :-( .

Today while reading distractedly the Twitterverse, I bumped into Infinitest, that seems exactly what I was looking for.
Infinitest isn’t open source, but for personal use it should be possible get a free of charge individual license.

The installation is straightforward as usual, just add to Eclipse’s software updates, and we are ready to start!

Infinitest stay in the right bottom of Eclipse, waiting far some changes:

waiting for changes

When we save some file Infinitest starts and show the results of all test it founds in the project:

red test

A nice feature is the yellow bar when there is some error in the workspace and tests can’t run:

yellow bar

That’s all!
I have to try it more, but it seems very promising and useful.

Technorati Tags: , , , ,

Tags: , , , ,

Sep 07 2009

Scrum in an Hour

Category: agile,presentationgiordano scalzo @ 10:50 pm

Some time ago, I attended a Scrum Master Course (I’m embarrassed to call it Certification…) held by the Agile pioneer and team dynamics expert Joseph Pelrine.

It was an exciting and enriching experience, although little sigular because we were only two student: Pierlugi Pugliese and me!

Anyway, back to work my boss asked me to prepare a little presentation on Scrum to the upper and middle management; so I looked around and took several sources to write a simple, but appealing presentation.

The presentation was a success so that I performed it three more time to the whole Engineering department!

Technorati Tags: , , , ,

Tags: , , , ,

« Previous Page