15 Jul 2007

Thanks Chinmayi

You wake up in the morning and switch on your computer. You find that there are 95 scraps in your Orkut profile. Many of them are from strangers whom you have never seen and quite possibly won't ever see in your life. How would you respond to those scraps? To be honest, I would just delete my Orkut profile and get away from there. But Chinmayi, the singer, responds to most of scraps (or maybe all of them?). Yesterday I just found her profile in Orkut, and I couldn't resist my want to say a "Hi" to her. I don't know anything about music; I just listen to songs as a not-so-serious hobby. I like Chinmayi because of her voice. After reading her blog and seeing her on Vijay TV, I really started liking her personality. I could even say that I am a fan of her. When I wrote a scrap on her Orkut scrapbook I didn't expect her to respond to my scrap. I know she is very busy. If my assumption is right, Carnatic music practitioners like her do practice daily for at least a couple hours. All the weekends she has concerts. As she is working in the movie industry, most definitely she will have to meet with many important people. Surprisingly, I got a response from her! I was really happy to see her scrap on my scrapbook. When I searched for my scrap in her scrapbook I got the next surprise: 95 new scraps have been written on her scrapbook in one single day! And, she is taking time to respond to my scrap. Most definitely she doesn't know me, I am not ever going to meet her at all in my life. But still she takes the time to acknowledge my message. She has about three blogs and a web site. In addition to these many scraps, she would get comments for her blogs, feedback for her web site, and emails sent directly to her mailbox. All I could think of is that she is a good woman. I want to thank her for the respect she has shown to my message. But this time I just say it here, in my blog -- because I don't want to spam her again. Thanks Chinmayi!

4 Jul 2007

Code Duplication

You might have heard people talking about not duplicating code so that it becomes easy to maintain. Back those days, I didn't know all these and used to make a hell lot of mistakes while programming. By the word mistake, I mean doing something that makes developing and maintaining software harder than it ought to be. Of course, that helped me learn a lot of principles. The first thing I learned from my mistakes is not to duplicate code/ideas. Since I was bit by duplicated code very often, I try my best to reduce duplication as much as possible. In this post I am giving a few examples to show how I try to avoid duplication. Obvious Duplication The first pattern I recognised in my code was something like the following.
if exam.mark >= 35:
    exam.result = 'pass'
    logging.debug('Setting result to pass.')
    return 'pass'
else:
    exam.result = 'fail'
    logging.debug('Setting result to fail.')
    return 'fail'
Obviously this code is duplicating ideas, so must be refactored. How will you refactor this to take away the duplication? I will write it this way:
if exam.mark >= 35:
    exam.result = 'pass'
else:
    exam.result = 'fail'
logging.debug('Setting result to ' + exam.result)
return exam.result
(To be more precise, I will also replace the literals 35, 'pass', and 'fail' with appropriately named constant variables.) More Subtle Cases This is (part of) a PyUnit test case for an imaginary function double, that takes a number and multiplies it by 2.
def testDouble(self):
    inp = 5
    expected = inp * 2
    actual = double(inp)
    self.assertEqual(expected, actual)
Here, I have used the variable inp to avoid having to duplicate the test input I am passing to double. As another example, see this code that assigns a grade to a student based on the mark they have scored.
if mark < 35:
    grade = 'N/A'
elif mark >= 35 and mark < 50:
    grade = 'D'
elif mark >= 50 and mark < 70:
    grade = 'C'
elif mark >= 70 and mark < 90:
    grade = 'B'
else:
    grade = 'A'
I would rewrite this code like this to avoid duplication of the boundary values (although this is a very small duplication).
if mark < 35:
    grade = 'N/A'
elif mark < 50:
    grade = 'D'
elif mark < 70:
    grade = 'C'
elif mark < 90:
    grade = 'B'
else:
    grade = 'A'
Tell me about the ways you follow to make your code more maintainable.

1 Jul 2007

Web Development 101

They say a picture is worth 1000 words. So a video should worth more than a picture. Definitely a hands on experience is worth much more than a video. All you starting web developers: here is your most important lesson. I'll tell you what I did to learn this lesson. If you follow what I did, hopefully you will also learn :-) Steps to follow:
  1. Think of a very good Tamil song that you don't have right now, but would love to add it to your music collection.
  2. Google for it and most likely you will get a link for that song in Raaga.com. (If not pick another song; in this particular exercise, we are gonna learn from Raaga.com.)
  3. Click on the link in Google results and if you are lucky, you can listen to the song in their site. If you run any non-Windoze OS you're doomed.
  4. Then you decide: alright, I really like this song and I'm gonna download. Click on "Download" button. You will be politely informed (with a JavaScript alert) that you got to log in if you want to download.
  5. So, you click on Register link. And you know the rest: I am too lazy to type all that now.
  6. You will have to activate your account, this, that, and all other bullshit.
  7. After you have downloaded that song, sit back and think "do I want my site to be as brain-damaged as Raaga.com?"
If you are really unfortunate (like me) you will end up visiting their site often and curse them every time. See my shopping cart now: I have no idea how item 2 and 3 differ. Worse, I cannot find that out at all. That's all for now. Now you're free to dream about your cool web site.