18 November 2007

Software design is like a magic trick

There's something I love at least as much as software development: my wife (Hey!! Give me back that keyboard, will ya?. I love you *more* than software development of course,...). So, what was I saying, ah, yes. There's something I love at least as much as software development: magic.

And there are some interesting similarities between the design of a magic trick and the design of software. Let's take, as an example, the last functionality I have designed for specs (but not released yet).

The effect

When you start thinking about a magic trick, you just try to be creative, to focus on what will really make other peoples brain explode. Or, and I find this even more impressive, transport them emotionally in a real magic world. To that respect David Copperfield is an incredible magician, and there are lesser known magicians whose creativity and talent are as amazing: Jay Sankey, Juan Tamariz, Michael Ammar, Jean-Pierre Vallarino, Vito Lupo,... The list is long. All of them have spent hours just thinking: "Why is this magic?", "How can I make it more magic?". They think about the spectator and the effect first and before any consideration of how it could be done.

In my case, it's a lot less romantic,... I just want the developer to be able to:
  • create a table containing data rows
  • have a header of Strings labeling each column
  • have the rows being typechecked so that the type of the elements in a given column is always the same
  • apply a function to each row and have the function parameters being typechecked against the row types
  • have a light syntax allowing the table to look like a table as much as possible (not a list of lists,...)
Something like that:
|"a"|"b"|"c = a + b"|
| 1 | 1 | 2 |
| 1 | 2 | 3 |
| 2 | 2 | 4 | {(a:Int, b: Int, c: Int) => (a + b) must_== c }

How can I use my target language, Scala, to do that? Without loosing the ideal representation above; I want to keep my effect as magical as possible!

The method

Let's say I want to make the Statue of Liberty disappear, this would be baffling, wouldn't it? But even with an incredible budget, will the New-York authorities allow me to place a huge trap below Ellis Island? No? So how did he do it ?!

Here are 3 things I noticed with great magicians:
  • They have an incredible magic culture. They know every obscure method to make a coin disappear or a card change its color, including the name of the inventor, the year of publication and the thousands of tricks using it
  • They are always open to new stuff. Anything. Okito was just playing with a pill box when he invented the "Okito box". Michael Ammar actually listened to every single "great new idea" we had at our local magic club!
  • They can invest an incredible amount of time and energy to preserve the magic of the effect: exercise for hours for a single "invisible" move, research chemical catalogs, learn the order of 6 decks of cards by heart (you can find a Hollywood version of that in "The Prestige")
That's what I tried to do with the DataTable feature: use all the Scala wizardry I knew of, be opened to new ideas and invest the maximum of energy so the feature is as simple as can be for the developer.

Here was my first attempt:
("a", "b", "c = a + b") |
( 1 , 1 , 2 ) |
( 1 , 2 , 3 ) |
( 2 , 2 , 4 ) | { t => val (a, b, c) = t
(a + b) must_== c }
In that version, I used the following features:
  • Tuples have a litteral notation ( , , , )
  • You can create new operators, like | on tuples using implicit definitions
  • The definition of the | operator can make sure that I will always add Tuples having the same types for their elements
  • A tuple can be deconstructed as 3 values using "val"
This was mostly what I wanted but not quite, so I searched again and eventually got to this:
"a"| "b"| "c = a + b" |
1 ! 1 ! 2 |
1 ! 2 ! 3 |
2 ! 2 ! 4 | {(a: Int, b: Int, c: Int) => c must_== calc.add(a, b) }

Yes, almost my original intention! The most notable difference is the use of ! instead of | as a separator for the cells in the data rows. This is because operators beginning with | have a lower precedence over those beginning with ! in the Scala specification (6.12.3). So if I use |, I will have difficulties delimiting rows. I learned a new trick!

Frankly, it's not as good as the ideal version but it's ok if you read | as a delimiter for the table boundaries (including the header) and ! for the actual data. Sometimes in a magic trick you change a line, bend the story a little and it is still a pretty good trick.

And what about the rest of the magic? The first version only allowed to define a function taking a tuple as a parameter. Well, I worked a lot behind the scene to obtain that new version:
  • The DataTable class takes 20 type parameters. 20 is a limitation but should act like a warning anyway. Why would you seriously need more that 20 columns in your table? Can't it be refactored?
  • There are 20 DataRow classes, having from 1 to 20 type parameters. Each DataRow object can only be joined with a similar DataRow object using the | operator
  • The DataTable class has 20 methods to apply functions having 1 to 20 parameters. Applying each row to the function parameters is done in each of these methods. A nice side-effect of that is that I can use a function with less parameters than the row size
  • All that code is produced with a Ruby script to ease the work of the magician developer
You can have a look at the current code, which may actually be clearer than the points above.

Design and magic

This comparison with Magic can add more credits to the "Design is a craft" theory. I'm pretty sure that you could do the same kind of comparison with some craft you're fond of. So what's next: "Software design is like Origami", "Software design is like Wood carving"?

PS: for the Statue of Liberty trick, just Google for the explanation, you should get a glimpse of the kind of creativity those darn magicians can have!

08 October 2007

Scala to heaven, second step: anatomy of a scala script

This objective of this post is to contrast some java and scala code aimed at accomplishing the same scripting task: cleaning-up my garbage system.

I want to be able to count old items in a database and archive them (through my system API). Of course, if anything goes wrong, I want to be able to restore them.

I'll show what is the approach I took using java then the corresponding scala-way of doing things.

The Java way: connect to the server

First things first: get a server connection. Typically, this is how I do it in java:

DataServerConnection connection = ConnectionUtil.connect("me", "my password", "my environment file");
// I do my stuff here

Of course, when my job is over, I have to close the connection:


But in this situation, and in plenty others like writing to an OutputStream, I may just forget to close your resource. So, here's:

The Scala way: connect to the server

There is, among a gazillion things, a very useful feature in Scala: the possibility to have parameters of a method evaluated lazily. This means that the parameter you pass to a method will be evaluated only when the method body requires it, and not as the method is called, as it is the case in Java.

This way, I can write a better connect method:
def connect(user: String, password: String, env: String, actions: => Any) = {
val connection = ConnectionUtil.connect(user, password, env)
And use it like that:

connect("me", "my password", "my env. file", actions())

The actions are only performed once the connection is open, and the connection is closed without having to think about it. And for even more readability, I can even use the following syntax:

def connect(user: String, password: String, env: String)(actions: => Any) = {
val connection = ConnectionUtil.connect(user, password, env)
connect("me", "my password", "my env. file"){

Nice Ruby/blocks feel, isn't it? [for a better implementation of this pattern with try/catch and all, please check the Loan Pattern].

The Java way: processing stuff

For this script, the overall process is the same:
  • get some "Market data items" for some "type" and "currency"
  • count/archive/restore the oldest ones
The usual way to do that in Java is to nest some for loops and do the job inside the most inner loop:
for (String type : types) {
for (String currency : currencies(type)) {
for (String name : getMarketDataItemNames(type, currency)) {
final int id = market().getMarketDataItemId(type, currency, name);
doAction(action, type, id);
This buries deep inside the "selection" logic, the "action" logic. One alternative would be to construct first the list of elements to process, then process them. But in my case, this would mean dragging a very big chunk of the database in memory.

The Scala way: processing stuff

Scala offers the possibility to cleanly separate the selection logic from the action logic:

// select items
def items = for (itemType <- market.getMarketDataItemTypes.toStream;
currency <- currencies(itemType);
name <- market.getMarketDataItemNames(itemType, currency);
itemId = getItemId(itemType, currency, name))
yield (itemType, itemId, name)

// archive items
def archive(items: Iterable[Item]) =
for ((itemType, itemId, itemName) <- items)
archive(itemType, itemId, itemName)


The for/yield construct returns ("yields") a list composed of "items" (type, id, name), ready to be processed by the archive function. The interesting thing is that this list doesn't have to be build in memory at once. It is a Stream, i.e. a list whose elements are being fetched as they are needed.

The Java way: aggregating results

The last step in my script is to display the current number of processed elements as well as their total number. I did it very simply with Java:
final int totalProcessed = 0;
for (String type : types) {
for (String currency : currencies(type)) {
for (String name : getMarketDataItemNames(type, currency)) {
final int id = market().getMarketDataItemId(type, currency, name);
System.out.println(action + " item: " + name);
int processed = doAction(action, type, id);
totalProcessed += processed;
System.out.println("Done: " + processed + " Total: " + totalProcessed);

Again, the reporting logic is buried inside the loops, and this may fine indeed for a simple script. In other circumstances, you may want to be able to achieve a bit more independence between the functionalities:

The Scala way: aggregating results

The idea here is to be able to write:


With the same report function which will:
  • take a list of Report resulting from each action,
  • print the current Report
  • cumulate the current Report with a running total Report
  • without having to process everything, then do the reporting,...
I will not go in every detail of the exact solution (which is a bit complex to my taste in fact, see below) but here are the principles. First of all, each action yields its result as a Report object, containing the name of the processed item and the result of the processed action:
def archive(items: Iterable[Item]) =
for ((itemType, itemId, itemName) <- items)
yield Report(itemName, archive(itemType, itemId, itemName))

Then, the report function judiciously uses the reduce function to do the sum and report each element:

def report(reports : Iterable[Report]) = {
reports.reduceLeft {(x:Report, y: Report) =>
(x + y).report

Not so readable for the non-expert eye, right?! Press F1:
  • report iterates over a list of Reports
  • it sums all elements 2 by 2 until we have a final result. List("h", "e", "l", "l", "o").reduceLeft((a: String, b: String) => a + b) would produce: "hello"
  • before returning the summed element, it calls the report function to allow the aggregated report to print itself to the console:

class Report(itemName: String, processed: Int) {
var total: Int = 0
def +(c: Report) = { = total + c.processed; c }
def report = { reportItem; reportTotal; this }
def reportItem = println("Item " + itemName + ": " + processed)
def reportTotal = println("total number: " + total)

The sad truth is that my real-world solution is a tad more complex that the one presented above. In the "real-life", I have different types of Reports because each action doesn't bring the same kind of results.

count only returns counted elements, archive and restore return deleted elements + processed elements (to double-check that the action is ok). Abstracting over this and defining a Summable interface to provide addition over both Int and Tuples proved a bit more challenging than using the plain Java solution.

But the good news is that as long as you're not looking for too much abstraction, you will find really neat ways to write common programming logic in Scala.

04 July 2007

Scala to heaven, first step

Now let's start with our first step with Scala.

In the next posts, I'll be certainly assuming that you come to Scala from Java, however everybody is welcome!

I want first to demonstrate the use of a wonderful idea, borrowed from Haskell, the Option class. And this will be used with pattern matching. Said like that, this looks like advanced Computer Science stuff, but it's not.

This will help us manage something that plagues a lot of java applications: the dreaded NullPointerException.

An Option to parse options

Yes, I admit it. Parsing "options" on a command line is not the better way to introduce the "Option" class. So I'll try to go slow enough to reduce the confusion I have introduced in the first place,...

Anyway, this is a "real" example ("real" as "I'm using it", not as "the space shuttle runs with that"). I wrote a small utility in Scala to analyse the options passed on a command line. It can be used that way:

val action = getOptionValue("-action", "generateQuotes", args)

  • "-action" is the name of the option on the command line: -action generateQuotes for instance
  • generateQuotes is the default action if no "-action" option has been specified
  • args is an array of string containing all the options passed on the command line (typically through the main static method)
The Java way

How can I implement this getOptionValue method? Let's first provide a parseOption function taking only the name of the option and the arguments (args) as parameters. This function should return the value of the option if such a thing exists on the command line. What could be a Java implementation of that?

public String getOptionValue(String name, String defaultValue, Array[String] args) {
final String option = parseOption(name, args);
if (option == null)
return defaultValue;
return option;

public String parseOption(String name, Array[String] args) {
for (int i=0; i < args.length - 1; i++)
if (args[i].equalsIgnoreCase(name) && i < args.length - 1 && args[i+1] != null)
return args[i+1];
return null;

Please don't stare too much at the for loop, it is very naive but it was as YAGNI as I needed. Let's focus instead on the return value of the parseOption function. This function can either return a String or a null pointer meaning "I have not found an option value corresponding to the name you were asking for".

Unfortunately, if I don't look at the parseOption code, I have no way to tell what the return value when the option is not found. Is it null, is it an empty string? Many times, in real life, this can be pretty much hard to tell, because the value comes from another function which comes from,... and so on.

So in presence of a large codebase, you may eventually add a superflous check:

if (option == null || option.equals("")) return defaultValue;

Better be safe than sorry,...

The Scala way

Now what does Scala offers in that situation?

The Option class provides 2 subclasses:
  • None, which represents "I have found nothing"
  • Some, which represents "I have found something, and you can get it"
The parseOption function in Scala is defined like this:

def parseOption(name:String, args:Array[String]): Option[String] = {
for (i <- List.range(0, args.length - 1))
if (args(i).equalsIgnoreCase(name) && i < args.length -1 && args(i+1) != null)
return Some(args(i+1))
return None

And the getOptionValue function is defined with:
def getOptionValue(name:String, defaultValue:String, args:Array[String]): String = {
parseOption(name, args) match {
case None => defaultValue
case Some(value) => value

which reads like:
  1. parse the option with the name 'name'
  2. if you find nothing, return the defaultValue
  3. if you find some(thing), designated by 'value', return that value
Pattern matching in 3 sentences

In Scala, the "object match {case xxx => ...}" construct implements the "pattern matching" idea. Given an object, if its "structure" matches a given pattern, you can use parts of this object to do your job. In that case, I get an object 'Some', which was constructed from a 'value', so I can de-construct the object and access the value.

The death of the NPE?

I find this in itself quite useful and elegant, but this is not all! What if you forgot to add a None clause to your match construct? You get a compiler warning! So, not only you can infer from the parseOption signature that you will have to deal with None values, but you are even reminded to do so!

Now, if you add the Option class to the fact that Scala encourages you to declare final variables (with the "val" modifier, a bit shorter that "final" in Java) and forces you to assign a value to modifiable ones, you can really think twice when you have to write the word "null" in a Scala program.

And even more to the point

And there are other jewels, too. You can convey this idea of a "defaultValue" in an even more concise way. The Option class has a "getOrElse" method, so the code above can be rewritten as:

def getOptionValue(name:String, defaultValue:String, args:Array[String]) = {
parseOption(name, args).getOrElse(defaultValue)

You can also do it the other way around. You can define an action that will be done only if the value exists. The Option class implements Iterable, so you can write:

parseOption(name, args).foreach(Console.println _)

In that example foreach iterates on every value contained in the Option (designated by "_") and print it. So the value is printed only if it exists. Of course, here, I would really prefer a more meaningful term (such as "do") but you know what? There is also a technique that allow you to add methods to scala library classes, so it is possible to write that too! More on that later,...


To the interested reader, here are some more references:
  • This is how David Pollak uses options, in the lift web framework, written in Scala
  • The ancestor: Maybe in Haskell (you will also notice the presence of an Either structure which can be very convenient too)

Never ending debate

As a conclusion I will add another layer at the dynamic typing vs static typing debate. One of the goal I follow by using Scala is to experiment when and how static typing is better than dynamic typing. My current observations are:
  • Sometimes it can be irritating to get the types right, and you have to be quite aware about things such as co-variance, contra-variance, dependent types and all the folklore. I will certainly write a post about that to give another "real-life" example, otherwise it can look pretty esoteric. The way I see it is that static typing is a way to encode some business constraints so that they can be checked before you even run the program. Sometimes it is possible and clean, sometimes the translation from your domain constraints to type constraints is a bit clumsy but it works, sometimes it is downright impossible and you need some "programming"
  • There are some things I can do with dynamic typing that I can not do with static typing, such as generating some classes with repetitive attributes and methods, depending on the content of a file. The dynamic solution is very concise and provide "just good enough" objects
Longer post than I thought for a "first step" but I really hope this will give you some drive to try Scala out and then you'll get the virus!

03 July 2007

Scala to heaven, ground

Selecting one "Programming language of the year", yes but which one?

The contenders

I pre-selected several contenders:

1. An ML language, such as OCaml
2. A "purely" functional language such as Haskell
3. A language designed for concurrency such as Erlang

Those 3 languages looked very interesting to me, each of them addressing fundamental software considerations. Well that's the rational pitch. But what was anedoctically exciting?

Observation round

Erlang was the promise to venture in a domain where I feel seriously weak, concurrent programming, with a brand new paradigm, very different from what I know. OCaml was a bit unknown to me, the only references I had were a colleague telling me that he gave up with OCaml one day the type inferencer was as lost as him and a financial company actually using it.

Haskell was perhaps the most appealing, for 2 reasons: QuickCheck (I loved Tom's Moertel description of his participation to IFCP) and the promise of mind-bending ideas baked in the language.

In order to shed more light on the subject I started reading.

"A history of Haskell, being lazy with class", is a wonderful way to introduce the language and give great insights on the "why" more than the "what" and "how". I also read "The little MLer" to become more familiar to the ML kind. And I was waiting for Joe Armstrong's book on Erlang.

But then I noticed something. Yes, that little language, there, hidden behind the others, come here!

I have a winner

What's your name? Scala
What do you know about concurrent programming? I, I have a clever actors library which may help get those performances
What about functional programming? Well, I unify the object and function approaches so you can get best of both worlds
How useful are you? I can play well with your existing java infrastructure
Do you do pattern matching and gadt? Yep
Isn't static typing to tiring? No, you just have to declare the bare minimum. The rest is inferred. By the way, this should allow me to get a top-level IDE with refactoring and code completion soon.
What about some cool stuff I have in Haskell? You mean Options, Parsers/Combinators and QuickCheck? Can do that too.
Are you system-engineering friendly? Some even pretend that dependency injection is part of me
Can you ease XML processing? I have XML literals and pattern matching against xml structures
Can I write DSLs with you? I am not Ruby but I have clever tricks that can help you a lot for that.

And the list goes on. I haven't discovered the least of what can be done with Scala and it is funny to see on the mailing list that even the language designers seems to think that there will be unexpected and surprising uses of scala features in the future.

First steps

The next steps in the "Scala to heaven" series will try to demonstrate the use of day-to-day Scala features to solve my own programming issues. As a beginner, I feel this is missing. We have good examples in the documentation, but abundance of examples won't be bad.

25 June 2007

Scala to heaven, under the surface

Selecting one "Programming language of the year", yes but which one?

At school

First, let's have a look at my abysmal lack of culture on the subject. I think my first programming language must have been Basic. The only mental image I keep from that time is a linear sequence of instructions, with the ability to go back in time or to jump in the "future" to do something else.

Then I played with the Turtle a bit (Logo), I was much more comfortable with recursion, the way function reuse was presented to me and the interactivity with the environment.

At the Engineering school

But they told me that I should structure my programs, encapsulate, and there came Pascal and (later) ADA. In the meantime, I was shown enough Scheme to understand that recursion must be a fundamental thing, at least to return change in a vending machine.

My first internship

C++ and object-orientation sparked a bit more my interest in programming languages, as I first worked on a library for algorithmic geometry. Now I have "objects" I can manipulate, turn up and down, shuffle around. My language eventually supported a wonderful abstraction. "Hello, Mr Object, I am also an object, how can I help you?".

Most of my professional life

Java was a natural move from C++. No more nasty memory bugs, pointers and de-reference. Yes, that kind of profound analysis,... But to my credit, I was more interested by finding efficient ways to understand business concepts and map them properly to a programming language than by the language itself.

The last 2 years

On the road to languages zen, I had several "enlightments". First, the Pragmatic Programmers book gave me an advice: learn a new language each year. Well yes, nice advice, but I have JAVA, the quintessential power of a modern object-language at the tip of my fingers. Why in hell would I be interested in something else?

But damn, it was a conspiracy. Paul Graham, chanting the virtues of Lisp, Steve Yegge executing the nouns (what! my favourite thinking tool?!), even my favourite platform was growing new groovy languages.

Cool! Good place to start. I happened to imagine that, for the product we developed at the time, programing was required here and there to help users describe exactly what they wanted. So I embedded Groovy in our application and started appreciate some nice features:

myList.join(", ") or myList.each { x -> doStuffWith(x) }

Wow, this makes a difference. I can express my mind much more precisely than before! Projecting my ideas on a editor is much more straightforward.

Yes, but can we do better?

Let's go: "Programming with Ruby". Seems cool, Ruby has inspired Groovy (blocks, some of it early syntax), it's recommended by the gurus and there's even this thing, making a lot of noise, there, Rails. Not a bad idea, java for webapps is such a stack.

Of course, I appreciated that I was able to build my first useless website with just a few lines of code and an editor (not to mention my further use of Camping). But I was really blown away by Ruby. So many features that can really make your life easier and say more with less.

One month ago

Time to move on, now I know I don't know. Even if I appreciate that Ruby can "steal" features from other languages, I want something new and useful this year.

Let's go hunting for the language of the year!

11 June 2007

Less is more, slow is fast (expensive is cheap?)

Dear Joel,...

Here's a letter I just received from Joel Spolsky's company, FogCreek Software. One year and a half later, they've sent me, for the second time, a DVD I had ordered. Good customer service, you would say: my first package was lost in the wild so they graciously sent me another one. But I really didn't expect that they would resend me the initial DVD that was miraculously returned to them after more than a year! Is that exceptional service or what?

Why did they do that? I was already pretty much satisfied the first time when they quickly sent me another DVD after the first one was lost (I've even heard about some companies doing that on purpose, just to show how good their support was afterwards). But what could they gain by over-satisfying me? Well, I assume, they just didn't miss that wonderful opportunity to show me how much they actually care about me. Not that I felt so miserable and alone on earth, but I was really touched by the attention. Now I really can say that this company has something special.

Ok, ok. How can they afford that? How can they sustain a double-digit growth each year by wasting so much time? Well, they simply don't ("Fog Creek is focused on growing slowly and carefully and staying profitable"). However if they pay so much attention to each of their customer, which I think they do, they are certainly here to stay.

Less is more

There is some kind of "less is more" pattern here. Less market growth, conquest, expansion,... but also more satisfied customers, upgraded installations, license extensions (anyway, Joel thinks that it takes ten years). And guess what, I think that the people working at the customer service are happier too. They certainly prefer 1000 times receive congratulations than coping with angry clients.

In a sense, this is also a "political" decision. Deliberately choosing to do less but to do better shows how you care for others: your customers, your co-workers. It is some kind of "idealist" choice. But is it also a "pragmatic" choice? Is it also a good economic choice for your company, especially for a software company (yes, I don't speculate often about fashion,...)?

Just wondering

Unfortunately, I don't have an answer with elaborate studies. But I am wondering,...

1. Are there times when you should choose "speed" against "quality"?

From the moment you select a feature to implement in your next release what do you get from delivering it faster but buggy? One more customer, of course! The one who is craving for this new feature. The one who's considering that not having it is a deal-breaker, the one,... who will be so mad to discover that it doesn't work!

[Unless he's just a golf player, he's a close buddy of the CEO, and he doesn't care so much as long as your software is buzzword-compliant. But this is another discussion]

Even when prototyping a new idea, we should be careful with "speed", because the frontier between "rapid prototyping" and "semi-chaos" is pretty thin (not to talk about the frontier between "prototype done" and "let's call it 1.0"?)

2. Is it so expensive to focus on the user-interface?

Yes, this can be pretty expensive. You may have to hire a usability engineer for the sole purpose. And I say that because I know what's the big gap between the half-baked interfaces I will ever produce and the interfaces that are carefully crafted by professionals. But this is not only for the beauty of the game. How many hours are going to be lost by: your helpdesk, your consultants, your newly-hired engineers, your partners? Oh, yes, and your customers,...

3. Why not set-up an "aggressive plan"?

This one really makes me wonder,... I guess that for any project, if you could start it over a thousand times, you may discover the most efficient way to order and parallelize activities so that the least amount of time is lost. But you can't do better, can you?

What are the variables you can play with when starting a project?

1. deadlines
2. quantity of people
3. motivation
4. quality of people
5. customer expectations

So far, an "aggressive plan":

1. fixes deadlines using ballpark estimates of the project estimates and insists that the target can't be missed. Time lines being then the only success criteria, this usually compromises any chance of success before the project even begun.

2. throws in between a moderate amount of people ("we can do it") and a big crowd ("we'll do the maximum")

There are strong suspicions that throwing more people on a project can only make it worse (Brooke's law and this study). Anyway, I am not sure there's much "aggressiveness" to have here. The "right" number of people will do. The good question is not: "is it aggressive enough?" whatever it means, but "are we properly staffed for this?"

3. motivates highly everybody

By either: insisting on the "heroic" aspect of the venture or reminding that "aggressive" concerns each one personally,...

4. tries to gather talented individuals,..

As far as 2 or 3 weeks allow it (because the preparation phase was also "aggressive")

5. decides bravely to deliver only one single feature.

No, just kidding!

Quality, ergonomic user-interfaces, sustainable project pace and even slack, why not go slow?

"less is slow, slow is fast, fast is more" (or the other way around ;-) )

One place where it makes the most sense in certainly when coding:
  • slow is fast: you worked hard to produce less, but you get more value because code is a liability, and less allow you to move things around faster
  • fast is more: there is a tipping point when you can add much more ideas to your software because you just play with the right legos
Why does it sound so paradoxical? A is ContraryOf(A). I believe strongly this is a matter of perceptions.


Try this at home. As an experiment:
  • crank out code (I mean "crank" if you don't religiously follow the red-green-refactor mantra)
  • take time to refactor it
  • add some meaningful comments (the why, not the how)
  • add some user doc (it can be a document or a few comments in a header)
  • take note of the additional time taken
  • forget all that for a few months (2 will usually do)
  • try to estimate how much time was saved -just for you- when you read that code again
Seems simple and obvious, so why don't we do that all the time? Because we're so focused on the here and now that we don't perceive the poor chap that will read the code in 2 months. He's not here now, to cry in anger [This is why pair programming helps. He's sitting next to you,...]

Same idea, different context: why don't we do TDD all the time? Because we don't perceive the difficulty of what we're doing. If you'd realize that each line of code can bear many different and subtle bugs, you would just want to secure it before coding the rest. [And there are plenty of other benefices too].

I am ashamed to confess this, but 2 days ago I wrote a simple "Table" class, taking a list of n row names, a list of m column names and an array of n x m values. I had to create a method that returned the value for a given row name and column name. Can you do it with you eyes closed? Good for you. I was inspired enough to write a few specs before using the class,...

Those are 2 examples showing that we should be careful with our perceptions and ask ourselves:
  • Can we really "not afford" to refactor?
  • Can we really "not afford" to give more time to a project?
  • Can we really "not afford" to hire a usability engineer?
  • Can we really "not afford" to look for better tools and practices?
My theory is that the reality of what we do is so complex that we can be easily fooled by our perceptions [Or we choose to see things this way!]

Higher-order ideas

In my next post, I'll go slow again. I'll have a look at a language I just started learning. Yes, there's going to be a substantial learning curve, desperate evenings just trying to understand why this f*****g line doesn't work. But I am pretty sure this will pay off, allowing me to get more with less, to go fast because I was slow.

I've chosen Scala as my "Programming language of the year". Scala is not even in the 50 most popular programming languages, but I think it really embodies higher-order ideas, ideas that let you express other ideas with less words, doing more with less.

Stay tuned for the "Scala to Heaven"!

06 May 2007

Net pearls

Reading tons and tons of internet stuff, I often find some posts or some documents I find interesting or funny: some net pearls.

As usual I would like to share this with the rest of the world and present those collected pearls as a post.

Pearl n. 1

This historical document by Dr. Winston Royce presents the waterfall model. The interesting sentence in this document is:
I believe in this concept, but the implementation described above is risky and invites failure.
This line must not have been quoted very often by waterfall proponents,...

Pearl n. 2

The next pearl comes from a study of the productivity of several languages. This story is interesting because it compares the use of different types of languages, static or dynamic, through different axes: productivity, length, memory consumption, speed,... Whatever my love for programming languages, the pearl is in the following conclusion:
Interpersonal variability, that is the capability and
behavior differences between programmers using the
same language, tends to account for more differences
between programs than a change of the programming
Pearl n. 3

This one is both funny and interesting (read the whole thread): do we need a "might-equal" operator?
How about adding a "might-equal" operator?  I recommend useng
the characters "=?". I hav implemented my hone programming
language, incubating the might-equal operater. It works
somthing like this:

if (x =? y)
print ("x might equal y");
print ("x might not equal y");

Acording to my studys, the might-eqqul operatir is 299834%
more useful than the standerd ekwal operator. For
testing porposes, I have usd the mighty-kwal operatyr
in severul custom sofware packages, including spell
chex and statistics soffy-soff. I hope that you ull can
make yous of it in yore own pergroomink lunkishes.

Pearl n. 4

This presentation from Michael Feathers introduces some coaching patterns and suggests visualizing the group as an individual, "Pat", even if one-on-one action is the privileged interaction medium:
‘Pat’ does not embody an organizational goals, ‘Pat’ is an amalgam of the team
Pearl n. 5

A little bit of history here. Here's a snapshot of notes taken during the meeting that led to the Agile Manifesto.

Pearl n. 6

Acceptance tests is one area that is still not very well covered by software vendors, despite their great usefullness (in terms of executable specifications). This may not be true anymore with Greenpepper software.

Pearl n. 7

Do you like programming quizzes? Here's a nice puzzle from John McCarthy (Lisp's inventor), solved with Haskell:

We pick two numbers a and b, so that a>=b and both
numbers are within the range [2,99]. We give Mr.P
the product a*b and give Mr.S the sum a+b. The
following dialog takes place:

Mr.P: I don't know the numbers
Mr.S: I knew you didn't know. I don't know either.
Mr.P: Now I know the numbers
Mr.S: Now I know them too

Can we find the numbers a and b?
This puzzle is solved by encoding the facts in a few statements.

Pearl n. 8

You can still do funny stuff with Java! Check this out and turn the page.

Pearl n. 9

What's hot, what are the next technologies you should keep an eye on? This conference program proposes some answers:
  • Services (SOA, web services, composition, mashup)
  • Web frameworks (Rails, Tapestry) and Ajax
  • Application frameworks (Spring, OSGi)
  • Build systems (Maven) and automation
  • AOP
  • Open-source (use, participation)
  • Collaborative, open webapps (Web 2.0)
Pearl n. 10

I like concrete examples when it comes to describe concurrencies issues, the next big question in our industry (not in the previous list, which also shows that is it far from complete). But fortunately, humans do distributed computing!

Bob. Alice calls Bob: "Could you get me those numbers?"

Bob jots Alice's request on his to-do list. "Sure thing, Alice, I promise I'll get them for you after I solve this engineering problem."

Bob has handed Alice a promise for the answer. He has not handed her the answer. But neither Bob nor Alice sits on their hands, blocked, waiting for the resolution.

Rather, Bob continues to work his current problem. And Alice goes to Carol, the CFO: "Carol, when Bob gets those numbers, plug 'em into the spreadsheet and give me the new budget,okay?"

Carol: "No problem." Carol writes Alice's request on her own to-do list, but does not put it either first or last in the list. Rather, she puts it in the conditional part of the list, to be done when the condition is met--in this case, when Bob fulfills his promise.

Conceptually, Alice has handed to Carol a copy of Bob's promise for numbers, and Carol has handed to Alice a promise for a new integrated spreadsheet. Once again, no one waits around, blocked. Carol ambles down the hall for a contract negotiation, Alice goes back to preparing for the IPO.

When Bob finishes his calculations, he signals that his promise has been fulfilled; when Carol receives the signal, she uses Bob's fulfilled promise to fulfill her own promise; when Carol fulfills her promise, Alice gets her spreadsheet. A sophisticated distributed computation has been completed so simply that no one realizes an advanced degree in computer science should have been required.


Here we are. I didn't mean at all to reach 10 pearls exactly but this came to be precisely the number of pearls I wanted to share. This must be a proof of the profound wisdom they embody.

See you for Net pearls 2! (Sooooo handy when I have nothing to say ;-) )

11 April 2007

Pattern matching with Ruby

How do you know you got it all in your toolbox? Short answer: you never have it all. Long answer: you may add more, a lot more,...

I was reading one of Steve Yegge's old posts lately: "Choosing languages". This article introduced me to the pattern matching capabilities of a language such as Haskell. Wow,... Yes we can't do that in Ruby. But can't we really?

Indeed, this triggered my memory like a mantra: "if it's not nailed down, steal it... if it's not nailed down, steal it,..." Here's the article: If it's not nailed down, steal it. The author presents a "theft" in Ruby: the implementation of pattern matching.

So using the 'multi' gem, you are able to express the Fibonacci function as:
multi (:fib, 0) { 1 }
multi (:fib, 1) { 1 }
multi (:fib, Integer) { |n| fib(n-1) + fib(n-2) }
This may not be very impressive here, but later on in Steve's article, Steve tells us that pattern matching can really be a huge win when dealing with complex structured data. Here's an example from a contest in the Amazon's developer journal.

bad [[_,fs],[_,ds],[_,cs],[_,gs]] = (cs /= fs) && (cs == ds || cs == gs)

Here, the bad function gets a list as its argument. But instead of naming the list, it specifies a pattern that it expects the list to match: a list of 2-item sublists. It ignores the first element of each sublist, and assigns the second element to fs (farmer side), ds (dog side), etc. The variables then become available for use in the body of the function, after the "=" sign, where we check if if the chicken's alone with the dog or the grain.


For sure, writing the same thing in Java, or even in plain Ruby would be a pain. So I rolled up my sleeves and tried to enhance the 'multi' gem with the ability to use Arrays and variables in Arrays. So I allowed to write things such as:
multi(:foo, [String, Integer]) {|x, y| x; y}
multi(:foo, [:p1, :p2]) { |symbols| symbols[:p1]; symbols[:p2]}
multi(:foo, [:p1, [Integer, :p3]]) { |symbols, x| symbols[:p1]; symbols[:p3]; x}
multi(:foo, [:p1, [:_, :p2]]) { |symbols| symbols[:p1]; symbols[:p2]}
I also added some support for a 'multi' block, which is closer to a Haskell definition (though the 'let' name will be certainly shock a lisper,...):
multi(:foo) do
let(String) {|x| x.should eql "hello"}
let(String, String) {|x, y| x.should eql "hello"; y.should eql "world"}
My conclusion on that little experiment was:
  • The use of a "symbols" map is a bit awkward, even if it uses the symbols declared in the pattern. It looks like a trick compared to a Haskell definition
  • It is possible to reproduce Steve's examples in an easy way (I didn't say efficient!)
  • The limitations mentioned in the article regarding the use of 'multi' methods in classes still hold: they are not inherited. 'multi' is not the 'def' keyword
What I am really interested in is: will I use it? Steve says that once you've tasted Pattern matching, you start seeing it everywhere (design patterns, anyone?). I still don't see it in my daily java programming, nor in my nightly ruby programming.

Now, let's say I start seeing it ("Yes, I got it! I am in the Matrix!!"). Should I use it in my ruby code? And should I use the rest? Let's see:
Is it still Ruby? Will other people still understand what I write?

Hey, why not? I am truly amazed by the Ruby's capacities to be benefit from ideas from other languages or paradigms. Indeed, I think it still feels quite Ruby-like with functions and blocks, and those features allow me to write more concise code, at the right abstraction level.

However, capturing an animal and putting him in a zoo is not the same as watching him in its environment. Haskell, Lisp, Erlang and others are quite unique and they each provide different ways to blow your mind.

By the way, is it possible to do concurrency programming "a la" Erlang in Ruby? The challenge is opened,...

15 March 2007

StrengthFinder: discover your talents, turn them into strengths!

I was the great show opener with my Spiderman costume

When I was 10 years old, my class was organizing a small theater play, based on the novel "Colombine" which is a variation on the famous characters from the Comedia del' Arte.

I have always been fascinated by stage action but this time, this was special. Catherine, my secret-beloved-classmate was playing the role of Colombine and she was the most beautiful girl ever in her white dress.

The other central character of this comedy was Arlequin, a joyful, carefree, colorful character. Arlequin entertains Colombine, charming her with his tricks and pleasant stories. I would have loved to play this role, the role of my life for sure, but it had been given to Gilles,... my best friend.

Well, I had accepted my fate, especially since Gilles was 1. a very good Arlequin, 2. my best friend. Until the day Gilles was ill at home,...

He can't rehearse the role! "Pleaaaaaase, let me do it!!!!". Yes! Here I go, ready to seduce Catherine with all my acting talents. Helas, after a few tries, everybody sees that I am not at all the same little extravagant boy than Gilles and that my performance is, how would I say? Well, average,...

Realizing on stage that I will never be able to seduce Colombine/Catherine by being her beloved Arlequin, I burst into tears.

Then, my teacher has the kind of idea that makes a great teacher. She acknowledges that I love being on stage, that I am a serious boy, engaging to other people and that I prefer intimacy to extravagance. So she just decides to create the perfect role for me, building on my talents. I'll be presenting the play!

So, on the big night, I sat under the lights, opening the "Colombine" book, reading an introduction that created the warm feeling of entering a formidable romantic story. Even if having a Spiderman costume, the only one left, must have left some parents wondering,...

Do your best or do -your- best?

This teacher had the great instinct to let me do something I was good at and that motivated me a lot, creatively finding a way to use my talents. She turned something which could have been a traumatic experience into a success story.

This story is like the piece of a puzzle for me. Last year, I made the semi-conscious decision that I should definitely do what I was good at. Why would I try to be a good project manager, a salesperson, a coach, an entrepreneur, a PR, a professional services leader, a top-executive if I can't give -my- best at it. Not my best efforts, but my own best, the things which make me very different from anybody else. Deciding this is both relieving and very energizing. This is the second piece of the puzzle.

Rely on other's strengths, they love that!

I also remember 2 years ago how I realized that I was much more successful when letting others use their own specific talents without trying to do all by myself. Severine is good at organizing meetings, she can do it! Rely on other's talents when you lack some. They will even be grateful that you let them do what they love and what they're successful at!

No wonder why I am always asking candidates what they like to do,... This is the third piece of the puzzle.

The online test

Then, starting a new job this year, I met my project manager, freshly arrived in the company, too. I was very impressed by the way he had been handling things and connecting to people from day 1. Having read "Behind closed doors: the secrets of great managers" last year, I thought: "He's really acting by the book!" (Kaz, you are allowed to blush if you read this blog,...).

Kaz told me about an idea I had discovered last year, when working with an external HR cabinet in my previous company: improve people's strengths, not their weaknesses. This idea was really appealing but also very perturbing (more on that further down).

So Kaz wants people in his team to pass a "StrengthFinder" test. This test is provided by the Gallup institute and aims at finding the 5 top strengths amongst 34 possible strengths.

I passed the test and the first thing I noticed having read the results was: "Hey, that's the first test I cannot feel bad about!". The test is telling me: here, here,... and here, you're really great!

And what about the rest? Is there something I should improve, work on, be wary of? NO, forget it. Use all your energy maximizing your own existing talents. Not the ones you may possibly acquire on the edge of exhaustion.

This makes hell of a difference, creating a nice little psychological "click" in your head,...

Me, you, our team

I don't want to paraphrase the book, it says a lot more that I do on the subject but here are some personal and team consequences:

  • It is more than ok to rely on others strengths. This has many positive side effects: work is done ok, people love doing that, you are relieved of what you don't like
  • Ÿ It is very important to think about your team in terms of provided strengths because this is your only building material for your project. You're not going to build on their weaknesses!
  • Ÿ You can also think about the strengths you're looking for when recruiting, to complement those of your team
  • Ÿ There's no better way to set yearly objectives: people have more chances to be successful, they will bring more value to the company, they will enjoy doing their job
  • Ÿ "StrengthsFinder" is in fact more like "TalentsFinder". Now I know what are my talents, I still need to grow them to turn them into real strengths. Even Mozart had to practice violin,...

Now, this little "click" is so important that I need to talk about the thoughts that can sabotage it.

Preserve the "click"

It is very interesting to start thinking about why we can hear some kind of big warning when thinking about this "strengths-only" approach. Very nice, but,... :

1. Your defaults can ruin your life/project if you forget about them

2. You can always improve

3. You should always improve (it's a matter of will)

4. Life cannot be a bed of roses where you just do what you like

5. Why would I forget about trying to be a great Commander?

Let's take these points one by one:

1. Well, you don't have to totally forget about them! You can for instance use personal strategies to mitigate most of the side-effects. You're very creative but incapable to think about paying your bills? Add an Outlook recurrent meeting to your calendar, buy a Palm pilot, arrange an automatic transfer, put a sticker on your fridge,... Whatever. Just do the little thing that will prevent disasters. Don't try to turn into a "Logistic man/woman". But wait do better than that: find someone that will be delighted to do that for you! (Is that what marriage is all about ;-) ?)

2. Yes, you -may- improve, but is it worth the energy and, most of the time, the unpleasant feeling worth it? Why don't you use this energy to be even better and successfull in what you're already great at? You prefer to hear: "s/he's incomparable" or "s/he's trying hard"?

3. This is more like a moral argument whose psychological roots I leave to the reader to find out,...

4. Why not? By carefully selecting who you work with, you may just find in others the strengths that makes your life great. Chasing receipts and checking amounts to the cent is not considered to be anyone's preferred activity. Yet, I've met people just loving to do that!
I like this one,... Well, because you can't have your cake and eat it. By focusing on your strengths, you're great but you give up being a super-heros capable of anything. I will never be Arlequin but I can be a super-presenter,...

My top 5

To finish with, here are my Top 5 strengths:


From what I know, this is pretty accurate and provides a solid starting point for improvement.

Now, why don't you take the test and drop me a line about how it feel passing it!

Disclaimer: To do the test, you have to buy the book. I have no interests in Gallup and there must be numerous other resources with the same approach, I am only interested in -your- little "click",...

Is Project Manager the highest lifeform?

Back after a looonnng time

I haven't kept the sacred "blog at least once a month" pace since last December because of my move to Tokyo (And I still don't have internet at home, arghh). However, here's my comeback
with a classical post about the developer vs project manager divide. I decided that an interview would be a better form of post than a long monolog and I really enjoyed doing it (beside I don't have to be the one to be smart!). You can find many more posts on the subject, including this recent one that had lots of comments, even if it's more on the lines of "everything sucks".

But I have more posts coming up:
  • StrengthFinder 2.0
  • Pattern matching with Ruby
  • Net pearls
So stay tuned!

An interview for a change

I have had comments by Teki on my "Interesting software vs plumbing" post. Those comments brought us to the subject of programmer's evolution: starting from being a self-satisfied geek, why would you become a project manager?

Wanting to write a few lines on the subject, I preferred to go for an interview with one of the best software professionals I've came in touch with, my friend Olivier. [some of my comment are in brackets]

me> Olivier, it is sometimes said that a programmer should evolve to a manager position to get a satisfying job in terms of career (recognition, money, autonomy, employment), what do you think about that?

him> I don't agree! A good programmer doesn't necessarily make a good project manager. Becoming a project manager is not the only possible evolution. Technical expertise is also amongst the possibilities. It is true that most of the time, the management positions are the only ones recognized socially and financially. But It doesn't have to be that way. In my previous job, I encouraged the HR department to setup 2 different "ladders" in the company.

By the way, as a recruiter, I have a very, very bad feeling, when a programmer comes to me and says: "Hey, I want to be a project manager in 2 or 3 years". Programmer is a real job, not a transition.

me> Do you think that there could be more than one "expertise ladder"?

him> I don't know, this "ladder" thing is essentially a social and monetary issue. Most people need social recognition and it is often correlated to salary (at least in France).

me> Is it possible to be a good programmer as well as a good project manager?

him> I hope so! [me> Olivier is both ;-)]

me> Is it seldom to be good at both? Can it be learned?

him> I think it can be learned. The real difference between Mr. Universe and me is not the amount of muscle, but essentially the time spent training. Training sufficiently may not make me Mr. Universe but it may still build lots of muscles. The only question is: will I enjoy the training, am I willing to do all necessary efforts?

me> Is it possible to be a good manager without having been/being a programmer?

him> Yes, I think so. I've worked with a very good project manager, who was not a programmer at all. However, he had a real respect for my expertise and trusted my judgement. Respect is a key point in our field. It is also true for programmers who are sometimes capable of utter pretention: try a newbie message on the mailing list for instance,...
[me> I sometimes wonder if it is possible to really have a deep understanding of programmers and software without having being one yourself]

me> Will programmers take over the world :-) and be rewarded as they should? What should they do to achieve this?

him> I don't think that any "world movement" [me> or certification program,...] can achieve that. Local action is prevalent, relentless communication is my only tool. I try to communicate whenever possible: to programmers, QA, directors, sales, consultants. Communication has to be relentless to make people realize the value of programmers.

However, the recognition of the importance of programmers will continue to rise as the upper management realizes their creativity is needed and how much turnover affects the company.

me> Why is programmers perceived added value so low?

him> Their added value? Is it really so low? Yes, perceptions can be very far from reality: some people think that programming is an obscure task, reserved for some kind of mutants. Other ones (sometimes the same but at different moments!), think that programming is easy because they use Word everyday and find it intuitive, easy to use. Or, they have programmed a few Excel macros and feel it is not such a big deal. But how much complexity is hidden behind a good software? It is so hard to make it simple!

Anyway, this is unfortunately the reality for most engineers I know, from mechanical to electricians.

me> Do you think it is a Taylorist / scientist vs agile / lean issue ? What I mean is: do you think that a Taylorist view of software leads to a natural division of Commanders / Executers while an agile view of software makes it more like a collaborative / creative process ? How do you see the future to that respect?

him> Taylorism doesn't apply well to software programming. It is fundamentally a creative activity and an undeterministic process. The V-Cycle was such a big misunderstanding. It has unfortunately spread through most of development standards from administrations to banks. Driving a car is a much better analogy: you constantly have to adjust your direction, your speed and manage obstacles as they occur.

Anyway, I don't think that any agile / lean methodology will drastically change things. Whatever has been said on the subject, there are not enough total project failures. I have not seen one around me in my whole career. [me> Lucky guy, I have seen quite a few, from medium to big].

Still it is so common to expect obligation of results and not obligation of means. Can you write a best-seller in 3 months? Maybe you can write "something" in 3 months, maybe you can write a best-seller in some years, but it takes a genius to write a best-seller in 3 months. How many programming Mozarts can write a brilliant symphony for each project they start?

me> Is there a question that you would have liked to answer ;-) ?

him> The question you ask at the beginning leads us to: what does it take to be a good programmer, a good manager? Is there an universal definition of those 2 roles, or only local ones? Whatever the answer, having the 2 activities being in conflict is always a sign of a big underlying problem: lack of respect,...