programming

Moronic beyond belief

That’s me, in case you didn’t already know. While a lifetime of evidence exists, in the interests of brevity, I will only present its latest manifestation. Warning: this is a work-related (read: geeky) rant.

if ( A or B ) // A and B are some valid conditions
{
if ( A ) return x; // x is some valid return value
if ( B ) return y; // y is some valid return value
throw error; // should never reach here, right?
}

There should be absolutely no reason I’d get the error. Yet, I was. And once I got the error, I praised myself for having kept an “open mind” and actually anticipating that the impossible could happen. I enchanted myself with the discovery I was about to make which allowed this execution flow and rewarded myself with a subway cookie.

A while later, after banging my head around trying the complex conditions A and B by hand, I realized that it was impossible for this impossible thing to happen - as any first year undergrad could tell you. I’m doing a PhD and have to keep an open mind and let my brains leak out. That’s my excuse.

What happened was pretty simple. Master Yoda would put it this way: “Putting way too many error messages leads to lost time. Lost time leads to hurrying up your code. Hurrying up your code leads to copying and pasting aforementioned error messages. That leads to confusion about which error message you’re seeing. Confusion leads to suffering. Mmmm?” I sure found that out, after trying for an hour to debug code that was completely and obviously correct.

After this episode, which was actually the least of my worries (ever heard of segmentation faults?), I was wondering why it felt like I’d gone around in a circle a hundred or so times. And that’s when the universe, in a cruel stroke of brilliance, responded through subversion:

$ svn commit
Committed revision 314.

Just one of those days.

programming
tech

Comments (1)

Permalink

TiddlyWiki hack - blog plugin

I use TiddlyWiki to maintain a journal about my daily research work. It serves very well because all the journal data is kept in a single html file, which is viewable on any platform using Firefox - hence it’s far easier to put in version control.

Anyhow, one thing was lacking - I wanted a blog-like (time-based) view when I opened my tiddlywiki file, for easy parsing of previous posts (usually, the previous day’s work). A kind soul had written a plugin for it (here) - but it unfortunately it doesn’t work with the current version. So I decided to see how rusty my Javascript was (very rusty, as it turns out), and after an hour or so of hair pulling, I finally fixed it to get what I believe is a working plugin. Grab it from here.

[Update 09-Jan-2008]
Emmanuel has been kind enough to add restricted tag support, so you can, for example, only apply the blog-like behavior to entries that have a certain tag. Thanks!

blogs
programming
tech

Comments (9)

Permalink

Thought for the day

Software bugs cause you to believe in a higher power… that likes to mess with your life.

programming
research

Comments (1)

Permalink

Caught, time and again

Is the solution to this apparent to you?

Given this piece of C++ code:

  // definitions (a and b are primitive types)
  a = b+1;
  if (a == b+1) cout<<"True";
  else cout<<"False";

Under what conditions would the code print “False”?

The answer, when I finally get it, is obvious, but I manage to forget it every time and repeatedly fall into this trap.

programming
tech

Comments (2)

Permalink

Playing God with Scheme

Pretty sensitive issue this - whether man should be allowed to play God or not. I was a debater once, and I’ve pretty much debated this with dozens of arguments from each side. One thing you can’t argue with, though: playing God is fun.

I was playing around with Scheme today (for the uninitiated and/or the young, that is a programming language). I was pretty uninitiated too, which is why I had never bothered to try out Scheme before… but this is the closest to playing God that I have come. Here’s why.

Weird but cool syntax: Almost everything in scheme feels like it must have been created around the beginning of time. Scheme is to C++ what Sanskrit is to Hindi. For instance, here’s how you add two numbers in Scheme:

(+ 2 3)
5

This seems simple enough to follow at first - but then you realize everything in Scheme follows this rule - and so if you want to write an absolute value function, this is how you do it:

(define (abs x) (if (< x 0) (- x) x))

It all goes downhill from there.

Warp the world: The scheme book I was reading unwisely told me that anything and everything in Scheme was changeable. I did not read a sentence further and went ahead and wrote this out:

(define (+ a b) (* a b))
Setting compiled read-only variable+ can yield to incoherent state
+

This basically tells the interpreter that I want addition to mean multiplication. Despite the warning that I was entering a demented world, I was overjoyed that this appeared to work. And, sure enough:

(+ 5 10)
50

Bending the fabric of reality: Upward to enlightenment, I realized that + was just a name, and could really point to anything. We often hear these days that “everything is an object”. But really, in Scheme, “everything is what you want it to be”. And so, after turning my world upside-down, I turned it inside out.

(define + 2)
Setting compiled read-only variable+ can yield to incoherent state
+

Which says I want + to be a variable with value 2. For an ideal God, of course, the world would just carry out orders without cursing, but hey, I was just playing God! And was indeed bending the world to my will. And so I can now enter this, which looks positively alien, but is perfectly logical and returns a nice round figure:

(* + (- + +) (/ (* + +) +))
0

By this time, of course, subjects of the now-warped-and-bent-beyond-recognition world were, as you can imagine, feeling like Neanderthal man presented with the supernova of the Crab Nebula (or bash users presented with the Windows command line), and I thought it was about time to un-warp the world again. Even without restarting the interpreter, this is quite easily done:

(define (+ a b) (- a (- b)))

…and there was light :D (though of course the original + procedure can take any number of arguments, I’ll figure that out someday)

If you’re feeling down, I heartily recommend an episode of playing God to get you feeling on top of the world.

Uncategorized
fun
programming

Comments (5)

Permalink

How great minds really think

One blogger out there had the amazing idea of e-mailing a few great programmers and asking questions related to coding. He’s posted the answers, and they’re definitely worth a read, and probably a lot of thought as well.

philosophy
programming
tech

Comments (0)

Permalink

Thunderbird, etc.

I have been looking to a way to link Thunderbird and Growl for showing me mail notification messages for a while now. Thanks to this post, I was able to get it working today.

Growl for thunderbird

One issue I faced that I have four mail accounts in Thunderbird and I don’t care about three of them, but by default the above hack gives a notification for all messages. Thankfully, the back end controlling the notification is a good old bash shell script. After a little hacking, all was well.

To digress a bit, I would like to propose the following theorem.

You will always need to code a shell script right after you’ve forgotten the syntax of the if and for statements.

Proof: Emprical story of my life since I wrote my first shell script.

Thankfully, there is a corollary:

Every possible shell script in bash has already been written (and indexed by Google).

Proof: See above.

Back to Thunderbird. Thunderbird has had Junk mail filtering for a while now, and occasionally it will interpret some messages as “Junk” and give you a button to indicate that it’s “Not Junk”. Today, I saw this:

Thunderbird scam protection?

Interesting to note that there’s a difference between “scam” and “junk” mail detection. Even more interesting to know that Thunderbird doesn’t particularly care much about NUS Business School!

fun
mac
programming
tech

Comments (2)

Permalink

Review: Getting Real - by 37signals

This is an excellent book about building a web application. Quite different though, from most books you’ll find.

It is (as far as I know) an online-only purchase, you pay and get a PDF. The folks who wrote it are 37signals, a company that makes web-based enterprise management applications. I know them mostly from their excellent Signal vs Noise blog, and from their almost unbelievably simple free app, TaDaLists.

I finished my first reading of the book in less than two days. Although it’s decently long at 170+ pages, these guys don’t beat about the bush. In fact, the whole book feels more like a TeXpoint slideshow than a real book (it doesn’t even have a cover image). Because of this, their point comes through and makes much more of an impact - to me, at least. Flowery language is fine for fiction, but for a non-fiction book I prefer to read stuff that gets to the point and states it in simple, clear language. This book does a great job of it.

The essence of this book is keeping software (and software development) simple. That’s whats worked for them and they make a really strong case out of it. The point that made an impact was - know your priorities and do them well, and don’t worry about the rest. Unlike a lot of other KISS (for those who don’t know thats Keep It Simple, Stupid) approaches, they actually talk about what “simple” means, and what should be a priority at different stages of development - planning, design, coding, support etc. Their approach in the book is to state their point, explain it briefly, and give a lot of quotes from people about the topic. They also talk a lot about the business process - finance, customer relationships etc. While I don’t have any experience with this, their ideas seem to make a lot of sense.

Surprisingly, a lot of the ideas in the book seemed natural and familiar, and I realized that’s because the way software development is done in academic research is similar to what they advocate. We don’t generate large quantities of requirements or specifications; there are very little “extra” features except fulfilling the main goal; we keep the software small so it can be changed easily (and it often is); and most importantly, we code stuff that we care about and are interested in. In this sense, the ideas of simplicity in the book were not exactly new, but to me its an important validation that the same things can work even in the corporate world.

A final point. They say at the beginning of the book that the strategies they talk about can be applied to a lot of other activities and projects as well, and I wholeheartedly agree. After reading the book, I could very well imagine that any task that needed a systematic approach could benefit from the ideas in Getting Real. An excellent book if you’re thinking of building a web application and a wonderful read otherwise as well.

books
programming
reviews
tech

Comments (0)

Permalink

Everything isn’t an object

My introduction to the world of programming was via BASIC, C and C++. Along the way of course, I learned other stuff, including Java with its common theme of “Everything is an object”.

I could never get this concept of everything being an object, and hence I’ve never liked, and have always struggled with Java. That’s always made me feel totally dumb - given all the hype and oomph that Java receives for its features and ease of use, I could really never figure out Java totally.

It turns out, I’m not the only one. I came across this article by Steve Yegge entitled Execution in the Kingdom of Nouns. A totally humourous yet amazingly insightful take on “everything is an object”. I’d say its a must read for any serious programmer (and I’m sure the rest have run away by now) - and what it did for me basically was tell me why I felt the way I do about Java. Most memorable quotes for me:

Object Oriented Programming puts the Nouns first and foremost. Why would you go to such lengths to put one part of speech on a pedestal? Why should one kind of concept take precedence over another? It’s not as if OOP has suddenly made verbs less important in the way we actually think. It’s a strangely skewed perspective. As my friend Jacob Gabrielson once put it, advocating Object-Oriented Programming is like advocating Pants-Oriented Clothing.

I’ve really come around to what Perl folks were telling me 8 or 9 years ago: “Dude, not everything is an object.”

I found Steve’s other articles are very insightful too - especially the one on “Moore’s Law is Crap“.

programming
research
tech

Comments (2)

Permalink

Don’t mess with my net connection!

Alert: Technical rant coming up.

They say Hell hath no fury like a woman scorned, but I don’t think they compared it with a CS postgrad who had his internet connection summarily disabled by the network administrator. That too, for no good reason.

Fedora Core 5 Test release 3 has just been released, and I was looking around the Fedora mirrors to download it. The problem with FC is that not all mirrors keep test releases, and even when they do, they keep CD images. While installing every new test release of Fedora is my idea of fun, the idea of burning 5 CDs every time is not. So I was browsing through a number of ftp site directories to see if any had the DVD image and suddenly, without any warning, out of the blue, I get the SMOD - the security mail of death from the network people. In short, it says, “Your computer has been administratively disabled due to excessive TCP port scanning”. I couldn’t believe it. I’m running FC4 and running absolutely nothing beyond the usual programs. I’ve received idiotic security warnings before, and it’ll take me more than the sysadmins to make me believe I’ve got a worm on my machine.

So I go down to the helpdesk. There I am made to wait while the network guy pulls up my records and shows me Exhibit A - my computer, apparently had been trying to establish connections on multiple ports on some servers within a short span of time (for each server, I could see around 5-6 TCP ports on which connections were established rather quickly), registering it as a TCP portscan offense. I asked the admin guy to lookup the IP addresses - they turned out to be all Fedora mirrors. I assured him I had been doing nothing other than looking around for the DVD image. He was still suspicious but enabled my computer again. Phew.

Now I don’t know the first thing about networking, so I found some descriptions of the FTP protocol (courtesy AC’s, network security expertise) and found out that port 21 is only the command port of FTP, and a data port is assigned randomly for a transaction. And since it is assigned randomly, its seemed quite possible that browsing directories would result in sending packets to different ports.

So I decided to test the theory and ran tcpdump while browsing an FTP server directory. The result? Exhibit B - the partial output of tcpdump:
14:56:46.362982 IP localhost.51939 > linux.nssl.noaa.gov.ftp:
14:56:57.985858 IP localhost.51940 > linux.nssl.noaa.gov.50081:
14:56:58.796223 IP localhost.51941 > linux.nssl.noaa.gov.60070:
14:57:03.518263 IP localhost.51943 > linux.nssl.noaa.gov.56276:
14:57:04.569114 IP localhost.51944 > linux.nssl.noaa.gov.54505:
14:57:11.413372 IP localhost.51945 > linux.nssl.noaa.gov.50827:
14:57:12.437386 IP localhost.51946 > linux.nssl.noaa.gov.50061:
14:57:15.358883 IP localhost.51947 > linux.nssl.noaa.gov.51742:
14:57:16.373010 IP localhost.51948 > linux.nssl.noaa.gov.57000:

9 different TCP ports connected to in 30 seconds, only one of them (the first one) being port 21. I tried this on a different machine than the one which was disabled, and so I can be quite sure that both the systems are not infected by something. So it seems one can’t browse FTP directories without the risk of being labeled a hacker. God help us all. The sysadmins are going to hear from me.

programming
tech

Comments (1)

Permalink