2011-11-22

Learn Python instead of Java as your first language

I'm struggling with figuring out which programming language to teach students would provide the most effective learning experience.

It's easy to find tons of statistics showing how Java is the most popular language with tons of community and books, etc.  That, however, doesn't say very much about the learning experience of the student at all, or what the students will come away with.

I found the following from a respected hacker in the community, for example:
"For example, if your company wants to write some software, it might seem a prudent choice to write it in Java. But when you choose a language, you're also choosing a community. The programmers you'll be able to hire to work on a Java project won't be as smart as the ones you could get to work on a project written in Python. And the quality of your hackers probably matters more than the language you choose. Though, frankly, the fact that good hackers prefer Python to Java should tell you something about the relative merits of those languages.

"Business types prefer the most popular languages because they view languages as standards. They don't want to bet the company on Betamax. The thing about languages, though, is that they're not just standards. If you have to move bits over a network, by all means use TCP/IP. But a programming language isn't just a format. A programming language is a medium of expression.

"I've read that Java has just overtaken Cobol as the most popular language. As a standard, you couldn't wish for more. But as a medium of expression, you could do a lot better. Of all the great programmers I can think of, I know of only one who would voluntarily program in Java. And of all the great programmers I can think of who don't work for Sun, on Java, I know of zero."  (Great Hackers, Paul Graham, 2004)
So essentially, the idea here is that if you choose a language based on whether it's the popular "standard", you've lost sight of the fact that programming is very much an art, and the language is a medium of expression — why would anyone wish upon students the torture of a lesser blub language?

Java is becoming the new Cobol since 2007.  So of course it's popular, but we probably wouldn't want students learning Cobol as their first language now either.

The better language is Python, according to the best hackers in the community known to Graham (and he knows plenty, given what he does as his "day job" now).


Consider this, also from a respected hacker in the community:
"If you don't know any computer languages, I recommend starting with Python. It is cleanly designed, well documented, and relatively kind to beginners. Despite being a good first language, it is not just a toy; it is very powerful and flexible and well suited for large projects.

"I used to recommend Java as a good language to learn early, but this critique has changed my mind (search for The Pitfalls of Java as a First Programming Language within it). A hacker cannot, as they devastatingly put it approach problem-solving like a plumber in a hardware store; you have to know what the components actually do." (How To Become A Hacker, Eric S. Raymond, 2008)
Here the essential idea, I think, is that Java can help you build stuff, but it doesn't help you very much in learning to think and express your thoughts programmatically.  Being good at putting together code in Java that uses what other people have written doesn't help you solve problems better in a hacking or engineering kind of way.


It gets worse for Java according to this critique, as ESR mentioned in the quote above, which details failings of teaching Java at the University undergrad level.

But I am interested in this question in terms of high school students.  The difference for high schools is that they may offer an AP or IB program, which may mandate the use of Java for their exams.  In that case, of course for the sake of writing an exam, Java must be taught.

Looking longer term, however, teaching Java as a first language in high school raises lots of questions (some already answered above).



When you teach a grade 10 student, remember that this student is only about 14 or 15 years old.  Three years of high school and four years of undergrad later, the student might be entering the job market seven years after learning their first programming language.  That's seven years!  Who knows what the world will become by then in the computing industry.

All we can say is what we can see in front of us:
  • Native programs on Windows will increasingly be written in C# and C++/CLR.
  • Native programs on Mac OS X will continue to be written in Objective-C
  • Native Apps on iOS for iPads, iPods, and iPhones, will continue to be written in Objective-C.
  • Web development in an Ajax style with the LAMP stack, especially with the continual support for HTML5 in browsers, will continue to stress the need for JavaScript, HTML, CSS, and one of Perl, PHP, Python, or Ruby.
  • Apps for iOS or other tablets or smart phones written using web development technologies will probably continue to be popular, and would use the same mix of JavaScript, HTML, etc. as web development.
  • Java is popular in the so-called "enterprise" space, and also for Android OS for tablets and smart phones.  But note that Android is a Linux OS (it's not GNU/Linux, but it is using the Linux kernel), so the kernel is programmed in C/C++, and you can program Apps for Android in most languages now, including: C/C++, Python, and Ruby.  In fact, Android technically doesn't even run a Java VM, as Java is just a language used to program for the Dalvik VM — an issue Google has had to defend in court against Oracle.
From that perspective, popularity of Java is irrelevant.  If a student wants to develop for Macs, they need to know Objective-C.  If a student wants to develop for Windows, they need to know C# or C++.  For Web, they need JavaScript, and possibly one other language (which could be Java, but there are many options here, including JavaScript too, by the way).  For Android, there are many options to choose from.

All in all, it's almost like learning C is the best as a first language if career or "real-world" is at issue!

But that ignores the learning involved in a first language.  Students need to learn to think in a way that can be expressed easily with pseudo-code — that's really the most important thing to learn as a beginning computer science student.

As Peter Norvig found, Python was much closer to pseudo-code than Java (and in fact, he found his attempt at re-writing AIMA examples from Lisp to Java to be "largely unsuccessful. Java was too verbose, and the differences between the pseudocode in the book and the Java code was too large").

With the above information at hand, I think it's clear that the choice of Java as a first language is simply not the most effective.  As for what language to teach instead?  Well, there's Python.  MIT chose Python as their first language to teach undergrads — if it's good enough for MIT, it's good enough for me!

There's also JavaScript, if students want to do something with the web browser.  I hear that Khan Academy has chosen to redo their Computer Science videos with JavaScript as the language, but that's just hear-say.

Addendum: Not long after I wrote this, I read Bad code plagues business applications, especially Java ones, where it discusses a study that found Java Enterprise Edition applications have the greatest number of problems in terms of bad coding practices that affect stability, and performance.  Should this news affect how we view Java in terms of what to teach students?

5 comments:

mircea said...

I like this post. Excuse me my english, i am from Romania. I learn first some php an mysql, then some Java. Yes, i think Java is not a good choice for learn programming. I discover about a year this magnificient language called Python and i'm delighted...Sytax is very simple, the rules clear, compile the script very easy..This program in my opinion is the first choice for a beginner programer. Thank you for this post...

Unknown said...

Excellent post which was really useful in helping me to decide whether to go the Java or Python route.
Thanks
Andrew

Unknown said...

Thanks for the great post. I was torn between learning Java and Python asy very first language. I really wanted to learn java to program for Android, but after reading this post I am glad to hear Python works great for Android as well. I have already began learning python and was just needing to know if I made the right decision.

Unknown said...

First, I don't know Python. I started on Pascal, and touched on Delphi, C, C++, PHP and Javascript. Now I'm finally going back to school and I'm forced to use Java (ugh).

My 2 cents: I think people should start on C. Forcing people into an OOP paradigm from the get-go (Java) seems ridiculous. Going from Pascal to Delphi was cake... I don't see why you couldn't start with C and step up to OOP with C++, and the beauty would be that, unlike starting with Pascal, you'd have a solid familiarity with C syntax which will make it ever-so-easy to transition to PHP, Javascript, etc.

I wish I could go back in time and start with years of C programming instead of years of Pascal programming. Like a first spoken language, I'm quite fluent in Pascal, but I'm always having to look things up in C/C++.

Unknown said...

Thank you, I even followed your link to the outside article on pitfalls of JAVA as a first language.

I am a busy DJ, and being in the social scene does not mix with my interests in computer science / hacking - however I am determined to learn a decent skill set over the next rest of my lifetime.

Your article and some other advice has convinced me to look more honestly at Python, as it will relate to use in Kali Linux pentesting platform.

I am interested in being able to master that pesky Android tablet and its myriad inconsistencies and problems - such as the infamous default download write location.

Thanks again!