Braces Considered Harmful ========================= Nick Ourosoff argues that Java's primitive types are harmful to our efforts to teach the object-oriented paradigm, owing to the fact that one of the most common operations, evaluating expressions, is rendered non-object-oriented [Comms. Aug.2002]. Oruosoff claims that a pure OO ontology, having only objects, would be obviously simpler, and so more likely to be correct and teachable, than Java's dualistic ontology of objects and primitive types. Simpler it would be. Correct, perhaps, but teachable, I don't think so! Students who have spent most of their lives writing out mathematical formula like 2+3*6 are hardly likely to find the object syntax 2.+( 3.*(6) ) or explanations in terms of passing a message *(6) to the number 3, very natural. Java is a pragmatic (commercial) language. It was designed so that people, especially professional C programmers, could learn it quickly. Compared to many other languages it is indeed very clean and consistent, and pretty much enforces an object-oriented style. This is why it caught on so rapidly and has been so successful. Contrast Java's popularity with that of SmallTalk, a pure OO language. I am reminded of a cartoon that circulated some years back. It depicted attempts to compute two plus two in various programming languages. In Basic it was trival, in Pascal, almost so. In Lisp it took a few attempts (presumably to match the brackets correctly), while in SmallTalk it took about ten tries to get it right, simply because of the unnaturalness of the expression. The first electronic calculator (from HP) required users to enter formula in reverse polish (postfix) notation. Less than six months later, when Texas Instruments came into the market with a machine featuring data input in common infix notation, the fate of postfix calculators, were sealed forever. And what about the numerous attempts to change the QWERTY keyboard? Psychological inertia beats elegance everytime. It is simply much easier for a learner to add to their knowledge structures than to rewire them completely! Those who want to adopt a pure object-first introduction to programming could use SmallTalk, but there would undoubtedly be complaints about having to learn a non-commercial language! Alternatively, they could continue to use Java, but without initially showing students the primitive types. Admitedly, a textbook that took this approach would help, although I suspect that trying to do arithmetic this way is still going to meet with considerable resistance! Indeed, I see this as an indication of a more general problem with a pure object-first approach to teaching programming. I thus favour a more pragmatic approach since students (at least CS students) will need to learn non-object approaches sooner or later anyway. While primitive types may be harmful to Java's elegance, I believe the real dangers lie elsewhere, in two simple yet stupid idiosynchrasies and in its braces. The first problem is Java's use of the double equals in conditional statements when a single equals is so much more natural. This would not be such a problem were it not for the compiler's acceptance of the single equal sign (meaning assignment) in conditionals too. I am certain everyone has fallen into this trap (which is common to other languages too!) My second niggle concerns the evaluation of double z = a/b; which gives different results depending upon the types of a and b. If they are both integer the result is truncated, i.e. 3/2 gives 1, whereas if one or both are real the result is 1.5. This potentially dangerous behaviour passes the compiler without a peep (surely one for the risks column!) But my real pet hate, in my opinion the single most harmful aspect of Java (in common with almost every other programming language), is its braces! OK, I understand it had to look like C. I grudgingly accept the confusing do-while instead of repeat-until, the ridiculous lack of a then in the if statement, even the double equals, but braces, I ask you! As an instructor of an introductory programming class it is my task to persuade students to produce maintainable (i.e. nicely laid out, readable) code. This demands blank lines and appropriate indentation. It also requires braces that match! It would be so easy, surely, to have the language (compiler) restrict code to one statement per line and have the visual structure determine the program logic. Instead of which we are free to write incomprehensible code, all on a single line or perhaps "neatly" aligned on the left margin, and to waste hours chasing mismatched braces. Why? When will we be rid of these monstrous braces (begin/ends, etc.)? Enough. Computers are supposed to make our lives easier. Braces are much more harmful than primitive types. David Davenport Computer Engineering Dept., Bilkent University. david@bilkent.edu.tr