Monday, July 27, 2009

Asylum

Is your boss insane?

Does he take bad decision all day long, telling you to work on useless things while you know what needs to be done?

Are you colleagues insane?

Do you think their design could be better? Their code more robust? That their architecture could be more testable? That they should use this and that framework? That they don't know how to code but you do?

Are you insane?

Do you do whatever is in your power to help your boss and colleagues to understand what you think is important? Do you know the reason behind your boss' decisions? Do you understand the business, the competitors, how your product compares to others? Do you suggest improvements and changes of priority when you see fit or do you stay silent? Do you coach your colleagues when you know something they don't? Do you learn every thing you can from your colleagues, either personally or professionally?

If you start to think others are crazy, think again. Bring yourself to the center and check what you can do to understand them. Sometimes it does not take much except a small thing called 'communication'. Talk to people, understand them, know why the act the way they do. Then maybe what you saw as 'insanity' will go away... most of the time.

Wednesday, July 15, 2009

What is rubberducking anyway?

Some people have asked me where the title of my blog (rubberducking) comes from.

Well it's quite simple - rubber-ducking is the art of talking to a rubber duck to find solutions to your problems.

Sounds crazy? Well probably a little bit...

But you probably had this kind of conversation a few times:
You: Hey Joe, I've got this problem with my code... I'm wondering how to... Oh yeah just saw the problem, thanks!
Joe: ...

Now I'm sure Joe is a nice person, but what if he's not available? A rubber duck can sometimes work nicely:
You: Hey ducky, I'm having trouble with this algorithm... Everything is by the book, even this tricky part here... Oh wait, got it! Thanks!
Ducky: <quack>

Anyway I wanted a medium to explore ideas and get feedback... Rubber ducks are nice, but sometimes I need the duck to talk back, hence the blog

If talking to a single duck is not enough, you can try to talk to many pixies... You never know, it might help.

Wednesday, July 8, 2009

Java Brainteasers

I had lunch with a few colleagues yesterday and we started talking about obscure stuff. Among other things we talked about:
  • the initial default size of a hashmap, which is a power of 2 (16) in HashMap but a prime number (11) in Hashtable in java 1.6 (possible explanation, see the 'Tip' section)
  • the strictfp keyword, which can be used to ensure that floating point numbers behave the same on all hardware.
  • the volatile keyword, which seems to have a slightly different behavior since 1.5. I did not find anything from Sun about this, but I found some references indicating that something might actually have changed... Basically it seems that writing to a volatile variable synchronizes all cached copies of variables with the main memory, not just the volatile variable itself. I must admit this one was totally new to me.
Anyway that reminded me about Java Puzzlers, a book about obscure features of Java in the form of puzzles. I decided to go through the book again to see if I would gain anything. Although many puzzles show how things can go hairy while using really bad code, a few bits of knowledge can always be useful. Here is a few of the things I learned (or re-learned) while skimming through the book:

Weird behavior of Math.abs
The absolute value is not always positive!
Math.abs(Integer.MIN_VALUE) == Integer.MIN_VALUE

Unicode considered evil (at least problematic in comments)
Unicode characters in comments can cause problems. Consider the following comment:
/*
Test files are located in \utest\samples
*/

The file will not compile because the java compiler tries to convert \utest to a Unicode character (I saw this in a real project once)

Escape regex
I learned that Pattern.quote() can be used to automatically escape a literal string as a regular expression.
System.out.println(Pattern.quote(".")) will print \Q.\E

This method was added in Java 5.

Recursive array printing
I also learned about the Array.deepToString() method, which can output the content of multi-dimensional arrays. This method was also added in 1.5.
int[][] array = new int[][] {{1, 2, 3}, {4, 5}};

System.out.println(array); => [[I@1ad086a
System.out.println(Arrays.deepToString(array)); => [[1, 2, 3], [4, 5]]

Ternary operator madness
And finally the behavior of the ternary operator (? :) was changed in 1.5. Consider the following statement:
boolean b = true; // or false, doesn't matter
List list = b ? new ArrayList() : new Vector();


In 1.5, the code compiles fine.
In 1.4, I get an error 'Incompatible conditional operand types ArrayList and Vector'.
Before 1.5, both values returned by the ternary operator must have a shared ancestor other than Object. In 1.5 the requirement was relaxed - any common ancestor is fine, and since all classes inherit from Object you can return two completely unrelated classes.

Now why would you want to do something like this?
Object o = b ? new ArrayList() : new JButton();

Don't ask me, I'm just the messenger...

Thursday, July 2, 2009

Addiction

Hi, my name is Mathieu and I am a test addict

I wasn't always like this. It all started a few years ago. I was working on a graphic application and there was a fluke. I only saw it once. The colors kept flickering: red-green-red-green-red-green. I was mesmerized, I could not look away.

I was staring at the screen, trying to figure out what went wrong when I felt something... snap... inside my head. I didn't know what it was and went home, but it kept haunting me. I could not sleep that night, so I went to see the Guru, a little guy standing high on his ebony chair, on the last floor of the highest building in town. I talked about the colors, and like I felt something was missing from my life.

"Unit test you shall" was his answer.

I did some research and found what he was talking about. Testing frameworks, stubs, mocks. A new world opened to me. I read about all this weird stuff all night.

The morning after I started to test, I was wondering what it would feel like.

Red - hm mm, OK
Green - nice, something works!
Refactor - change this, and this... and the code still works!

I felt peace inside, a feeling that nothing could go wrong with my life, ever again...
Red-Green-Refactor
I was hooked.
Red-Green-Refactor, Red-Green-Refactor

Over and over, faster and faster

The light kept flickering. I couldn't get enough.
I felt... strange. I felt... alive.
Corporate drones kept coming close, attracted to the light like little bugs, but they had to shield their eyes - the light was too strong. A few people went blind and were set to maintain the mainframe, deep under the Earth's crust.

But one particular mindless Cobol Zombies was attracted by the light . I handed him the keyboard and as he started to code, I could see flesh forming all over his rotten body. He was alive!

The addiction could spread. I was amazed. I had just saved a life!

But I cannot do all the work alone. I need your help. Help spread the word, help save the world!

Disclaimer: This is a mostly true story. Some events and places have been slightly modified for my own enjoyment.
No Cobol Zombies were injured during the writing of this story.