summaryrefslogtreecommitdiffstats
path: root/content/blog/2015-12-16-java-as-afirst-language/index.md
blob: a6c4cca87e032838f092b2b81a0198abf7ee33f6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
+++
title = "Why Java is not good as a first language"
lang = "en"

[taxonomies]
tags = ["programming", "java", "learning"]

[extra]
author = "harald"
+++

During the spring semester of the last year, a friend of mine was trying to
learn Java as part of her studies. I think it's great when anybody shows an
interest in programming so I of course volunteered to help her out a bit. That
was an interesting experience in many ways.

<!-- more -->

On the one hand I found it very difficult to find ways to explain in plain and
simple terms concepts that after decades of programming feel like second nature
to me. Another challenge was of course to guide her towards understanding
without giving her the answer to her excercises, especially when the email
exchanges got rapid and I sensed the frustration rise at the other end.

On the other hand though, the language itself also created some obstacles to
learning in my opinion. To understand why, let's look at the seminal "Hello,
World"-sample program:

```java
import java.lang.*;

public class Hello
{
    public static void main(String[] args)
    {
        System.out.println("Hello, world!");
    }
}
```

There's not many lines of code here, and the gist of it is not hard to
comprehend even for a beginning programmer. But there's a lot going on here
that unnecessarily obstructs the understanding of the code.

To fully understand this code, you need to be familiar with _classes_,
_objects_, _methods_, _accessibillity_, _arrays_, _packages_ and _streams_.
That's a very tall order for the first day of class. So what the teacher does
is to teach the students to ignore the stuff they haven't learned yet:

* Why do you have to write `public class Hello`? Don't worry about that, just
  copy it for now.
* What's the `public static void main`-thing? It's where you should write your
  program, just ignore it for now.
* What about the `args`? ...

And they have to. There's no way you can get around this code without ignoring
pretty much everything in it. That's bad for many reasons.

Programming is about paying attention to details, not ignoring them. So this
very way of introducing someone to programming encourages habits you have to
unlearn again as you advance. Another reason this is bad is that since the
students won't understand it, they will not be able to write it from scratch
either. This again encourages them to use crutches like editor macros for
generating code templates, or simply by copying something that works from
somewhere else.

Now contrast this to a programming language that was designed for learning:

```pascal
program Hello;

begin
    writeln('Hello, World!');
end.
```

This is the same program written in Pascal[^1]. There's essentially nothing
that even the novice programmer can not grasp here. You declare the name of the
program, and write the code of your program between the `begin` and
`end.`-statements. Output to the terminal is handled by a simple call to
`writeln`, there's no need to know about streams, functions or anything else.

Now it's easy to expand by introducing variables, loops, conditionals,
functions etc. Along each step the students should be able to follow without
having to repeat, memorize or copy incantations of unintelligible code that
just has to be there. Once the basic concepts are learned properly it should be
much easier to move to new languages, as it will now mostly amount to mapping
the same concepts to a different syntax.

It can of course be argued that "Hello world" is not a realistic example of
programs that you will write as a student. You will quickly move beyond this,
but to learn the basics the extra stuff still gets in the way. By removing the
cruft and allowing the students to build understanding one concept at the time,
they have a far better chance of learning anything at all. And there will be
less bad habits to unlearn afterwards.

Neither is it my intention to single out [Pascal] as the perfect teaching
language here. [Ruby], [Python] and perhaps even [JavaScript] are also good
candidates, as are numerous other modern languages. In fact I don't think the
language itself is all that important. The important thing is that the student
can understand the code she writes even from the very beginning. And that you
can progress from there without having to bite over too many concepts at the
time.

[^1]: This is quite likely the first Pascal code I've written in 25 years!

[Pascal]: http://freepascal.org/
[Ruby]: http://ruby-lang.org
[Python]: https://www.python.org/
[JavaScript]: http://prog21.dadgum.com/203.html