Monday, June 11, 2007

I was looking around for suggestions on first programming languages, and I noticed that many people tend to look at this as 'what language is most useful'. If you want to program applications, learn C, C++, or Java (no!) whereas if you want to do web development, learn Perl, Python, Ruby, or PHP (no!). And, yes, I do this too, but I do this for people who want to learn programming because they think it'd be cool to write a Neopets clone or something. Not people who want to be programmers.

Here's the thing: if you're a programmer, you know more than one language.

So why does it matter which language you learn first? Because learning some languages will encourage correct thinking and good programming habits right from the get-go, and that's what you need to be learning. One of my professors, Dr. James Wirth, told us 'It's not that it's a foreign language; it's a foreign way of thinking.' And he's exactly right. I've written small quantities of code in something like twenty different languages. I'm only proficient in a few, but picking up a new programming language is not the obstacle in becoming a good programmer. In one of my courses, we had assignments in something like six different languages, and code snippets littered the blackboard in many more. I haven't programmed in Ada or COBOL or Fortran or Pascal, but I had learn to read them on the fly, and I didn't find it a challenge. This might sound hard if you've never done any programming, but once you've learned C, Assembly, Prolog, and Scheme, you can understand the basics of nearly any language. Trust me: this is something you can do. After you learn to program.

Learning to program is the key. This starts with understanding how the computer runs programs. One guy I knew in school tried the non-command 'hurry' to tell the computer to run his program quickly. When you've never done a lick of programming before, this is not an unusual way of thinking about it. But any language will teach you that. What else is there?

When I first started, I was writing spaghetti code in QBasic. What was I missing? Functions and scope. Once I learned about using functions (and more importantly, once I began putting more and more stuff in functions), I started to think like a programmer. Suddenly programming wasn't rigid. Callbacks a long, long while later brought me even further along. We want a language that encourages the new programmer to build tools to do his work for him. What languages do this? I think LISP and dialects may be the champs her.

What else do beginning programmers desperately need? Style/Formatting. There were even upperclassmen in the CS department who didn't indent. (For the record, they began with C/C++, Java, or Dr. Wirth's Box English (broken link)) Everyone knows which language teaches indentation: Python. Worst might be Perl. Few will allow the programmer to ignore more style guidelines.

It's basic and of debatable importance, and probably should be covered separately if you're in a CS program, but How Things Work is also relevant. By this I mean, for example, understanding variable, something Dr. Wirth's Box English seems to focus on at a high level. Understanding overflows. Understanding data types. Should a beginning programmer be forced to learn the difference between and int and a string? Personally, I love not having to treat strings as char*s, but they'll need to learn sometime. Additionally, it will come up from time to time no matter what language you use. Try adding '3' and '8' in ECMASript. (Perl thinks it's 11. ECMAScript, Python, and Ruby all say it's '38' because '+' means concatenate. C thinks it's k or 107, depending on whether you ask for an int or a char output. A beginner will think Perl makes the most sense, but the most intuitive answer for a wizened programmer put before a language he doesn't know should be the C answer.) What language will teach you the nittiest of the nitty-gritty? Well, I guess machine language or Assembly. But shout-out is due to C for being a usable language that manages to force low-level understanding as well. Whether that's a good thing or a bad thing for beginners, of course, is something we could argue over all day and night.

And, let's face it: if you're trying to learn to program, learning a language at the same time just complicates things. Simplicity, please. I was using Perl for a while before I learned when it's $, %, @, $#, ${%var}, $#@var, \$, \%, \@, $var->{..., and even worse was (is) [] or {} or (). Let's see...a hash is () and a hash reference is {} and a list is () and an array is () but an array reference is [], and a list of references is \() or (\a, \b...). I love Perl, but this would frustrate beginners more than anything. When it comes to a simple, consistent syntax, Scheme obviously ranks way up there. I think Python and Ruby also score quite well. Since I mentioned QBasic earlier, I may as well do so again: QBasic has a very simple syntax. The case-insensitivity would probably go over well, though perhaps not something we should be teaching young programmers.

I started out talking about this, but I think it deserves a broader look: Learning to think. Leaving spaghetti code behind was a major boon, but that's just part of it.

The winner: There is no definite winner, of course. Python, I think, fairs pretty well. Scheme for teaching good thought is a classic answer (though usually just under the guise of LISP in general), and with the added benefit of a simple, clean syntax, is a very important consideration. If you want deep understanding of the computer rather than style and the more abstract 'think like a programmer', C or C++ is an excellent choice.

Personally, I think I'd go for Python to begin with, partly because it's popular. There's a lot to say for an active community when you're learning a new language.
The nitty-gritty can be gotten later (you should learn Assembly at some point anyway, and that's nothing but nitty-gritty), and Scheme also later on in order to broaden the way of thinking. A good programmer needs to touch on functional programming, anyway, and perhaps logical programming with Prolog as well. Meh. I had a bad experience with Prolog. The darn thing insisted I had some syntax error and I couldn't finish the simple assignment. :-(

No comments: