2

Java allows this:

class X{
    int i,j[]; // j is an array, i is not
}

and even worse, it allows this:

class X{
    int foo(String bar)[][][] // foo actually returns int[][][]
    { return null; } 
}

Okay, the reason for this might be that it was lent from C/C++. However, Java meant to be easier than C/C++. Why did the Java inventors decide to allow this hard-to-read construct. The convoluted types of C where the variable name is in the middle of the type are just hard to read and provoke programming errors.

Especially the brackets behind the method signature. I have never seen these in use and that is for a good reason. No one looks behind the signature when checking the return type of a method. While the first example may save some keystrokes (because int does not have to be written twice), the brackets behind the signature do not even save any, so I see absolutely no gain here.

So is there a good reason for this (especially the second one) that I am missing?

gexicide
  • 129
  • 4
  • 7
    This would be a much better question with the rant edited out. – Blrfl May 12 '14 at 20:21
  • 8
    If this is the worst idea in language design you have seen in decades, you either have a serious problem with hyperbole or you've not spent a lot of time on language design. –  May 12 '14 at 20:21
  • 2
    @Blrfl: True, I have edited it out ;). I was just writing a Java parser and that stuff ruined my nice grammar, so I was indeed quite upset. – gexicide May 12 '14 at 20:34
  • 1
    Java is part of the C ghetto. That's the answer to a lot of the mindbogglingly stupid things Java allows. – Jonathan E. Landrum May 12 '14 at 20:51
  • If your grammar doesn't conform the the spec, it's not so nice. – Blrfl May 12 '14 at 21:13
  • 1
    "A Wolf in Sheeps Clothing: C syntax to make developers comfortable" ([from the horse's mouth](http://programmers.stackexchange.com/a/157606/31260 "Gosling: 'How The JVM Spec Came To Be'")) – gnat May 12 '14 at 21:22
  • Since you'd typically use array indexes as `foo[i]`, I'd argue declaring arrays as `int foo[]` makes a hell of a lot more sense than other ways around. It's also entirely clear which variable is an array, while `int[] foo, bar` is not. (Is it just `foo` or are both of them arrays?) – Izkata May 13 '14 at 03:26
  • 1
    @Izkata That idea is called "declaration mirrors usage" and it's the reasoning behind C's declaration syntax. But decades of experience have shown it to not work nearly as well in practice. It doesn't scale well to more complex types, it clashes with the (quite reasonable and common) mental model that a declaration is just `type name, name, ...;` and when you have prefix "type operators" like `*` in C, you need operator precedence for them which increases ambiguity (`int *is[]` vs `int (*ps)[]`). –  May 13 '14 at 11:47

1 Answers1

3

The question can be divided into "Why does Java C support brackets behind variables and even behind method signatures?" and "Why would Java inherit so much from C?"; Java also inherited other weird syntax, especially that for switch (why not use curly braces as for everything else and use : and break ?).

To answer the first question - I believe back in the 70s in language design they didn't focus that much on readability but rather on performance. Back then programs weren't that complex or large so readability wasn't a main concern.

To answer the second question - back in the middle '90s when Java appeared most programmers worked in C and C++; C++ had a large success because it was a superset of C by adding OOP. Java simplified C++ as much as possible but tried to keep it close as syntax as much as as possible so that programmers can transit more easily from a language to another.

I actually believe that most Java programmers aren't even aware the code presented is a valid one (at least the second one) and hence do not use it when writing. The main thing that lead to this is a great thing called - Java Code Conventions which is most likely to be read by programmers than the language specification itself. Also this kind of stuff is a disadvantage of backwards compatibility.

Random42
  • 10,370
  • 10
  • 48
  • 65
  • 1
    How does this syntax improve performance? – FrustratedWithFormsDesigner May 12 '14 at 21:22
  • *back in the 70s in language design they didn't focus that much on readability* This is just plain wrong. The syntax of B and C were a response to the verbose syntax of Cobol and Algol. – andy256 May 12 '14 at 23:38
  • @FrustratedWithFormsDesigner I said that they didn't focus on readability but on performance; I don't know by which inference you deduced from what I said that this particular syntax improves performance. – Random42 May 13 '14 at 06:18
  • @andy256 In B and C they reduced verbosity thus readability by writing less code - thus less memory occupied by code. – Random42 May 13 '14 at 06:22
  • 2
    @andy256 Cobol is highly readable, far more so for people with no strong background in mathematics than is C. Don't know Algol. – jwenting May 13 '14 at 06:47
  • @m3th0dman verbosity is unrelated to memory use for a compiled language. – andy256 May 13 '14 at 11:40
  • @jwenting yes it is (at least in the procedure division). I was responding to the erroneous claim that readability was not a focus then. Brevity was a way of improving readability. – andy256 May 13 '14 at 11:43
  • @andy256 and for many people utterly failed. Recall the obfuscated C programming contest series? Not sure if they're still going on. – jwenting May 13 '14 at 13:25
  • @jwenting One can write unreadable code in any language. – andy256 May 13 '14 at 13:46