Thursday, February 26, 2009

defensive programming

Defensive programming complements patch programming. It is a must for those who want to enjoy life outside of programming and not climb the programmer ladder.

Below I'll unfold a short initiation session, so read on when ready ...

Let's start with the basics, I'll give you your motto: An extra safety net can't hurt. It's the salute that a defensive programmer uses to greet other defensive programmers. At the time of this writing, it is not known that defensive programmers have a special dress code, so you'll need to learn the salute by heart, and as a safety net, keep a paper with some sample code of yours at all times with you. This way you can easily prove your membership to the group and you won't be taken as an undesirable TDD- or refactoring-programmer.

A good practitioner of defensive programming requires that you:

1. Extensively use safety nets like if (obj != null) sometimes right after the object is created, so you'll never be responsible for a NPE again.

2. Add extensive comments explaining why you did what you did in the way you did instead of refactoring the code. This way, if somebody does not understand your code, it's completely their fault.

3. Clone an object both when you set it in its container and when you return the object back from the container; this way you'll be extremely sure that nobody will modify the initial object.

4. When creating a class with similar functionality to an existing one, copy and paste the old class; better yet, even if your class will deprecate / replace the old class, leave it there in case someone takes pleasure in investigating obsolete code.

5. When extending a class, to make sure that an inherited method is called, override it and merely call the super version; who knows what happens with the dynamic dispatch mechanism in the JVM and your method won't be called.

Far from being exhaustive, this list should have given you a good picture of this programming style. As a final advice, take some rhetoric classes: they will teach you how to convince the rest of the team that those bugs in your list ... aren't actually yours.

Tuesday, February 17, 2009

patch programming

It is again that time of the year when we work like crazy to clear the list of bugs; it is right after we developers did our part of feature regression testing, and right before the product will enter the QA phase, and then product launch.

It is that time of the year when we won't make important changes to the codebase not to break existing functionality. That time of the year when I am answered back with "I suggest you keep that in mind for the next version and just put a workaround instead".

That period when good old refactoring is not practiced much, and instead we code-patch. Some of the patches will be checked in with TODOs, but some of them will lack even this. Because I expect the number of TODOs in the code to increase, I decided I would put a "baseline" on the number of TODOs and compare them with the new number after release. So here it is, 895 TODOs and 31 FIXMEs. I am thinking now at a possible new code quality metric TODOs/codebase size.

If you are the optimistic type, you will have your hopes that after product launch, we will definitely replace the hacks with proper code. And I just have my doubts because everybody will be coding new features in that time of the year.

The result of all this development process can't be other than increasingly dirtier code. But how much can the code become dirtier before "exploding"? And what will this explosion mean? From a technical perspective it can mean writing the same amount of code in more time, or hiring more developers, or less frequent releases, or less features in new product versions. The peak could be a rewrite. All of these have economical consequences that you can easily infer.

I gave all of this some thought and the solution I see is practicing Zero Known Bugs. ZKB means a much shorter period like this one, so less opportunities to introduce hacks. ZKB on anybody's buglist means more confidence that making a change won't break existing functionality, means more focus on activities with added-value like new features. Zero-bugs means a smaller QA team, less fiddling with the bug tracker and less time from managers that weekly count bugs and take time sending encouraging emails that say We can do it together. It also means more free weekends and less stress.

So why not do it? I guess my next step is how I can convince the one who make decisions that this is the way to go.

P.S. After I wrote this, I intended to find a good link about ZKB. You should read it. So yes, it is controversial like most of the software development topics. But be it exactly zero, or 2, the principle is valid and we as a team are far from it, so I still needed to write this piece as one who had never been in a team that practiced ZKB :(

Friday, February 6, 2009

boring (III) / unread email count

After the fun I had last time with JavaScript, I felt like coding another micro-project along the same idea: this time I wanted to display the unread emails count in the Reader page.

So I needed that special URL GMail uses to retrieve the count. So I sent myself several emails and all this time I used HttpFox to sniff everything that went in and out my browser; in the end I discovered a JavaScript object that held the information I was after.

Next, I needed to make an async request to the URL I found previously. A very forgiving version of XmlHttpRequest that allows one to make cross-site requests is the one GreaseMonkey provides.

Next, good old string parsing did the job of extracting the unread count from the response I got from GMail, so finally I just needed to append the count to the existing GMail link in Reader.

At this step unfortunately, more testing proved that the URL I've found worked for me only and not for other GMail accounts. I relied on the fact that account specific information would be sent using cookies, and I failed.

So I was back at finding the magic URL. My search went on through cookies, text/html and text/javascript content GMail responded with and I was lucky enough to track down the same js object sent back from another URL, this time a simple one with no scrambled GET parameters. A quick test for 3 GMail accounts on 2 different machines proved it working.

You can install the script from here.

Thursday, January 29, 2009

boring (II)

If the advice in the first part of this series did not work for you, or the book you picked is too long and you'd happily put it aside for a period, here is a new idea: code your own micro-project.

It should take you ~20 minutes per day, because what I am talking here is swapping your daily blog reading with coding. And it should be smart so that you don't get bored with your own chosen assignment.

It could require much thinking, as it's the case with an algorithm from commons you've been using and you are thinking to rewrite it and make it faster. It could require studying as it's the case with a new programming language in which you want to write a Hello World. It could require browsing the web in search of information as it's the case of a public web service with which you want to interact.

And here is my story: I've been using Google products for quite a while, indeed Reader is my first browser tab; and you've realized by now that I'm also a user of Blogger. Opening Blogger always required opening a new tab and typing the blogger URL, then hitting Enter :). Yes, it's a matter of seconds, but don't forget I needed an excuse to start my micro-project.

So I ventured into the realms of Greasemonkey and with this, into Javascript and DOM. I haven't been coding hard-core javascript since the days of Netscape 4.7, and here was my chance to code JS again. I heroically resisted the temptations jQuery laid in my path ... no, this time I'm not gonna let you help me, this time I want my hands dirty with JS.

Long story short, here is now the top of my Reader page, I am just one click away from you (would you be so kind to notice the link to Blogger that my script adds whenever I access a google page):

Sunday, January 18, 2009

a good way of complaining

Do you love the things you do? ... then criticize, suggest improvements, and post bugs.


Stop complaining about the tools you use if you haven't done a thing to make them better.


Because you are part of a team, it is not only about the things you fix, but also about the things you bring to others' attention.

Sunday, January 11, 2009

you at work

How would you picture yourself during your 8h-work a day? If you were to draw yourself in a typical work day how would you do it?

Forget about your current work place, just imagine, draw your mental projection of you working. Would you be at a desk, would you wear glasses, would you have plants around you, would it be artificial light, would it be air conditioning?

How do you think the others around you (would) envision you - the professional programmer - in a typical work day? Would it be in a dark corner, bent over a keyboard and wearing your nerdy expression? Or it'd be under sun light, a merry, open expression on your face?

If it is a gray environment what you picture, if it's tens of light bulbs above you, if it's only aluminum and concrete and no plants, if it's halls and no terrace, then there is an alternative and architect Norman Foster knows it.

Watch and do the exercise above again. (The first 10 minutes of the video might bore the impatient, but it becomes really interesting afterwards)

Friday, January 9, 2009

the future is bright

This might sound familiar to you, but since the marketing department think this is important, I might as well spread the word. These are excerpts from an email arrived this morning in my Inbox:
[...] the most successful leaders will be those who leverage technology and change to go beyond new mandates for efficiency
Nearly three-quarters of CIOs now believe that long-term strategic thinking and planning is the leadership competency most critical to their roles
Clear words, clear guidelines, now we have the recipe for defeating the world economic crisis.