Teaching Kotlin to college students who don't know Java


#1

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 java.lang.Object like hashCode() or 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 var and 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 field instead). 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 StackOverflow is 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.util Collections 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.


#2

I would not advise teaching kotlin to students in 2017. It is at very early adoption stage and nobody can predict its future. Kotlin is really heavily based on Java infrastructure like tooling and frameworks, no separate kotlin community exist but part of Java/Android one.

It will not at all. It has lot of small subtle differences (that’s the actual point of kotlin - fix Java) which will make students really struggle at real world code even with basic things.
Popular languages are really small compared to their ecosystems and kotlin one is JVM (well technically JS also) with Java at its core.

Current state of kotlin is “abstraction over Java for experienced Java developers” which is questionable for educational purposes.


#3

I am a UK based academic also teaching programming. Currently I’m teaching a second year module that has been spiced up and given context by getting students to do android development. Since learning Kotlin I’ve thought about its use for teaching. I’m not decided any way, and in my case there is a distinct advantage to using whatever is tought the earlier year (I don’t want students to spend too much time learning another syntax).

Overall, from a teaching perspective I have the following observations:

  • Freestanding functions can keep students away from OO before they are ready and reduce the overall level of “magic incantations”
  • Support is good, but not quite at Java levels (and still a bit rough around the edges - students can trip up on the slightest hic-up, they are poorly motivated and any issue will cause escalation)
  • There are some parts of Kotlin where Java leaks through. I’m not sure how often students would actually hit it, but it could be an issue (distracting from useful learning fighting with tools instead).
  • Lambda support in Kotlin is very good and concise without being opaque
  • Some industrial skills can be good, but it should not be the primary driver (will they learn better from Kotlin)
  • Java has way too many weak design points and other bits that for students become “magic incantations”. (bits of code they learn by rote, not gaining an understanding as to what the code actually does)
  • Data classes and final by default classes should help students learn more about structured data types without having to confuse it too much with polymorphism etc. Hopefully it disabuses students of the idea that all functions belong naturally inside objects (and you need to create an object if you don’t have one - the DoSomethinger idea).
  • I share your worries about the textbook issue.
  • If you are not teaching the module alone, you need to be very careful that your support will be able to handle the language. I had a lab demonstrator last year that was struggling with technical issues and it did not help student attitudes.
  • Frequent language changes may be problematic. From a teaching perspective you want the same language at the end of the teaching as at the beginning. When that update your version prompt comes, many students will however hapilly click yes (even just before the major coursework deadline). You need to be careful and reflect on how much you can tie down the student work environment although I think the changes between 1.0 and 1.1 are acceptable.

#4

All points of view are reasonable.

My small suggestion is to try to avoid any external dependencies, like Java stdlib or third party library, dig solutions only in Kotlin Dokka. You should evaluate “kotlinc” for compilation.

Koltin has REPL, functional and OO style, they are easy to mix and it is easy show pros and cons.

I don’t agree so much, I’m graduated in Italy and the language of my first exam was Pascal (not Turbo Pascal): no module, no library, no string.
The focus was “write a good, structured code” and I justified my stylistic choices. I don’t used Pascal never more but I don’t disapprove its choice.


#5

I don’t use Pascal these days either but, like @fvasco, I always felt that my first language - Algol 60 (a predecessor of Pascal) - gave me a good grounding in structured programming.

If Kotlin were a ‘top 10’ language, I don’t think we would be even arguing about whether it’s a suitable language for teaching purposes because, when you look at the most popular languages, is there any one of them that’s cleaner, easier to understand and seemlessly integrates the procedural, object-oriented and functional paradigms?

Perhaps C# is the closest but Kotlin already has some of the things which they’re trying to do and, unless they sacrifice backwards compatibility, their implementation will never be as good IMO.


#6

Pascal was specifically designed for teaching, It has oversimplified structure, enforced rules, almost no runtime an OS support (compared to modern situation), single way of doing almost anything. Perfect language for making simple learning problems like implementing algo from math book.

kotlin is full opposite of all above - lot of syntax sugar, requires rich and heavy runtimes, enormous and complex tools, works through over 9000 layers of abstractions all of which are heavy leaking.

My conclusion - not a good candidate for education.


#7

Certainly Kotlin has syntactic sugar, but not as much as you’d suspect. Also most of the big stuff (inline lambdas) works as expected. While Kotlin has some really great tools from a library writing perspective, you don’t have to expose students to it (and you can isolate them). The Kotlin runtime is not really large at all, and tools are not needed (but you may want to teach students with proper IDE support - do you really want to teach them that programming is tedious and that if you named something wrong you’re in a world of pain?). Sure, as Kotlin has very nice abstraction tools such as delegates, but the main educational impact is that if you don’t use them, you can avoid them.

Most of the abstraction leaks are (modest) runtime costs of the abstractions that should be JITed away and can certainly be optimized often when generating bytecode. But the thing is, you don’t really want to teach that to beginners anyway. In many ways Java has more leaky abstractions much closer to the surface. Kotlin is quite consistent in how things work.


#8

In 2014, the predecessor of my class was taught in old-school Java (using no features that weren’t there in Java5). I totally redid the curriculum in 2015 to use Java8 and incorporate functional thinking right from the beginning, which is much easier to do when you’ve got lambdas built into the language. (Java7 requires you to understand anonymous inner classes to have any hope, and you end up lost in a maze of twisty design patterns, which don’t fit as well in an introductory class.)

My first semester with Java8 was rough. My office hours were totally slammed (typically 15 students crammed into my office at a time) and I was a constant presence on the class Piazza board. When I taught it my second time, however, I hired a large number students from the previous year, and my support load dropped dramatically. So now I’ve got a pipeline.

I would expect, were I to shift to Kotlin, that “year 0” would again be exceptionally painful, but the payout would come in “year 1”. And, of course, Kotlin makes it easier to roll out the language features one by one, rather than being forced into so many “magic incantations”.

I’m not especially worried about changes in the Kotlin language itself. If I had started teaching the class in Kotlin 1.0 and then Kotlin 1.1 came along, all the old code would have continued to work without issue, and I would have immediately taken advantage of some of the new features, like type aliases.

The place where there’s an issue is making sure that the classes that follow my own can survive the disruption. Several of our other classes use Java, and it’s an important requirement for my class that students have a basic fluency with the language. Needless to say, I could unilaterally ignore my colleagues and change my class, but that’s a disservice to my students.

Instead, I’m internally shopping this around to get my colleagues on board, and I’m posting this thread here to point to the various features of Kotlin that I like or that I anticipate would be problematic for students.


#9

The trap with kotlin is that most teaching aids target those moving from java, and on that basis continually assume a strong knowledge of java. It can be very frustrating to have concepts explained in terms of java, leaving the student feeling they will need to learn java first in order to understand training materials.

There are now online some courses that teach kotlin without any prior programming knowledge.

What i found missing, is material to help those who already know a language like python to learn kotlin. I have created a few pages on this and am continuing to build on this. If such a resource is useful to you, or you would like to contribute, please ask.


#10

I’d be interested to review what you’re doing. It’s going to be at least a year before I’d be ready to try putting Kotlin into production for my students.


#11

@innov8ian - A few months ago I published a Kotlin tutorial orientated towards Python developers that you might be interested in. Note that the tutorial is already out of date and has a industrial focus instead of an academic one. Currently I am pondering whether or not to update the tutorial since there has been a very sharp spike in demand (interest) since Google announced support for Kotlin.


#12

Thanks, that is a great tutorial. It has pushed me along the path to adding a ‘resources’, and i hope you don’t mind if I add a link to your tutorial as a useful resource . The goal of what i am building is slightly different, with less focus on teaching programming concepts. What i am posting is not specifically an academic focus either, but as things progress there will be discussion of the theory and as well as how to handle code that does not easily translate from python (or other dynamics) to kotlin.


#13

I would love the feedback. I will be around 1 month before what i am building has real substance or a level of completeness, but it will continue to build and improve. You can find the start here:


#14

@innov8ian - You can link to my tutorials, that is ok. :slight_smile: