Functional game programming in Java-based CS1
Stephen Bloch, Lee Stemkoski · Journal of computing sciences in colleges · 2013
Many CS1 curricula these days use graphics and video games as motivation (e.g. those associated with Scratch/BYOB/Snap, Processing, Alice, and Media Computation in either Java or Python). A challenge faced by any such curriculum is how to simultaneously teach good principles of problem-solving and software development such as test-driven design, model/view separation, and writing functions that are short, coherent, and reusable. Interactive graphics, in particular, has been notoriously difficult to test, so most graphics-based curricula simply don't talk about automated testing. Functional programming addresses many of these problems. Treating graphics as data objects, rather than as snapshots of what's on the screen at a particular moment, makes them as testable as integers or strings. Test cases for a functional program are much better-behaved and easier to write than test cases for an imperative program, so even beginning students can and do use them as a valuable tool in the problem analysis phase, before writing code. Our functional API enforces model/view separation (at the beginning) and treats event handlers as ordinary functions, as easily testable as a payroll computation. And functional programmers have traditionally developed the habit of writing short, coherent, reusable functions -- a habit that serves them well even when writing imperatively. One of the authors has taught this material for years using a dialect of the Scheme language. However, many schools are unable or unwilling to use a language that doesn't look like C, so in 2012-2013 we ported our support library to Java and experimented with teaching the same material in a Java-based CS1 course, with good results.