Entity-relationship-oriented programming

December 10th, 2009

How I agree with Dafydd Rees when he writes:

Most Java and C# programmers have no idea how to do object-oriented programming.

They’re really doing entity-relationship-oriented programming. Trouble is, they’re in the majority so they form a self-reinforcing group that take for granted knowledge and skills that they don’t even realise that they don’t have.

Aaand… I’m afraid I was in the E-R-oriented camp until not long ago. Now I’m beginning to see how how objects should work.

Back from XP Days Benelux, on to XP Days London

November 29th, 2009

Last week I attended the XP Days Benelux. It’s the fourth time I go there, and it’s been a mild shock to realize how often I was there. The first time I went there was because I had the pleasure to meet Pascal, who was kind enough to help us run the first Essap. It was a big bet for us to do something as big as a Summer School. Luckily, we had help from Pascal and Francesco.

I was just starting to get my bearings in the Agile world back in 2006. Pascal came to Essap and taught us about estimating, planning and executing a plan, with his and Vera‘s ingenious XP Game. And that was not his only contribution… he was, like, a *real agilist* who had a long tradition and a strong community behind. In 2006 we had just started the Milano XP User Group. It was great to meet someone who had a much bigger experience of working with agility.

There is this peculiar thing about the Benelux Agile community. They are real cosmopolitans. They speak English easily, since their countries speak many different languages and they are used to speak English even among themselves. They really *live* the agile values. I mean, if you want to succeed with agile, you better start living your life with the agile values. The organization of the XP Days reflects this.

For instance, it’s not like in old-school conferences where you send you session proposal and then it’s either rejected or approved. At Xp Days you are supposed to send a first draft, then you receive feedback on your proposal, then you improve your proposal with the feedback. This reflects the value of feedback, and the principle that good things are not done in one shot, but iteratively.

In the room where the plenary sessions were held, the core values of this group of agilists were exposed prominently. See in the first picture here, they are those sheets of papers high up to the right of the projector screen. They are unreadable in the photo, but they were well readable if you were in the room. They were

  • courage,
  • openness,
  • focus,
  • respect,
  • committment.

One other example of agile values in action is the many forms of feedback that are encouraged. For every session, you’re encouraged to write your feedback on a small card for that session. At the closing of each day, people who attended each sessions are asked to tell everybody what they learned (in 60 seconds! The timebox is another agile principle.) When you leave the conference you are asked to write a feedback sheet for the conference in general. “Give the gift of feedback”, is what the organizers say.

So my trail in the world of agile has been very much influenced from the beginning by the Benelux agilists. It’s a great trail to be in :-) Over the years I got to meet many more friends there, and it’s great to meet new ones every year. Some, like Yves, Marc and Willem, continued Pascal’s tradition and came to help us organize Essap in 2008.

My and Antonio‘s contribution to the XP Days this year was the session on the Birthday Greetings Kata. Thanks to all participants! We learned a lot of valuable feedback on how to improve this session, and I’m ready for the next stop, which will be in London.

Impara il TDD con il team Orione di Sourcesense

November 25th, 2009

Summary: my company, Sourcesense, offers a one-day course on TDD

Se ti interessa imparare il Test-Driven Development con sviluppatori che lo usano tutti i giorni, in un corso di una giornata, mani-sulla-tastiera, iscriviti al nostro corso. E’ il primo corso pubblico che facciamo; prima d’ora abbiamo sempre fatto corsi in casa dai nostri clienti. Speriamo di fare un pienone!

Quando: 27 gennaio 2010

Dove: a Sesto San Giovanni, via Venezia 23

Quanto costa: € 300,00 più iva; ma puoi spendere solo € 200,00 più iva se completi il pagamento entro il 27 dicembre.

Chi lo insegna: io (Matteo Vaccari) più un altro sviluppatore del team Orione di Sourcesense.

Tutte le informazioni, e il link per l’iscrizione, sul sito di Sourcesense:

http://www.sourcesense.com/it/agile/training/

Back from iad09

November 21st, 2009

So this was the sixth Italian Agile Day!

It’s been great to meet old friends, and make new acquaintances too.

Appreciations:

  • Peter Stevens, for sharing insightful tips and tricks on how to succeed with fixed price projects.
  • Alberto Provaglio, for the interesting insights on system dynamics.
  • Jacopo Romei, for the courage of sharing his not-so-successful experiences.
  • Gabriele Lana and Simone Genini, for helping me with the coaching workshop.
  • Simone Casciaroli, for convincing me to do a workshop instead of my old boring presentation, and working hard at preparing it. We’ll do another one some other time!
  • All the people that attended the workshop. It’s been overwhelming. Thanks for coming and contributing!
  • Alberto Brandolini, for teaching me that there’s a lot more to DDD than I thought.
  • Indrit Selimi, for taking the picture at the workshop.
  • Marco Gulino, for the self-sacrifice of giving up coming to the IAD, getting up at 4 in the morning *and* having to deal with a confrontational situation.
  • My Sourcesense collegues, for contributing many sessions again in 2009.
  • Marco Abis and the Bologna XP User Group, for all the work *and* for being great people to hang around with.

Thank you all! Let’s do even better next year!

How I setup confluence on Debian

November 13th, 2009

Pardon the dreariness of this post. I did this twice in a couple of days. I thought I might as well write down what I did, to save me time next time. Let me state first that this is an opinionated install :-) I don’t like bloated Tomcat installations with many applications on them. I prefer to have each application in its own place with its own server. So I will use the Confluence standalone package, which is integrated with Tomcat. When I will install Jira on the same box, I will download another standalone package.

Preparations

Connect to your server. Execute

  apt-get install apache2 ca-certificates
  apt-get install mysql-server

Create file /etc/mysql/conf.d/confluence.cnf with the following contents

  [mysqld]
  default-storage-engine=innodb
  lower_case_table_names=1

Then restart mysql with

  /etc/init.d/mysql restart

Edit /etc/apt/sources.list, appending the string ” non-free” to every line.
Then execute

  apt-get install sun-java6-jdk

Install Confluence

Download the Confluence standalone package for gnu/linux (not the one for
evaluation)

  wget http://www.atlassian.com/software/confluence/downloads/binary/confluence-3.0.2-std.tar.gz

Create the directory /opt/confluence. In that directory, expand the confluence
archive. Then create a symlink from /opt/confluence/confluence-app to the
actual directory that contains the confluence application

  ln -s /opt/confluence/confluence-3.0.2-std /opt/confluence/confluence-app

Then create directory /opt/confluence/confluence-data.

Edit the file confluence/WEB-INF/classes/confluence-init.properties within the
confluence distribution, and add the line

  confluence.home=/opt/confluence/confluence-data

Edit the file conf/server.xml and change the following:

  • Change 8080 and 8000 to something other than these, which are the default
    Tomcat ports and might be useful some other day. I use 8180 and 8100.
  • In the first Connector element, add the attribute address=”127.0.0.1″. This
    will make Tomcat bind to localhost only. I don’t want to make Tomcat
    directly accessible from the outside.
  • In the first Context element, change path=”” to path=”/confluence”.
    Otherwise I can’t get the reverse proxy from Apache to work.

Create a file /opt/confluence/startup-confluence.sh with the following contents:

  export JAVA_HOME=/usr/lib/jvm/java-6-sun
  su -m www-data -c /opt/confluence/confluence-app/bin/startup.sh

Change ownership of the whole /opt/confluence thing to www-data

  chown -R www-data.www-data /opt/confluence
  chmod +x /opt/confluence/startup-confluence.sh

Download the mysql driver

  wget http://dev.mysql.com/get/Downloads/Connector-J/mysql-connector-java-5.1.10.tar.gz/from/http://mirror.switch.ch/ftp/mirror/mysql/

Extract the archive, and copy the jar it contains to
/opt/confluence/confluence-app/lib

Create the database and the user for confluence

  # mysql -uroot -p
  mysql> create database confluence;
  mysql> grant all on confluence.* to confluence@127.0.0.1 identified by 'secret';

Now if you didn’t do this when first connecting, exit from ssh and reconnect to create a tunnel, to be able to browse Confluence from localhost on the server.

  ssh -L1234:localhost:8180 root@myserver

Execute /opt/confluence/startup-confluence.sh, then point the browser to

  http://localhost:1234/confluence

You should see the confluence configuration screen. Now just follow the wizard. Remember to choose “Custom installation”, “External database”, and “Mysql”.

When Confluence is up and running, we still have some things to do. Let’s configure Apache to serve Confluence in http://myserver/confluence . Edit /etc/apache2/site-available/default and add the following lines:

  <Location /confluence>
  	Order allow,deny
    Allow from all
  
  ProxyPass /confluence http://127.0.0.1:8180/confluence
  ProxyPassReverse /confluence http://127.0.0.1:8180/confluence

Then execute

  a2enmod proxy proxy_http
  /etc/init.d/apache2 reload

You should now be able to see Confluence at http://myserver/confluence

Now we must make sure that Confluence will start at system boot. Just call
/opt/confluence/startup-confluence.sh in /etc/rc.local. It’s simple, and it
works.

You may stop here unless you want to serve Confluence via ssl. In that case… let’s carry on. Execute

  a2ensite default-ssl
  a2enmod ssl
  /etc/init.d/apache2 reload

You should be able to access https://myserver/ now. Now move the proxypass configuration from sites-available/default to sites-available/default-ssl.

My impressions of the Emergent Design Workshop

October 29th, 2009

OK, so I did participate to the Emergent Design Workshop by Francesco Cirillo. This is the second time I attend a workshop with Francesco. The other one was about coaching and agile process management. This one is about the technicalities of making the Agile thing work for real in the code. It’s never easy to work with Francesco; if you do attend this workshop, be prepared to challenge everything you know.

In my particular case, I knew I didn’t know object-oriented design well. OK, I did read about the design patterns, and I did read some of Robert Martin’s writings, but never got really into this stuff. Yet somehow, I thought I could get away with not knowing this stuff deeply. This workshop changed this; now I realize more fully the amount of stuff I didn’t know, and why it’s very important for me to learn this.

And,… even more importantly… I learned to see why a certain kind of semi-procedural code disguised as object-oriented is not satisfactory; and it’s not fun. I gained a new set of eyes and a higher level of criticism for code. What I learned resonates with what I wrote earlier about “code that speaks.” It turns out I was on the right track there; the goal is to have code like Lego bricks; objects that you can combine together to obtain the desired results. Code that can withstand changes in specification, without becoming more complex. Above all, the thing I’m grateful to Francesco for is, to get back to the fun of working with software like objects.

A database for every developer

October 17th, 2009

A database for every developer. No, *two* databases for every developer.

This is a fundamental for project organization that many projects get wrong. Every development workstation should be equipped with a full local development environment, with a local copy of the database software, and a one-command way to recreate the databases from scratch.

Why *two* databases? Well, one is for exploratory testing of the application we’re building. The other one is for automatic unit tests.

Why *local*? Because whenever the database server is not local, it becomes difficult to add a new workstation, it’s impossible to work when you’re not in the office, and you must depend on other people to fix your database problems.

The software that we write should *not depend* on the data sources that live outside our development workstation. To this end it’s a good start to have simple scripts that allow you to rebuild your database, so that you know you can experiment, change everything, make mistakes, and you’re still able to get back to a known working situation in a flash.

Why it’s important that I can rebuild the databases with *one command*? Because if it takes more than one command, it’s too complicated and I’m likely to make mistakes. Because it’s too easy to fall in the trap of not knowing exactly which steps are needed to set up a new database instance. If you have a single script that does the job, that script is also a living, always up-to-date document that describes how to recreate the database from scratch.

The benefits are not just in development; when the time comes to release our software in production, you can see how helpful it is to have a script that is able to set up the database with no effort. In fact, all database maintenance operations should be automated. It’s one of the principles explained so well in The Pragmatic Programmer, a very good book.

For example, this is a typical script that I use in my non-Rails projects:

#!/bin/bash

src=src/main/sql
dbname=myapp_development
dbname_test=myapp_test
dbuser=myapp_user
dbpassword=myapp_password

# Usually no changes needed beyond this point

if [ ! -d "$src" ]; then
  echo "Run this script from the main directory"
  exit 1
fi
read -s -p "mysql root password? (type return for no password) " MYSQL_ROOT_PASSWORD

if [ "$MYSQL_ROOT_PASSWORD" != "" ]; then
    MYSQL_ROOT_PASSWORD=-p$MYSQL_ROOT_PASSWORD
fi

mysqladmin -uroot $MYSQL_ROOT_PASSWORD drop $dbname
mysqladmin -uroot $MYSQL_ROOT_PASSWORD --force drop $dbname_test
mysqladmin -uroot $MYSQL_ROOT_PASSWORD create $dbname
mysqladmin -uroot $MYSQL_ROOT_PASSWORD create $dbname_test
echo "$dbname created"
echo "grant all on $dbname.* to '$dbuser'@localhost identified by '$dbpassword';" \
     | mysql -uroot $MYSQL_ROOT_PASSWORD $dbname
echo "grant all on $dbname_test.* to '$dbuser'@localhost identified by '$dbpassword';" \
     | mysql -uroot $MYSQL_ROOT_PASSWORD $dbname_test
echo "$dbuser authorized"
cat $src/???_*.sql | mysql -u$dbuser -p$dbpassword $dbname 
cat $src/???_*.sql | mysql -u$dbuser -p$dbpassword $dbname_test 
echo "schema loaded"

This handy little script will create the development and test databases, and load all sql scripts. I like to name sql scripts like 001_create_foobar_table.sql and 002_add_frobniz_column_to_foobar.sql, so that they can be loaded in sequence. It’s a simple way to develop the database schema incrementally. I may talk about it in another post.

Two floats are never equal

October 17th, 2009

While we are on the topic of floating point fundamentals, there is another thing to remember: it is always a mistake to compare two floating-point numbers for equality.

It all boils down to the simple fact that floating-point arithmetic is not exact. It is meant for approximate calculations with engineering or scientific measurements, which are inexact to begin with. In fact, floating-point arithmetics results are almost never equal to the “true” value you would get by using exact real arithmetic.

Therefore, wherever you see something like x == 0.0, you can be fairly sure that it’s a mistake. Whatever computation produces the value of x, it’s unlikely to ever produce exactly 0.0.

The proper way to compare floating points is equality within some tolerance. For instance:

boolean approximatelyEqual(double a, double b, double epsilon) {
  return Math.abs(a - b) <= epsilon;
}

The above code works for most applications. It does not take into account the case that the inputs are NaN or infinities. I’m no expert of floating point arithmetic, so I will not give advice about this. For reference I copy here the following code from JUnit:

static public void assertEquals(String message, double expected,
    double actual, double delta) {
  if (Double.compare(expected, actual) == 0)
    return;
  if (!(Math.abs(expected - actual) <= delta))
    failNotEquals(message, new Double(expected), new Double(actual));
}  

The purpose of the compare call is to have the test pass when the two numbers are both NaN.

Money is not a float

October 17th, 2009

One suggestion I took to heart is that in order to be great, you need to work on fundamentals. It’s no good to be up to date with the latest and greatest, be they Agile techniques or new technologies, if you’re weak on fundamentals.

So I’m starting a collection of fundamentals, that is certainly not going to be comprehensive. Rather, it’s a random collection of things that I think are fundamental, yet many experienced developers get wrong.

Let us start with a surprising discovery: did you know that the number 1/10 cannot be represented in a finite way in base 2? Yep, it turns out that in base 2 the number 1/10 is periodical, much like the number 1/3 has no finite decimal representation in base 10. But what is the implication for us?

The implication comes when we make the mistake of representing a money in a floating-point number. Suppose you encode the amount of “ten cents” in the floating-point number 0.10. And now look at this program, and guess what happens when it runs.

  public class MoneyIsNotAFloat {
    public static void main(String[] args) {
      double tenCents = 0.1;
      double sum = 0.0;
      for (int i=0; i<10; i++) { 
        sum += tenCents;
        System.out.println("0.1 * " + (i+1) + " = " + sum);
      }
    }
  }  

(Hint: 1.0 times 10 equals… 0.99999999999999).

And this is not a Java problem. The same happens with any language, for it’s a matter of floating point arithmetic.

The simple fact is that floating-point arithmetic is not exact, therefore it should not be used for representing money!

What to use then? One simple solution is to use a plain int to represent an amount of cents. Integer arithmetic is exact. A 32-bit int should be enough for most applications. If you’re worried about overflow, use a BigDecimal type. Java has one, and most modern languages do too. (Just a note: if you use a Java BigDecimal, remember that you should not compare them with “equals”, you must use “compare”. Go figure.)

Throwing the baby with the bathwater

October 14th, 2009

I was talking with a customer recently, who complimented us because the software we are producing is stable. He also said that one important goal of this project is to write something that is easy to mantain and extend.

At the same time, he was adamant that we should stop doing things like pair programming, and estimating user stories with the whole team, because these things “slow us down”.

Ahem. If you want good quality code, you better apply these practices, for they are meant for producing quality code. :-) Of course we will continue doing all these practices, for we think it’s best for the project.