Archive for the ‘programming’ Category

Dear Java, Please Let Me Work

Friday, August 20th, 2010

I had to read the content of a file for a ruby project. This is how I did it:

def read_file_content(filename)
  File.read(filename)
end

Simple enough, right?

Then for some reason I thought about how I would have to the same in Java:

public String readFileContent(String filename) {
  try {
    BufferedReader reader = new BufferedReader(new FileReader(filename));
    StringBuilder buffer = new StringBuilder();
    String line = null;
    while ((line = reader.readLine()) != null) {
      buffer.append(line);
    }
    return buffer.toString();
  } catch (IOException e) {
    return "";
  }
}

Need I really explain why I prefer Ruby these days? Even if you remove the try/catch clause and rethrow the exception, that’s still a lot of code compared to a single line of ruby. I am also aware that it is possible to write the same Java code in a single line using Apache’s commons-io (IOUtils.toString(InputStream) – but still, why are the base Java libraries so verbose for file manipulation?

I still like Java, but I’m impress by the compactness of Ruby. It just works as it should. Sometimes working in Java it seems I’m just smashing the keyboard instead of getting actual work done.

Unreasonable Fear

Wednesday, August 11th, 2010

Sometimes, developers are a bunch of wimps.

We get so comfortable with our programming language that many of us are afraid to try something else.

Many of us learn one programming language (Java, C# or something else) and we stick to it no matter what. We do use libraries and frameworks and we’re often eager to try a new one in our project. But talks about learning a new programming language are usually met with quick rejection. We want to stay in our comfort zone.

I think something is wrong with this picture. Is learning Ruby on Rails really more difficult than learning Spring? Is Hibernate really simpler than activerecord?

Why are developers afraid of trying something new? Some problems can be solved easily in Clojure or Erlang. Are you sure Java/C# is the solution to your problem? It might be part of the solution, but probably not the entire solution.

Discovering ruby (and rails), I see flaws in Java that I didn’t see before. I learn a different way of doing things that expands my knowledge. When I come back to Java, I see problems from another point of view and sometimes use a different solution than I would before learning ruby.

Why are you still afraid?

Training On Hold – For Now

Tuesday, August 10th, 2010

I planned on continuing my training sessions while I was on vacation. However it seems that I am even more tired after a day in the sun than I am after a day at the office. Go figure.

Anyway I decided to put the training on hold until I come back. A guy has to take a break from time to time. And it gives me more time to read and think about my objectives for the next year. That’s what I’m going to do for the next two weeks.

I’ll continue to do katas and small projects when I come back.

How to warn users not to use an interface

Thursday, July 22nd, 2010

Everybody knows no one reads documentation. Hamcrest found an original way to warn users to use the org.hamcrest.BaseMatcher class instead of the org.hamcrest.Matcher interface.

Look at the interface yourself.

They added a dummy deprecated method called _dont_implement_Matcher___instead_extend_BaseMatcher_ in the interface.

I am still unsure whether I will ever use this in my own projects. But when this method appeared in my code today I could not help but look at it. It was then totally clear that I was not doing what they wanted me to do. I’ve seldom seen documentation that obvious.

Training week #2 and a challenge

Sunday, July 18th, 2010

This week I consolidated the two katas I did last time (Prime Number and Bloom Filter). Nothing much to talk about.

The only thing of note is that I got late in my RSS feed (again) so I missed the announcement of Corey Haines’ Learn How to Type Week.

I repent and decided to try my own this week (along with a few colleagues).

I’ll keep you posted about the results. I’ll work in increasing my wpm.

Developer in Training

Sunday, July 11th, 2010

I believe in software craftsmanship and in training to become better.

That’s why when I was challenged at work to take a few hours each week to train, I thought “What a great opportunity!”.

Well that’s what I should have thought. Instead I said: “But what about the project? The deadline? The world is going to end!”

Then I realized how lucky I was to work at Pyxis. I asked the Product Owner and it wasn’t such a big issue.

Lesson #1: Ask and thou shall receive

For my first training session, I decided to do the some katas in ruby as it has become my language of choice for pet projects.

Part 1 – Primes

I started my first session with the Prime Number kata. I like this kata since it is really simple, can easily be done in less than 30 minutes (closer to 10 once you’ve done it a few times) and I learned a few useful tricks in ruby by watching this kata permormed by Uncle Bob on Katacast.

I plan on doing the Prime Number kata every week as a warm up.

Part 2 – Bloom filters

Afterwards I tried kata #5 from http://codekata.pragprog.com/2007/01/kata_five_bloom.html – implementing a Bloom Filter. It’s an algorithm that probabilistically determine if an element is part of a set. It works like this : if the element is in the set, it will always return true. However if the element is not in the set, the bloom filter might still return true depending on some probabilistic stuff.

Training ends

Here are my highlights of the session:

  • I learned a new algorithm, it’s purpose and a few use cases
  • I performed everything in pure TDD
  • Continued to learn new keyboard shortcuts in Rubymine (I try never to use the mouse in katas. If there is a keyboard shortcut I forgot, I look it up and make a point on using it)
  • I managed to take some time to train!

The only downside I can think about is that I spent a little while trying to configure ruby 1.9.2, rspec and the latest beta of Rubymine 2.5 before reverting to ruby 1.8.7.

As for improvements, I see lots of ways to improve the Bloom Filter katas, especially when it comes to my tests. I’ll give it another try next time.

Git is not (just) Source Control

Sunday, June 20th, 2010

After many discussions with a colleague about Git, I came to realize that Git is not merely a source control. With time it became a tool in my developer’s sandbox. To develop software effectively, I need (among other) an IDE or text editor, a prompt and countless libraries. I came to rely and trust these tools.

I used many other tools for version control but I saw them as infrastructure – mandatory in the grand scheme of things but not used to improve my productivity.

Git changed everything by allowing me to rethink the way commits and branches work.

  • By promoting small commits that do one thing but do it well, git forces me to split my task in even smaller chunks that can then be commited. This sometimes leads to architectural changes.
  • By making it possible to  reorder the commit history (git rebase -i origin), it pushes me to make each commit clean and atomic.
  • Git makes branching cheap and easy. Like you, I try to code each features in a separate branch. But unlike you, I am undisciplined and often forget. However if I start on a feature and realize it takes slightly longer than expected (and I’m talking about an hour max, not 3 days), I create a branch for that feature. It’s easy and it allows me to work in small, independent chunks. It also allows me to always be able to come back to a stable branch if I need to do a few quick fixes on another part of the project.

Git is not a mere tool – it changed the way I design and code.

Interesting Quote About Failure

Friday, May 21st, 2010

I saw this quote on 37signals’ blog and I thought it fit well with my latest post on failures.

“It’s important that nobody gets mad at you for screwing up. We know screwups are an essential part of making something good. That’s why our goal is to screw up as fast as possible.”

- Lee Unkrich, director of Toy Story 3, Wired magazine

If the goal is to screw up as fast as possible, you need an environment that allows you to do it safely.

Failure is an Option

Tuesday, May 18th, 2010

Failure is often a dreaded word. It need not be. Programming is an art where it’s very easy to fail and you can usually fail fast. Use it to your advantage. Learn to fail with style.

Give yourself the option to fail deliberately and safely. Many tools can help you:

  • Branches – Since I started using git, I often create a branch to play around with the code… then delete it and start again from scratch once I know where I need to go. This gives me the freedom to change configurations, disable features and modify code without tests. I even go as far as breaking existing features (sometimes they don’t even compile) if it makes me learn faster.
  • Prototypes – create a simple throw-away project to test your code.
  • Tracer bullets – whenever I need to connect a few modules together, I like to have a tracer bullet that will handle the normal path without any error handling. It gives me confidence that all the pieces fit together. It proves the chosen architecture works and yields the expected results.
  • Tests – if I want to know how a particular method/class reacts in a specific context, I will sometimes create a test to test my behavior. I might keep the test as-is or sometimes I throw it away and it becomes part of a larger test.
  • Scripting languages – ruby, python, groovy or whatever you fancy, these languages can often help you test an algorithm faster than in Java or C#.
  • Pen & Paper – low tech, but sometimes writing down your thoughts is the best way to go. If it doesn’t work on paper, chances it works on a computer is pretty slim.
  • Rubber duck – having an inanimate yellow friend near your computer that you can always talk to can never hurt :) .

A rubber duck

Code is cheap and fast to write… once you know the way. A few failures early on will help you get a success when it counts.

Why Katas Work

Monday, December 28th, 2009

Musicians, singers and athletes have something in common – they do a lot of practice to become the best. They may have a show or a competition from time to time, but they practice every day.

Martial arts practitioners do katas over and over. At some point it becomes a form of art.

Can the same be done with code? Can you practice to code a certain piece of software over and over until it becomes art? Uncle Bob certainly thinks so. I started doing some katas a few months ago but a question was still nagging me : do they really help me become a better programmer? Does practice matter?

According to The Talent Code, the answer is probably yes.

The book explains how to harness your talent, how to become an expert in your field. And it seems that all world experts, no matter their field of expertise, have spent a lot of time (10 000 hours) practicing. Not just any kind of practice – you can’t just play around and hope you’ll learn something. There is a concept of ‘deep practice’.

There are three parts to deep practicing:

  • Chunk things up – slow things down, do a simple part of the whole
  • Repeat – a lot
  • Learn to feel what you are doing

I think that’s what I’ve been unconsciously doing with katas. I wrote the same application every time. But every time I was improving, basing the next iteration on past experience. I was using more keyboard shortcuts, less code, better idioms to express myself. I was learning from my mistakes and becoming more efficient.

I believe that katas help me to  ‘deep practice’. It forces me to reflect on what I’ve done and learn from my mistakes. In the end I believe it helps me to improve.

That’s good enough for me to keep doing it.