Thinking in another (programming) language

With the semester coming to a close, I finally have time to focus on a programming language other than C or assembly. Machine-level programming has been a great experience, and while I’m definitely a better programmer because of it I wouldn’t expect someone to notice. Lots of professional code isn’t written in C, for the same reasons restaurant chefs don’t farm their own wheat. If you want to get something done, you can’t bother with every detail.

For example, one of the limitations of C (and C++ and Java) is that functions can return at most one thing. The ugly solution is to return a struct, which carries with it considerable overhead. The struct (basically a box to put everything else in, and the box is the one “thing”) must be declared and documented ahead of time. Over time, one learns to code in such a way where it is not necessary to return multiple items. If one absolutely had to, the code is:

struct box{int a, b, c};
//Dozens of lines later...
box mybox = returnThreeValues(); //Pack the box
int a = mybox.a; //Unpack the box, one thing at a time
int b = mybox.b;
int c = mybox.c;

But Python has a feature called tuples, which means that the packing and unpacking happens automatically. It opens the door for all sorts of new functions, like:

a, b, c = returnThreeValues()

It’s more than just a convenience of syntax. Programming in differently languages means thinking in a different language.

My final project in was to create an RPN calculator in assembly language, which is also the first project in Stanford’s course on Objective-C for iOS. Even though it’s the same functionality, the code is very different in both syntax and semantics.

As I progress through Project Euler, I am concerned whether I am actually writing idiomatic Python, or merely C in Python syntax. I don’t think so – I’m exercising my newfound power to iterate through anything with ease. It’s really freeing not to worry about memory management and type conversions. Don’t get me wrong, I’m not shying away from working in more industrial languages. Yes, learning about sorting algorithms is cool, but so is a magical sort() function that orders numbers and alphabetizes strings. It’s great to understand how it works, but know that I do its great to have something that just works. I finally fully appreciate these xkcd comics.

And next semester (spring 2012) it gets even better. I’m taking an undergraduate course on programming languages, which promises to look into the design and theory of unusual languages. I’ve been told I’ll stare at a problem for an hour, have a flash of insight, and write two lines of code that do more than I ever thought two lines of code could do. These languages were designed to be powerful yet elegant. Unlike human languages, which evolve naturally, computer scientists get to ask “what makes a good programming language?”. What is the best way for humans and computers to talk to each other?

Actually, that’s a two part question. The study of best way for computers and programmers to talk to each other is programming languages. The best way for computers and users to talk is called Human-Computer Interaction (HCI), which is another course I’m taking. The other two are networks and game development. Four compsci courses, and I couldn’t be more excited.

Now, to take finals for three of this semester’s courses…

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: