I’m a CS professor. I’m pondering switching my class from Java8 to Kotlin. This post is mostly me just thinking out loud, but I’d be curious to hear what people think.
Right now, our 1st-year students learn to program in Python, with no prior assumed experience. In the two classes they have before they reach me, their programming assignments are relatively small and straightforward, so they’re less likely to run into all the ways that Python fails you (e.g., passing the wrong type as an argument and not having your program blow up until runtime). My mission has been to teach them Java as well as bootstrapping them up to larger software engineering projects, which means learning about how to decompose a project, how to do unit and coverage testing, how to think about code reuse, understanding how to use the type system to help you, etc.
My current class uses Java8 lambdas extensively to teach “real” functional programming, but the Java language forces you to learn a lot of messy stuff that makes it hard to get started. As an easy example of what I’m talking about, you can’t just start defining functions in Java. You have to know what a class is, and you have to know the difference between a static method and an instance method. For a more complicated example, Java8 lets you define code in an interface (“default methods”), which is exceptionally powerful, but you can’t override any of the core functions in
toString(), and explaining why requires understanding class extension vs. interface implementation, and that’s a Pandora’s Box that I’d like to avoid in the first few weeks.
Kotlin is obviously an attractive alternative. For example, in the first week, I might only introduce only data classes and functions, and already they can start writing useful programs. I could then roll out additional language features as necessary. So I might start introducing basic linked lists as a sealed class with a data class (
Cons) and an object (
Empty) within, then later on I could introduce lazy lists for which I could then introduce a shared interface with the eager list. Kotlin seems to be great for incrementally rolling out language features as needed without too much additional baggage.
In short, I really like the idea of moving my class from Java8 to Kotlin. But there are a handful of things that give me pause. I’d appreciate the thoughts of the people here on these topics and/or anything else.
Language levels and helpful annotations. Kotlin has a lot of powerful features, but it would be useful to be able to define subsets of the language that the compiler would enforce. For example, it would be useful to define a functional subset, where
varand setters are illegal. It would also be useful to forbid constructs that lead to confusing behaviors for beginners (e.g., getters and setters with backing fields are a minefield where you can end up writing a recursive getter if you forget to say
fieldinstead). You can imagine a variety of other places where a little bit of type annotations from the instructor could go a long way with the compiler. Simple example: say I’ve got the students implementing a list-length function. I’d like to require it to be tail recursive. Today, the only way I can do that is to have a unit test that’s designed to blow out the stack if the code isn’t tail recursive, but a
StackOverflowis less obvious to a student than a type system error saying “hey, this function is required to be tail recursive.”
Standardized formatting and style enforcement. One of the most surprisingly valuable things I did in my class was to require that the students’ Java code pass CheckStyle with zero errors or warnings. In prior years, I linked to the Google style guide, but the students were lazy, and would often submit code with annoying issues. IntelliJ’s built-in formatter is a start, but many of the things that CheckStyle will flag require more than just changing indentation, like requirements about how variable names and class names are spelled (e.g., the names of generic type parameters can be a single letter, or a capitalized word followed by a capital T).
Plagiarism detection. I’ve exchanged email with Alex Aiken, the Stanford professor who maintains the plagiarism detector that I use (Moss). It’s actually relatively easy for him to add new programming languages, since Moss is really more of an n-gram detector than an AST comparator. He’s not making any promises, but he suggested that he might be able to add Kotlin support by the fall.
Textbook resources. I don’t actually use a textbook in my class, but I do have very detailed slides, which I would indeed have to port to Kotlin. Of course, with Java, if a student wants more, they can find a book. There are literally thousands of Java textbooks out there. This leads me to ponder whether I should write a textbook, or perhaps find a close-enough Java textbook and work with the author to “port” it to Kotlin. (Note: there’s a difference between “learn how to program in Kotlin” books, of which we have a few now, and “learn how to design software, and do it in Kotlin” which is the sort of thing I have in mind.)
“Real world” training. Students are always keen to get summer internships in industry. By teaching Python and Java, they’re getting skills that immediately apply if they get a job. As it is, I get serious complaints that I ban the students from using any of the
java.utilCollections classes. Instead, we build our own from scratch, and do it with a functional design rather than Java’s mutating design. Given that this difference causes me grief, imagine how much they’ll complain if they’re learning some “other” language. Yes, everybody here knows that a lot of very real-world code is written in Kotlin these days, and it’s growing every day, but I’d clearly have to overcome some friction on this. I suppose I could dedicate the last week or two of the class to saying “hey, turns out you also know how to program in Java”. I’m not sure how well that would work.
Anyway, I appreciate feedback that any of you might have on this.