Archive for the 'Agile' Category

L’arte perduta di pensare ad oggetti

Saturday, April 28th, 2012

I published the slides from my Object Thinking presentation at Codemotion 2012… in Italian.

A personal update

Thursday, April 5th, 2012

One year ago I wrote What have I been up to?. I told the story of how we fixed a flaky application. A few months later, the same customer contracted me and our team to rewrite the whole application from scratch.

That was a big job! It lasted from early July to the end of March. All the members of the Orione Team in Milano were involved, with a contractor from Cagliari and one from Rome. About 8-9 people from XPeppers and 3 more from the customer. I’m happy to report that we delivered the system, essentially on time and on budget and with a happy customer.

How did we do that? I’m thinking about writing a longer article on that. There are many ingredients. Some of the most important are heavy customer involvement, incremental delivery, test-driven development and automated acceptance tests. But really, it’s about people and focus.

And now… a change of horizons. I’ve been working in Sourcesense first and XPeppers later for 5 years. The Orione team was formed about 4.5 years ago and I’ve been with them for all this time. I don’t think I can give them anything more. I feel it’s time for a change. I’m going back to being an independent coach and consultant. Wish me luck!

And contact me if you think I can help you.

Formalism versus Object Thinking

Sunday, January 22nd, 2012

The book Object Thinking makes a very good explanation of the divide between two different, antagonistic modes of thinking. The formalist tradition (in software) values logic and mathematics as design tools. A formalist thinks that documents have objective, intrinsic meaning. Think “design documents”; think “specifications”.

The empirical tradition (the book calls it hermeneutics, but I will stick to this less formal name :-) values experimentation. Empiricists hold that the meaning of a document is a shared, temporary convention between the author and the readers. Think “user story”; think CRC cards; think “quick design session on the whiteboard.”

The empiriricists brought us Lisp; the formalists brought us Haskell. The formalists brought us Algol, Pascal, Ada. The empiricists brought us C, Perl, Smalltalk.

Empiricists like to explain things with anthropomorphism: “this object knows this and wants to talk to that other object…” The formalists detest anthropomorphism; see these quotes from Dijkstra.

As a former minor student of the best formalist tradition there is, and a current student of the Object Thinking tradition, I think I’m qualified to comment. Please don’t take my notes as meaning that the formalist tradition sucks; I certainly don’t think this. I’m interested in highlighting differences. I think a good developer should learn from both schools.

Formalists aim to bring clarity of thought by leveraging mathematical thinking.

Object thinking aims to bring clarity of thought by leveraging spatial reasoning, metaphor, intuition, and other modes of thinking.

It is well known that mathematical thinking is powerful. It’s also more difficult to learn and use. One example that was a favourite of Dijkstra is the problem of covering a chessboard with dominoes when the opposite corners of the chessboards were removed. If we try to prove that it’s impossible by “trying” to do it or simulating it, we’d quickly get bogged down. On the other hand, there’s a very simple and nice proof that shows that it’s impossible. Once you get that idea, you have power :-)

An even more striking example is in this note from Dijkstra on the proof method called “pigeonhole principle”. Dijkstra finds that the name “pigeon-hole principle” is unfortunate, as is the idea to imagine “holes” and a process of filling them with “pigeons” until you find that some pigeon has no hole. The process is vivid and easy to understand; yet it is limiting. Dijkstra shows in this note how to define the principle in a more simple and powerful way:

For a non-empty, finite bag of numbers, the maximum value is at least the average value.

This formulation is simple (but not easy!) Armed with this formulation, Dijkstra explains how he used this principle to solve on the spot a combinatorial problem about Totocalcio that a collegue of his could not solve with pen and paper. He also explains how he used it to solve a generalization of the problem, which would not be easy to prove with the “object-oriented” version of the principle.

I think this note presents the contrast between formalism and empiricism vividly. If you put in the effort to internalize the formal tool, that which was difficult becomes easy, and you can solve a whole new level of problems.

On the other hand, the formalists do now always win :-) Formalists reject the idea of making tests the cornerstone of software development. In my opinion they are squarely wrong; examples are the primary tools to do software development, and you can’t even understand if a specification is correct until you *test* it with examples.

The one thing that boths camps have in common is that they are both minority arts. Real OOP is almost as rare as Dijkstra-style program derivation. The common industrial practice is whateverism :-)

Greed and Simple Design

Saturday, January 21st, 2012

Some people like Carlo say that the famous Four Elements of Simple Design by Kent Beck are an oversimplification. Perhaps it’s true, but still I find that they are a very useful compass. Consider again:

A design is simple when

  1. Runs all the tests.
  2. Contains no duplication
  3. Expresses all the ideas you want to express.
  4. Minimizes classes and methods

in this order.

Rule 2 is important, as it pushes us to invent abstractions that capture recurring patterns. But rule 3 is also imporant, as it pushes us to invent abstractions that correspond to the ideas that we want to express.

The other day I saw this post by Luca about a fun kata: implementing the scoring rules for a dice game called “Greed”. This exercise is part of the Ruby Koans, but its use as a programming exercise dates at least from the OOPSLA ’89 conference, when Tom Love proposed a contest to show how a program could be written in different ways and in different languages.

A little research shows many solutions for this problem. As this problem is presented in the context of a Ruby programming exercise, people usually tries clever tricks that exploit peculiar Ruby idioms. For instance:

def score(dice)
  (1..6).collect do |roll|
    roll_count = dice.count(roll)
    case roll
      when 1 : 1000 * (roll_count / 3) + 100 * (roll_count % 3)
      when 5 : 500 * (roll_count / 3) + 50 * (roll_count % 3)
      else 100 * roll * (roll_count / 3)
    end
  end.reduce(0) {|sum, n| sum + n}
end

http://stackoverflow.com/a/6742129/164802

There’s a place for this sort of exercises, but it’s not the sort of programming that I would like my collegues to practice! If we apply the rule 3, I expect to see in the source cose some mention of the *rules* of the game. I expect that there’s a programming element that corresponds to the rule that “three ones are worth 1000 points”, etc. Really, it does not take all that much more effort, and I assert that it’s more fun to code expressively!

This is my solution:

class Array
  def occurrences_of(match)
    self.select{ |number| match == number }.size
  end

  def delete_one(match)
    for i in (0..size)
      if match == self[i]
        self.delete_at(i)
        return
      end
    end
  end
end

def single_die_rule(match, score, dice)
  dice.occurrences_of(match) * score
end

def triple_rule(match, score, dice)
  return 0 if dice.occurrences_of(match) < 3
  3.times { dice.delete_one match }
  score
end

def score(dice)
  triple_rule(1, 1000, dice) +
  triple_rule(2, 200, dice) +
  triple_rule(3, 300, dice) +
  triple_rule(4, 400, dice) +
  triple_rule(5, 500, dice) +
  triple_rule(6, 600, dice) +
  single_die_rule(1, 100, dice) +
  single_die_rule(5, 50, dice)
end

There's some more duplication that could be removed (the five similar rules could be expressed as a single rule) and the names could be improved, but I think this is the way to go. Make your code look like a model of the problem!

Classes without a face

Thursday, January 5th, 2012

I have a feeling for classes that should not be there. When I saw this cartoon about “how an object-oriented programmer sees the world”, I was struck by the fact that all the names of objects were wrong! These are the names that would be chosen by a poor OO programmer. A good programmer would choose “Door” instead of “IndoorSessionInitializer”. But then the cartoon would not be funny :-)

A similar thing happens in the code base of our current project. Sometimes I see a class that strikes me as odd. Perhaps it has a name that does not communicate; more often it is simply a class that should not exist.

(more…)

On the folly of representing a first name with a String

Wednesday, January 4th, 2012

Object-Oriented decomposition is supposed to be different

When I read Object Thinking, I was intrigued by this quote by Grady Booch:

Let there be no doubt that object-oriented design is fundamentally different from traditional structured design approaches: it requires a different way of thinking about decomposition, and it produces software architectures that are largely outside the realm of the structured design culture.

So it seems that OOD decomposes a problem in a way that is essentially different from what you would arrive at with other design methods. I was intrigued: I wonder what are these different, elusive ways of decomposing things.

(more…)

How I remembered Object Thinking

Tuesday, December 13th, 2011

A revelation inside another revelation

I’ve been reading Object Thinking. It’s an unusual book, in that it talks mainly about the philosophy of OOP rather than technicalities like Dependency Injection. There are many gems in this book. One of the main things I got out of it is that Object Thinking is a revolutionary break with respect to established software engineering practice. Another thing is that the view of objects as Abstract Data Type is basically the establishment’s way to dilute and incorporate the grand new thing and make it look like a variation of the old thing.

Again, one thing that I changed my mind about is to take seriously the idea of modeling the domain. I mean, the old OOP books all said things like “take the description of your problem, underline the nouns in the description, and these are your candidate objects”. I used to think that this is a silly exercise! And I’m not alone. I heard many times the phrase: “objects are good, but not for the domain.”

In the last few years since I attended Francesco Cirillo‘s workshop on Emergent Design in 2009, I have been studying OOP. I learned about the GOF patterns. I learned about the SOLID principles. I learned how to write testable code. I learned how to do TDD for the infrastructure. I learned how to test-drive Chicago style and London style. I invented exercises for teaching the Hexagonal Architecture and the Open/Closed principle.

All of these things are good, and very useful to learn. Yet they are not the heart of Object Thinking. All these things are based on the shape of programs. For instance: I write a Long Method (bad), then I Extract Method a few times, then I Extract Class, and voila, I have done an Object Decomposition. But this is a mechanical exercise, that takes into account mostly the shape of the methods and not their meaning. If I’m lucky, my decomposed class will turn out to be something significant in terms of the domain. If I’m less lucky, I’ll have extracted some boring bit of infrastructure, like the IndoorSessionInitializer.

What I was missing on is decomposition based on Object Thinking. Decomposition based on writing the Domain Model. Thinking about the Domain Model as a simulation of the problem domain, as if the domain objects were little computer people sending messages to each other. Ding!

The incredible compressing brain of Kent Beck

Kent Beck’s brain contains probably the most efficient compression algorithm ever. Kent is able to compress in a couple of lines what others do take volumes to explain. Take Kent Beck’s four rules of simple design:

The right design for the software at any given time is the one that

  1. Runs all the tests.
  2. Has no duplicated logic. [...]
  3. States every intention important to the programmers.
  4. Has the fewest possible classes and methods.

Kent Beck, Extreme Programming Explained

Consider item 2. It basically means: look at the shape of your code, and wherever you see the same information encoded twice, find a way to generalize or abstract so that it is encoded just once. In a few words he is implying the contents of the Refactoring book, of SOLID, and all approaches that look at the shape of code for guidance on how to improve its design.

Then consider item 3. It basically means: look at the meaning of your code, and make sure that the meaning is apparent in the names and the structure. This implies that you should make your code a model of the problem domain.

(Items 1 and 4 are just safeguards against making refactorings that break functionality, or going wild with OOD ideas and making the design worse by making it overcomplicated.)

Am I crazy or…

As an aside, I find myself buying lots of out-of-print books. It seems the things that I find most interesting are not what the world of programming in general finds interesting. Even my beloved Extreme Programming seems out of fashion lately. Nobody wrote about XP since Kent Beck wrote the last of the Coloured Books in 2004. But I still think that OOP and evolutionary design and XP are crucial things to learn. Yet everyone wants to learn about the latest technology fad. The latest technology fad I’m getting into is SmallTalk, and it’s something that stopped being developed around 1980. This means that either I’m crazy, or everyone else is.

In conclusion

When I was young I really believed in objects. My first falling in love with objects was over the cover of an issue of Byte Magazine dedicated to OOP.

I remember now that in nineteen-eighty-something I wrote an object-oriented program for an AI assignment. It was a program for symbolic integration that took inspiration from some crazy idea by Douglas Hofstadter. It was a community of software agents, each one specialized in some symbolic solution technique, that worked at the problem without any central coordination. It was written in some variant of Lisp that had no Object-Oriented extensions; so I wrote my own OOP system on top of Lisp. It was fun :-) and it was not difficult at all to do.

Then, in the nineties, I got enamored of formalist thinking; not the brand that did UML, but the brand that did Formal Methods. By then, OOP seemed such a scruffy and ineffective thing to me. But by that time, when I thought of OOP I was thinking of C++; I was thinking of large, unwieldy frameworks based on very long inheritance chains. Small wonder that I didn’t like it.

In the eighties, when I thought of objects, I thought of letting artificial intelligence emerge out of the collaboration of many simple autonomous agents. What a silly and romantic idea! Yet this idea is at the heart of OOP as was originally imagined by Alan Kay and the others.

This is probably a good place to comment on the difference between what we thought of as OOP-style and the superficial encapsulation called “abstract data types” that was just starting to be investigated in academic circles. … To put it mildly, we were quite amazed at this, since to us, what Simula had whispered was something much stronger than simply reimplementing a weak and ad hoc idea. What I got from Simula was that you could now replace bindings and assignment with goals. … the objects should be presented as sites of higher level behaviors more appropriate for use as dynamic components.
Alan Kay, The Early History of SmallTalk

Can you tell a program’s paradigm by looking at it?

Friday, December 2nd, 2011

I attended an interesting talk the other night at the Milano XP User Group. Uberto Barbini was sharing his thoughts and experiences over using Object-Oriented and Functional Programming together.

Uberto showed us some production code, that he thought was a neat application of FP in a OOP context. The funny thing is that to my eyes it seemed that was neither FP nor OOP; it seemed procedural to me. Uberto argued that it was functional and not procedural, because there was no global state involved; it was a self-contained transformation of data.

That got me thinking. Do I like that definition of “procedural”? I don’t think so. I think that you can tell procedural code when the thinking that goes behind that code is something like:

First I do this.
Then I do that.
If Zot Then I do Blah.
Else I do Blorgh.

When you think of a recipe, or a sequence of steps, then for me it’s procedural. Essentially “procedural” is to model the solution as a process.

On the other hand, functional programming, in my humble opinion, is about mathematical models. You model the problem and the solution with functions, sets, maps, trees and all those abstract and precise mathematical concepts.

What about Object-Oriented Programming then? My thinking about OOP has been in the past that OOP is a natural consequence of writing modular programs. An object is a hidden data structure with a visible collection of operations. Then I understood that this is just one way to look at OOP, and probably it’s the wrong one :-) This point of view is heavily influenced by mathematics (the idea that the operation on an object define a sort of algebra) and by procedural thinking (the idea that programs = data + procedures, just like the title of an old favourite textbook.)

I learned from reading GOOS that OOP is mostly about the messages. There are some intesting and tantalizing quotes from Alan Kay about that. Even more recently I read Object Thinking, and I came to understand that OOP is about modeling the problem with a community of autonomous agents. The way to make OOP shine is to build a simulation of the problem. And I remembered the thrill I once had when I first read about OOP. About imagining these little software robots going about their business inside my programs. An object that can “think” for itself and has a behaviour. Now *that* is what OOP is about!

Back to the original theme, now was the code that Uberto showed to us OOP, FP or procedural? It turns out that it depends! You can’t say by just looking at the code. It depends on the thinking that goes behind the code. Uberto thinks functionally when he thinks about this code. That makes it functional code for him.

Suppose that years later I inherit his codebase. If I have no access to Uberto, it’s pretty difficult for me to reconstruct his mental process. I will probably treat the code as procedural; or maybe OO depending on my way of thinking about this code.

So this is a pretty shocking thought, isn’t it? I thought you could look at a program and tell its paradigm, functional or procedural or OO, just by looking at the code. But I now think you can’t; the three paradigms are not different ways to code; they are different ways of thinking! Procedural thinking is about recipes: follow these steps. Functional thinking is about math: functions and sets. Object-Oriented is about simulations: many independent agents. And this explains why it’s difficult to do OO well. You have to learn a different way of thinking. This is something that I heard Francesco Cirillo say many times. Now I understand it a bit better :-)

Sulla sessione “Is Software Evolution Really Effective” di Francesco Cirillo

Sunday, November 20th, 2011

Francesco Cirillo ha presentato questa sessione sabato scorso all’Italian Agile Day. Ho letto su http://joind.in/talk/view/4508 diversi commenti che non mi tornavano; allora ho scritto questa mia personale esegesi, perché penso che il messaggio di FC sia molto importante e mi secca vedere che viene spesso frainteso.

Quindi vi do la mia personale interpretazione, senza pretendere di parlare per Francesco. Fatto: se proviamo a leggere un qualsiasi libro in tema Metodi Agili, si dà per assodato che usando i Metodi Agili si diventa più bravi.

La realtà dei fatti, che ho personalmente riscontrato nelle mie esperienze, è che spesso questo non è vero. Diversi team agili di mia conoscenza hanno fallito progetti, o comunque hanno conseguito una fama di essere troppo cari. Fama immeritata? Che importa! Se il risultato finale è che il cliente non ti sceglie più hai fallito. E poi è facile che la fama non sia poi così immeritata.

E’ vero che molte pratiche agili producono un beneficio immediato; quel problema che facevi fatica a debuggare si risolve brillantemente con gli unit test, la comunicazione con il cliente migliora se andiamo a chiedergli che cosa pensa veramente, ecc. ecc. Ma c’è un grosso MA. Ci vuole molta, molta fatica per rendere questi benefici permanenti. E’ sulla distanza che si vede la differenza.

Tanti che credono di programmare a oggetti scrivono invece codice procedurale, e la differenza sulla distanza si traduce in codice ingarbugliato. Tanti che si rifiutano di fare big design upfront, non hanno capito che devono fare invece tanto design in maniera continua. E magari non sarebbero in grado di farlo neanche se volessero, il design upfront, né big né small. E poi, ci vuole tanto coraggio per continuare a mantenere un vero contatto con il cliente nel lungo periodo.

Kent Beck ha detto “What does it mean to be agile? My definition is that you accept input from reality and you respond to it”. Allora facciamola per davvero questa follia di accettare l’input dalla realtà! Misuriamo quanto ci costa sviluppare. E’ da qui che si vede se quello che facciamo funziona veramente.

E già che ci siamo, da quanto tempo non andiamo dal cliente a chiedergli “sei contento di come lavoriamo?”

Update 2011/12/11: updated link to HD video

Fixing session management in Tomcat

Wednesday, October 26th, 2011

Sessions are the Achilles heel of every application server.
Michael T. Nygard, Trampled By Your Own Customers (pdf)

Earlier this year I started a big Java development project with my team. I did mostly Rails development for a long time, so when I came back to Web applications in Java I had some expectations. I expected that Tomcat would treat user sessions in much the same way as Rails does; but it doesn’t.

(more…)