Thursday, March 09, 2006


Warning: Nerdy post on programming. If you're not into it, avoid please.

Refactoring means re-writing code keeping the external interfaces intact. There are many reasons to do this. You may be adding new features to your product/module, and the existing architecture may not allow these. So re-architecting the innards of the code, keeping your external APIs identical may be a good way to go about this.

Then there is performance. There may be parts of your code that may be tweaked to run quicker or more efficiently, and you may want to take advantage of that. Circumstances change and you may realize that the complicated bit of code you wrote as a one-off thing needs its own function call, since there may be other callers for that function and re-using the code is now important.

There is of course the possibility that what you wrote say a year back may simply not have been the best way to write that code then. You are a better programmer now, understand some of the trade-offs better and can write better code now.

Refctoring is something that one learns to look at while examining old code. Most programmers in this industry don't get the luxury of starting from a fresh code base. All of us work with existing code, and seeing things that may need refactoring is now de rigeur for me. Not necessarily because it needs to be, but as I said, new requirements and scenarios mean that there is potential for writing commonly used tasks as new functions, etc. What I write is mostly for internal consumption which makes things like application compatibility (app-compat in internal lingo) less of a challenge.

But it's difficult. One of the biggest challenges that I always have is figuring out a better way to do things. There are modules I've seen which beg for refactoring. There's something inelegant about them, something hack-ey. The key to great design (software or otherwise) is simplicity. And elegance. The solution has to look simple. And it has to look good. If it doesn't, that means you are (or the person before you has been) doing something wrong.

So, you know it's wrong, but since you've seen it being done that way, you get used to it. The classic problem I face is "That looks horribly complicated. I'm sure there's a simpler way of doing it." and I get around to figuring out a way to do it. I even take out pencil and paper (if I take out pencil and paper to work out things, that generally means it has to be pretty complicated. I generally work out many tasks in my head - more than is arguably good for me).

The most important thing is to be able to get your head out of the rut it's in because of seeing the existing solution. If you are able to throw away existing assumptions when you try to re-factor, and start from scratch to work out something better than what has gone before, that is an achievement by itself.


CAR said...

Ajay. Whats your take on the Origami?

Ajay said...

I've seen only the online ads and demos on the site. I think it's an interesting exercise -the familiar windows UI on mobile devices is a flawed paradigm and this is trying to do away with that by trying a different UI on top of the OS. But the hardware has been disappointing so far. Weight, power consumption and battery life aren't up to scratch yet IMO.

The name - Ultra Mobile PC is the lamest in a series of lame product names here yet. The Xbox and Vista have some punch as brand names. This is just so wrong on so many levels.