A programmer’s first language is?

My good friend and collaborator, Shawn Cornally, recently asked for my opinion on what language to use to teach programming.  He teaches science & math at Solon High School in eastern Iowa.

I’ll preface these comments by saying that choice of language is probably not as important as other things, like competence and enthusiasm of the instructor.  I started learning to program, on my own, in Apple BASIC, then QBASIC.  I can recommend neither of these, even though the B in BASIC stands for “beginner.”  Picking a poor first language is unlikely to either put off an enthusiastic student, or hinder their ability to learn better languages later.  Still, it’s worth thinking about, should you be thinking of learning to program, or are planning to teach it.

Java was the language chosen by my professor, Lynn Andrea Stein, for my first programming course at Olin, and I think it was a good one.  It has a consistent, powerful object model, and its structure encourages good practice.  And so, after a single term with Java, I was convinced.

But Java wasn’t the last language to wow me.  Several years later, a new professor (Allen Downey) arrived, and decided to teach programming in Python.  I didn’t take his course, but I did talk to him about it.  Python made a big splash across our close-knit group of engineers, and quickly became my favorite language to program in.  I’ll take the liberty to copy what he says in his course notes:

Why Python?

1) Python is a great first language

pleasant syntax

not too many exceptions

for many tasks, there is one (natural) solution, rather than an unnecessary choice

2) but it’s also powerful

can handle large programs

has lots of features and libraries

some friendly languages are limited to small, “toy” programs

By “pleasant” or “friendly” I think he means that Python syntax is often more intuitive (for English-speakers, anyway).  It’s often easier to figure out what a Python code-fragment does, even if you don’t know the language well.  This may not cause problems for professionals, who presumably do know the language well, but it’s an important consideration for beginners.  Consider the following blocks of code:

Python:

phrase = "I am Jack's Colon."
count = 0
for letter in phrase:
    if letter == "a":
        count = count + 1
print count

Java:

String phrase = "I am Jack's Colon.";
int count = 0;
for (Character letter : phrase.toCharArray() ) {
    if (letter == 'a') {
        count++;
    }
}
System.out.println(count);

The Python is just less cluttered with things which don’t have intuitive meaning (things like the “for ( : )” construction and “++,” and… you get the idea).  Don’t get me wrong, I think Java is a great language too, but Python is certainly friendlier.

(July 28th: Edited above code based on comments; thanks Christoph & tgdavis.  As other commenters have pointed out, there are more compact ways to accomplish this task in either language–but I’m just trying to explain what it means for syntax to be “friendly,” and so am trying to write an example that does the same thing in roughly the same way.)

There’s an intimidation factor involved, especially when teaching things that stretch students beyond what they know.  For beginners, convincing them that understanding programming is within their ability is important.  Programming is not difficult (at least, not at a beginner’s level), but our society is surprisingly tolerant, occasionally laudatory, of people who say things like “I can’t understand computers,” or “I’m not smart enough to do programming.”  That makes it really easy for students to give up on programming if they get too frustrated at the early stage.  I’m not saying that we need to make our courses easy—students are smart enough to see through that.  They don’t want to be spoon-fed or patronized, they want to feel comfortable… most of all capable with programming. It’s important that the path from “Hello World” to some kind of real power is as uncluttered as possible.  From that point, they’re hooked.  Or at least realize that they can’t get off the hook by claiming they’re not smart enough.

Shawn specifically asked me if I thought it was a good idea for him to teach in PHP, since wanted to teach them to build internet applications.  I think internet programming is really exciting, and a very relevant teaching ground for programming.   Doing interesting things with web pages is accessible to beginners, and lets them do things that they might find actually useful.  It’s not often that pedagogical and practical problems have as much overlap as they do in this sphere.

While I wholeheartedly endorse internet programming as a good place to do examples and assignments, I can’t really recommend PHP as a teaching language.  In the first place, I don’t think PHP enjoys any particular claim to fame as the language of choice for internet programming, and in the second I think Java and Python have strengths as general languages which make them better suited for elementary concepts and examples.  While PHP is used almost exclusively for internet programming, the reverse is definitely not true.  I’d venture to say that there will be plenty of opportunity for internet programming (especially at a beginner’s level) almost irregardless of the language chosen.

Unfortunately, PHP is comparatively less capable for more general programming tasks.  Java and Python code can be written and easily run on students own machines without the need for a webserver.  They also both have large built-in libraries of well-documented, consistently designed classes which make it easy to incorporate relatively sophisticated behaviours like graphics and network communications.  While there are plenty of PHP libraries out there, they are not all well-designed or well-documented.  Again, I don’t think PHP is a bad language–just not a good choice for teaching.

Some colleagues in my research group also point out that FORTRAN, for example, has intuitive syntax.  I can’t, however, recommend languages that are not object-oriented.  Object-oriented programming (OOP) is simply too important a paradigm in modern programming to not introduce it from the start.  It encourages good programming practice as well as scoping and modularization—all of which are fundamental concepts to writing elegant and maintainable software.

Allen Downey, the professor from Olin, has written and released textbooks for introductory programming in Java and Python.  They are free, released under GNU Free Documentation License, so you can download PDFs of the books to distribute, or print your own copy.  The Python one (Think Python) is also being published by Cambridge University Press, should you desire a hardcovers.  The Java one was updated this year, and specifically addresses the AP syllabus.

About these ads

10 Responses to “A programmer’s first language is?”

  1. Oolis Says:

    Scheme

  2. Sharma Says:

    Hey, I came here from Reddit; and one commenter pointed outdo that there’s a slightly more idiomatic way of doing that in Python:

    print sum(letter is “a” for letter in “I am Jack’s Colon.”)

    Python is easy, but it’s the idioms that really make the language look good.

  3. Carl Says:

    Also on Reddit, someone pointed out that reinventing the wheel is dumb: "I am Jack's Colon.".count("a")

  4. Christoph Hack Says:

    But there are also things you should care about in python too, even in those short programs. The identity operator “is”, isn’t right in this example, you should use “==”, which checks for equality. Otherwise you could get some bugs, which aren’t easy to locate… (e.g. 10000 is 9999+1 vs. 10000 == 9999+1)

  5. Christoph Hack Says:

    and here is an example with strings:
    >>> ‘foobar’ is ‘FOOBAR’.lower()
    False
    >>> ‘foobar’ == ‘FOOBAR’.lower()
    True

    So, please use the right operators :)

  6. Adam Says:

    or… len(filter(lambda x: x==’a’, phrase))

  7. tgdavies Says:

    Your Java doesn’t compile. Try:

    String phrase = “I am Jack’s Colon.”;
    int count = 0;
    for (Character c : phrase.toCharArray()) {
    if (c == ‘a’) {
    ++count;
    }
    }
    System.out.println(count);

  8. Spike Says:

    Ok, you got me. Too lazy to actually install a Java interpreter :)

    Sharma, Carl, & Adam: You’re right, there are easier ways involving the special features of Python (and presumably the same is true for Java code), but I want to get at the difference in the style of the syntax, and it’s hard to do that with just one line. I think the best way to make that comparison is to look at code which does the same thing in roughly the same way. Got any better examples which show this stylistic difference?

  9. Chuck Esterbrook Says:

    Another option is Cobra which combines the syntax of Python with features found in Java, C#, Objective-C and Eiffel. It has stronger error checking than Python among other advantages. See the “Comparison to Python” document on the site. Disclosure: I’m the author of Cobra (and long time Python user).

  10. Matt Says:

    I second the vote for Scheme. As a bonus, the book ‘The Structure and Interpretation of Computer Programs’ is available on-line: http://mitpress.mit.edu/sicp/full-text/book/book.html
    Once you get used to passing around functions as first-class variables any other programming language becomes easy :)


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

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: