Introduction
The purpose of this blog post is to explore issues related to the selection of a first programming language for CS majors. I originally started it with the intention of raising questions related to the rapid adoption of Python that is currently happening in CS departments across the US. However, I decided to make it more general to "score" a variety of different languages. I will generally restrict my comments to languages in the top 15 on RedMonk that I know people have used for introductory programming courses or which I can easily imagine people using for that purpose.
Why It Matters
There are many factors that go into why faculty and departments pick a language for CS1. Different departments and educators will weigh them all differently. The thing that I feel really is important to get out of the way is that we don't teach languages for the sake of languages. Instead, languages are a means to tell computers what we want them to do and, in that way, they are a vehicle for us to teach students the fundamental concepts that we really care about. Those concepts tend to carry across languages and time. Languages and technologies are constantly being created/updated and old ones tend to decline in usage as others replace them. These days, software development is a polyglot activity. No single language dominates the development landscape. Even the most popular of languages appear in well under 50% of the job posting for developers, so there isn't some obvious pick that is 100% essential for students to know. Contrast this with version-control systems where git is currently the clear winner. The decision of what language to use is more challenging.
So what factors should be considered when picking a language for CS1 instruction? My short list for this post is the following:
- Ability to cover important concepts.
- Enforcing good practices so students don't develop bad habits.
- Low overhead for small programs.
- Ability to solve reasonable problems without introducing too much complexity.
- Early error reporting.
- Make it easy to learn other languages.
- Ability to write things that are interesting in the first semester.
- Bonus points: be open, multi-platform, and preferably open source.
One thing that I have to point out here is that I'm specifically considering CS1 with the intent that students in CS1 intend to go on to gain a deeper knowledge of programming, software development, and computer science in general. I'm not as interested in the introduction to programming courses that are terminal or specialized for some particular application. Those courses can have very different learning outcomes that vary greatly based on the intended audience. Since I also think there can be advantages to teaching CS1 and CS2 in the same language, I'll occasionally mention things that would more likely be relevant in CS2 than CS1, but for the most part, I will focus on CS1.
Covering Key Concepts
It is critical to remember that in CS, especially the early classes, what we really want to teach are concepts. Languages are vehicles for that teaching, not the end goal. We aren't trying to teach students about Java, Python, or C++, we are trying to teach them about how to structure logic in formal grammars to solve problems in ways that experience has shown will be scalable and maintainable. Granted, CS1 isn't going to get to the point where scaling to large code bases and maintaining software in teams for years matters, but we are laying the foundation for that.
There are lots of concepts that any reasonable programming language will do a good job teaching. Things like functions, conditional execution, and iteration are found across all languages of interest. However, there are some other concepts that I think are important to teach early that not all languages are equally successful at.
Types
The notion of types is significant in all programming languages, but some languages bring it to the foreground more than others to force students to be aware of it. For the most part, this is a difference between statically typed languages and dynamically typed languages. Statically typed languages make students aware of types right of the bat. Dynamically typed languages require a little coaxing. In addition, topics like subtyping and parametric polymorphism can't really be explained or introduced at all in dynamically typed languages. Depending on when OO concepts are covered, this could be a significant omission.
The difference can manifest in subtle ways too. Consider the difference in the REPL experience between dynamically typed languages like Python and statically typed languages like Scala. In the Python REPL, executing the expression
4+5 and the statement
print(4+5) produce exactly the same result. In the Scala REPL, they produce very different results, in large part because the Scala REPL shows the types of expressions. So the response to
4+5 is something like
res0: Int = 9. This isn't just based on the typing system in the language, it relates to deeper decisions for those who implement the REPLs. For example, the JavaScript console in Google Chrome produces slightly different output for evaluating an expression and printing, but it doesn't show types by default. The creators of the Java
jshell utility also made the decision to not show types, despite being a statically typed language, and
jshell also has very different responses to the two commands. The Python REPL is particularly poor here in the decision to not distinguish at all between the two. I say this is particularly poor because I know from years of experience that one of the things students often struggle with is the difference between printing and returning values. It is useful to have a REPL that helps to make the distinction.
To make this completely clear, consider the following REPL session in Python 3.
>>> def foo1():
... return 9
...
>>> foo1()
9
>>> def foo2():
... print(9)
...
>>> foo2()
9
Now compare that to the equivalent Scala code.
scala> def foo1() = 9
foo1: ()Int
scala> foo1()
res0: Int = 9
scala> def foo2() = println(9)
foo2: ()Unit
scala> foo2
9
Which one of these do you think is going to help a student understand that there is a difference between returning a value and printing one?
Constants
This is one that I find to be significant and which
many novices might not understand. Most languages have a way to express that a variable can't change. Part of me wants to say that C++ has the edge here because the use of const is so critical to good C++ programming, but C++ also lacks immutable collections, as do most imperative languages. In that regard, functional languages might be better, but pure functional languages don't let students see non-const values. In that regard, Scala gets high marks as a language where an instructor can really explore the role of mutation.
This is an area where Python completely falls down. Making a value constant in Python is way beyond what you can do with a novice. JavaScript was bad here too until ES6 introduced const.
Scope
Speaking of the additions to JavaScript in ES6, I also feel that scope is a significant concept for programming and one that can be challenging for novice programmers to understand. Most of the languages that are commonly used have block scope. The exceptions tend to be scripting languages that were created for writing only small programs and therefore have simplified systems like only having global scope and function scope. The ECMAScript committee realized that this really isn't a good thing for a language being used to develop larger programs, so they introduced let and const basically to replace var. Unfortunately, both Python and PHP still lack block scope. Python also has another very odd behavior for default arguments that is closely related to scoping that I will come back to later.
Private
If OO is done early, I would also argue that private is a really important concept to cover. Other visibility options are helpful, but making members private is a big part of what allows OO to simplify life in large code bases because you can hide away certain details and know that certain parts of state can only be modified in a small region of code. In practice, this matters less if everything is made immutable, but in CS1 the goal is teaching concepts and if a language doesn't have a construct for doing something, it makes it a lot harder to teach.
This is an area where JavaScript has been bad. One could argue that because it was prototype based, JavaScript has always been something of an odd duck in this regard and that teaching students JavaScript to start with was going to make moving to other languages more challenging in terms of OO. However, the newest additions to JavaScript make it feel more like other class-based OO languages, including the ability to declare private members. Python is still weak here as the only notion of private is by convention. I can imagine the student questions asking if something is so important why doesn't the language take it seriously.
Discourage Bad Habits
I firmly believe that the choice of a first programming language is critical for another key reason, it is hard to break bad habits and unlearn things. This rule applies to all things, not just programming. I recall my High School physics teacher telling me that about physics. I have also experienced it myself in the area of programming both from my own learning experience and from working with students. I learned to program in line numbered BASIC with
GOTO and
if as the primary control structures. Even after moving to Pascal and C, then C++ in college, the remnants of the style I adopted from BASIC were still present for many years.
Many readers will be familiar with the following quote from Edsger Dijkstra.
It is practically impossible to teach good programming to students that have had a prior exposure to BASIC: as potential programmers they are mentally mutilated beyond hope of regeneration.
It takes a long time to break away from the habits instilled by learning to code in line-numbered BASIC. I saw this up close early in my teaching career when I had a student who had also learned how to code that way. I think it took two years to finally convince him that the style of programming he was used to was not a good one. He missed out on a lot of potential learning because he was stuck in that old style.
The thing is, this student could defend his style because he was good at that style and as a result, he could make it work. This is challenging to deal with and I certainly don't think that it is unique to this student.
Dynamic Typing
This is the area where I worry most about students picking up poor habits with modern programming languages. GOTO isn't a problem anymore as many newer languages don't support it, few examples students will stumble across on the web will use it, and because it is a language feature, students can't accidentally start using it without seeing an example. However, abusing types is something that students can easily fall into. Indeed, some languages have major libraries that basically tell students that this is okay.
The simplest example of this is probably heterogeneous collections. In dynamically typed languages, if a student has an assignment where they have to store multiple names and ages, they could easily set up something like
["Pat", 34, "Bob", 40, "Betty", 25]. Even index elements are names and the following odd index is the age. They can easily make this work, and because they can make it work, it can be very hard to convince them that this style of programming by convention leads to brittle code that is hard to maintain and is likely to break.
The challenge of convincing students that they should do the right thing with types becomes especially challenging if they see examples of what they are doing in libraries that they use in class. One behavior that I find particularly disturbing is when functions in dynamic languages return different types based on the values of the inputs. Unfortunately, this isn't all that uncommon in dynamically typed languages, even though it produces code that is nearly impossible to maintain. One example of this is the
pandas.DataFrame.xs method in the Python Pandas library, which is used extensively for data science. Statically typed languages tend to make it very clear that this type of behavior is problematic by making it painful to do if it can be done at all. As a result, students are unlikely to do it by accident and then find ways to "make it work."
Cute Tricks
Another habit that I don't want to encourage in novice programmers is "cute trick" types of solutions. These are ways of writing things that tend to produce code that can be hard to read and maintain. One example of this that jumps out to me is using short-circuit logic operators for things other than logic in languages that have "truthy" and "falsey" values instead of actual Booleans.
For example, doing something like
result = funcThatIsFalseyOnFailure() || defaultValue instead of using a conditional expression. This type of code hides what is really going on and uses the or operator to do something that clearly isn't just Boolean logic.
But I can teach it the right way...
I feel that I can already hear some readers saying that they don't need languages to help enforce good style because they will teach students to the do things the right way, and they can enforce approaches that avoid the bad habits. Unfortunately, I think that this ignores how much our students learn from sources other than the classroom. Personally, I think it is good to have students learn from other sources. Our real goal should be to set them up for life-long learning. Learning from other students and other resources should probably be viewed as a positive, but we have to acknowledge that when that happens, it is outside of our control. As such, students are going to see and probably adopt whatever a language allows, even if we don't tell them about it. The more popular the language, the more likely this is to happen.
Low Overhead for Small Programs
I've seen some debate about this, but personally, I like languages used in introductory programming classes to have low overhead for small programs. This is an area where scripting languages like Python and JavaScript shine while more traditional languages fall down. Java is particularly horrible in this regard as the traditional "Hello World" program involves a large number of keywords that you can't possibly explain to students on the first day of class. I would argue that this is why teaching environments like BlueJ and Greenfoot were created for Java. Note that I love Greenfoot and have used it for teaching non-majors, but I'd really prefer to just use a language where "Hello World" reads something like println("Hello World."). The only aspect of that which needs explaining is the double quotes for making string literals, and pretty much every programming language is going to have something like that.
I'm also fond of REPLs (Read, Evaluate, Print Loop). Here again, scripting languages shine, though even Java now has jshell. The first day I talk about a language, I like nothing better than to drop the students into a REPL and let them play around. As I mentioned above though, not all REPL experiences are created equal in terms of teaching, some provide more information to help students understand what is going on than others.
I also have to point out that scripting languages aren't the only languages that score well here. Functional languages, both statically typed and dynamically typed varieties, also tend to have both REPLs and support a style that is closer to scripting languages than to languages like Java and C++.
Hold Complexity at Bay
Limiting complexity isn't just about the first day and "Hello World". Every practical language is going to have advanced methods and dark corners. Languages that are good for CS1 don't force the instructor to introduce these before they want. The complexities that I refer to here come in a lot of forms and vary by language. Some are "concessions" that the language creators made early on to make a language more practical. Others are advanced features that benefit professional development, but only complicate life for developers. Some are just elements inherent to the programming model for that language.
I would argue that this is another area where Java falls down. One simple example fits in with the last point, every Java application requires at least one usage of the static keyword, but static is really challenging to explain to novice programmers. A slightly more subtle example is the distinction between primitives and object types in Java. The motivation for this in Java is simple, when Java was created, they needed to have primitives to make the language sufficiently performant, so they sacrificed some of the purity of the object model to put in primitive types and enable programs written in Java to run faster. They had learned from Smalltalk that making every value an object was too big of a drag on performance and would seriously hamper adoption. (I have to wonder if this might also have been a factor in the late adoption of Python, which is slightly older than Java, but didn't see broad adoption until many years after Java had become a dominant language and computers had gotten much faster.)
To understand what I mean about the problem with primitives, consider the issue of converting doubles and Strings to ints in Java. These are things that will inevitably come up in CS1. If you aren't doing this in CS1 it's probably because you have either intentionally or subconsciously created examples that avoid this. If you have double x and you need an int, you would use a C-style cast like (int)x to do the conversion. However, if you have String s and you want an int, you need to use Integer.parseInt(s) to do the conversion.
Later in the semester you probably teach students to use Java collections. Then you get to explain why they can't declare a List<int> and instead have to do List<Integer>. As an instructor, you just have to pray that no student ever tries to create a List<String>[], or an array of any generic type so you don't have to explain why they can't do that.
The thing is, these are all things that are going to come up in CS1 naturally, and they are going to slow you down if you are an instructor. They also aren't anything more than artifacts of the early history of the language. You can see this in how Scala, which is also a statically typed language that compiles to the JVM, handles these same situations. The conversions to integers are done with x.toInt and s.toInt. For some reason, I never have any trouble explaining that to students. If a student chooses to make an Array[List[Int]] there isn't a problem either. Interestingly, Scala can do this without sacrificing speed either. The reason is that Scala's compiler is smarter and it compiles thing to primitives whenever it can, but that is an implementation detail that never comes up in CS1 or CS2 and would only matter if you get to the point where you are trying to do high-performance computing.
I feel that C++ also falls down in the area of exposing complexities early, but mostly in the area of the memory model and memory handling. You really can't push C++ all that far before you are going to have to start discussing references, pointers, and memory handling. These are all things that students really need to learn and understand before they graduate, but personally, I don't want to have them take time out of my CS1 course.
Early Error Reporting
One of the fundamental rules in education is that early feedback is good. It is true for software development as well, whether you are talking about finding out if a change needs to be made to the product you are building or just an error in the code. The earlier you find out about things, the less it costs to deal with it. I believe that the same applies to how students find out about errors. Students benefit from error messages that they get early as opposed to finding out that something is wrong later on.
One of the things I like to discuss in CS1 is different types of errors. I tell them about the hierarchy of syntax, runtime, and logic errors. I point out how syntax errors are generally preferred because they get nice error messages telling them what and where the problem is while logic errors are the worst because they get very little assistance in finding those errors. As a developer, I really prefer languages and programming styles that turn as many errors as possible into syntax errors and produce as few logic errors as possible.
This is another place where dynamically typed languages, like JavaScript and Python, score poorly. Both produce very few syntax errors. Most of the errors that students are going to see will be runtime or logic errors that won't pop up until they run the code and use inputs that cause the offending path to be executed. JavaScript is particularly bad about trying to get things to work so you don't even get many runtime errors and lots of mistakes become logic errors.
In professional environments, developers deal with this by building up a suite of unit tests. While testing is something that I talk about in CS1, if I have to cover testable code and a unit testing framework to help my students do proper development in a language, then the lack of syntax errors becomes something that introduces extra unnecessary complexity early.
Ease of Learning Other Languages
Back in the mid-1990s, before Java had gained momentum, the question of what language a fresh grad was going to use in their first job was as simple as C or C++. At the time, C++ hadn't diverged nearly as much from C as it had today, so a department could easily give students a deep coverage of all the languages they were likely to use in their first job. Even with the rise of Java, a 4-year degree could be structured to introduce students to every language in significant usage.
When I look at the
most recent RedMonk language ranking I feel like my students could easily use almost any of the top 20 languages in their first job. Indeed, I have graduates working with languages well below the top 20 in their jobs. When it comes to programming, we now live in a polyglot world. We shouldn't even be trying to teach students every language they might use in their first few jobs because there are too many possibilities. Instead, we need to strive to lay a foundation where students learn a diverse set of languages that will enable them to pick up new languages quickly. Personally, I think it would be helpful if the language they learn in CS1 makes it easier for them to pick up the other languages they are going to exposed to in your program instead of harder. If the instructors in later courses are able to draw parallels between languages features in the language used in CS1 and the next few languages students learn, this can definitely facilitate learning.
So what are the properties of a language that can help students move to other languages? As far as I know, there haven't been any rigorous studies done on this topic. While that is unfortunate, I completely understand why it is the case. Large-scale educational experiments are challenging to do well and are influenced by a lot of different variables. Creating a study where language feature influence was larger than the noise would be very challenging. As such, I'm just going to throw out some ideas.
I feel that a language that leads well into other languages is one that isn't too odd in either syntax or semantics so that students can move to a variety of other languages and paradigms without being surprised by the new language. Let's start with the easy part of this, use a language that isn't too syntactically different from the ones they will learn next. Most people would likely agree that you shouldn't start in
APL or
J. I see this as an argument against Scheme/Racket as well. (I have to point out that every language will have strikes against it, and what matters is the total weight of pros and cons. For me, the dynamic typing of Scheme/Racket is a bigger killer than the syntax for the reasons listed above, but everyone places different weights on these things and there are certainly some big positives for Scheme/Racket.)
I'd even go further and argue that if a language has too many control structures that aren't seen in other places, that could be a problem as well. For example, Python supports an
else clause on loops that you won't see in other languages. The use of Boolean operators as shortcuts that I mentioned above could be seen as a fail here as well.
On the semantic side, I think that scope is again significant. I have been told by a TA from Rice University that when students get to Java in their third semester, after having two semesters of Python, that many find block scope to be confusing. Outside of Python, PHP, and Ruby, block scope is the standard rule, and given that scope is something beginning students struggle with, having a first language that handles it differently than most others seems problematic to me for future growth. Python also has very unusual handling of default parameters to functions. Consider the following code.
>>> def foo(a, b=[]):
... b.append(a)
... return b
...
>>> foo(3)
[3]
>>> foo(4)
[3, 4]
While the scope of
b is only inside of
foo, its memory is allocated at the same level as the declaration of
foo and it is remembered from one invocation to the next. I have yet to find another language that has this semantics for default values to functions.
Again on the semantic side, languages that have very different models of OO from the "norm" is also a red flag for me. JavaScript recently gained classes, but the OO model in JavaScript is still prototype based and very different from every other language in significant use today. Python also has odd elements to its OO model, like the need to list self as the first argument for methods. I can see how some might see this as a nice advantage for teaching showing how the object is available, but when the student moves to another language, it is yet one more thing to handle differently.
Lastly, I feel very strongly that before graduating, students should be exposed to multiple paradigms of programming. If a major never covers anything other than the OO-Imperative style of Java and C++, they will find it much harder to pick up other languages later that either aren't object-oriented or that focus on functional instead of imperative. For this reason, I actually like the idea of multi-paradigm languages for CS1 and CS2. Of course, one has to keep in mind that multi-paradigm can break a lot of the other things we are looking for if it makes things more complex.
Interesting First Semester Assignments
One of the reasons that Java took off in the education space was the relative ease with which students would write graphical code, especially using Applets. The Applets have long since died, but the ability to put interesting assignments into CS1 and CS2 is still very strong. Interesting here can mean many things and goes well beyond graphics today to include things like playing with data, robotics, or socially relevant problems. Regardless of the details, a general requirement is that you have strong library support, and hopefully ways of bringing in those libraries that doesn't overly complicate things. This is an area where JVM languages and Python excel. Really low-level languages, like C, tend to fall down the most here because the amount of code that is required to do anything interesting is often quite large.
Other Factors
There are a number of other factors that impact my thoughts on picking a language for introductory programming. I personally also appreciate languages that run on multiple platforms and are hopefully open source. Those languages generally are better for allowing students to work on their own machines, regardless of their OS, and have tooling that is free of cost. I also really like a language that works well for both CS1 and CS2. I discuss that in more depth in an
earlier post.
I also like to use a language and accompanying tools that are used professionally. It doesn't have to be a top 5 language. I'm fine with top 20 because the reality is that no single language dominates today, so whatever language you pick, the ability to learn other languages is more significant than knowing a specific language. The thing is, I'm not all that big on teaching languages and teaching environments in courses for CS majors. The reason is pretty simple, I only have students for 4 years and a limited number of hours and since I want to use the same language for CS1 and CS2, I don't feel comfortable spending that much time teaching something that I know isn't used anywhere outside of the classroom.
We also have to realize that the languages used in industy aren't fixed and what we choose to teach in colleges has an impact on them. One of the things people consider when they pick the language for a new project is whether they can hire enough developers of sufficient talent who know it. The more colleges that teach a language, the more likely it is to be adopted in industry. I'm quite certain that the broad adoption of Java in colleges played a major factor in its dominance in industry. Similarly, the current growth in Python is inevitably fuelling its professional adoption. So when you pick a language to teach, you might ask yourself, "Is this the language I want the software that runs my life to be written in?" If you don't think you want your bank or the elevator you ride in using that language, perhaps you should pick a different one.
Why People Choose Python
The language that seems to be taking over early CS education right now is Python. That is actually what prompted me to write this post because I worry about this particular choice for introductory language. There is no doubt that Python has some real advantages in the fact that it has low overhead in CS1 and that there is broad library support to enable students to do interesting things. Unfortunately, it falls down in other areas. The ones I worry most about are an inability to cover certain topics that I consider significant and the possibility for students to pick up bad habits that the language doesn't prevent.
Perhaps the most standard argument that I hear for Python is that it is "simple" and easy for students to learn. While I completely agree that Python is an easy language for experienced programmers to pick up, it isn't at all clear that being easy for experienced programmers to learn is the same as being easy for beginning developers to learn. Learning to program isn't just about syntax. It is about learning a new way of thinking, how to structure logic, and the general semantics of that syntax. Having a language that enforces more structure and which
gives early feedback when rules are broken could actually be very useful for novices. Some evidence for this was provided by
Alzhrhani et al. (2018) who found that students in a large data sample struggle more with Python than they do with C++. Indeed, I would argue that moving from a language that provides more error checking to one that provides less error checking is generally easier than going the other way, and that more assistance from the language to write good code is especially beneficial for the novice. Having a language that helps the student to build their mental model of the semantics of programming could actually be more significant than having a simple syntax.
I feel compelled to mention that another common argument for Python is that it teaches indentation. There is definitely truth to this, but I can't say I find this very motivating, especially since Python lacks the block scope that the indented blocks indicate. The reality is that auto-formatting tools for languages without significant whitespace are well developed and companies with strict style guides can easily have them enforced by software. In contrast to this, automatically cleaning up poor type usage in programs written for dynamically typed languages is a far more complex problem.
In some ways, the true challenge of teaching introductory programming using Python is probably summed up well by terms frequently seen in discussions of Python programs. It doesn't take much reading on Python to come across the term "Pythonic". When I asked a Python programmer about functions that return different types based on the argument values, like
xs on Pandas' Datasets, I was told that was un-Pythonic. The problem is that Pythonic and un-Pythonic are advanced concepts. When you are dealing with students who are still trying to understand the basics of conditionals, iterations, and functions, they simply aren't prepared to comprehend "Pythonic". Instead of having good coding done by convention, those students need a language that does more to enforce good style.
My Choice for CS1 and CS2
If you've gotten this far, you have already shown great perseverance in our current age of short attention spans. You might be wondering what language I would pick for teaching CS1 and CS2. I actually like Scala as the language for these courses. We've been using Scala in this role at
Trinity University for almost 10 years now, and I have very few complaints. As I've said before in this post, no language is perfect, but I find that the pros for Scala definitely outweigh the cons. I have some older blog posts (
here and
here) where I discuss this in more detail as part of my thoughts from the first ~5 years of using Scala. I'll just list some of the highlights here.
- REPL and Scripting interface for CS1.
- Static type checking and lots of syntax errors instead of runtime errors or logic errors.
- Type errors prevent a lot of issues.
- CS1 students should never see a NullPointerException.
- Syntax/semantics allow coverage of things I care about including const/mutability, block scope, OO, subtyping, parametric types, etc.
- Access to the full JVM for libraries.
- Expressive syntax that combined with libraries that allow me to give interesting assignments that don't take thousands of lines to code.
- Solid APIs that include links to the types for arguments and return values.
- In CS2 I can cover multithreading and networking.
- I can cover CS1 and CS2 topics without the language forcing me to talk about things I'm not ready to cover yet.
- Uniform OO syntax without primitives.
- Fairly standard OO model.
- Multiparadigm so students have a nice path to C++, Haskell, Java, etc.