Lambda the Ultimate

inactiveTopic Java's new Considered Harmful
started 3/2/2002; 10:58:44 AM - last post 3/6/2002; 11:35:17 AM
Chris Rathman - Java's new Considered Harmful  blueArrow
3/2/2002; 10:58:44 AM (reads: 2332, responses: 6)
Java's new Considered Harmful
The author tries to make the case that the standard Java object allocation method (new) is broken. The premise is twofold: First, the new operator is tied to one specific heap allocation, at the exclusion of any other possible memory pools. Second, the new Operator is non-polymorphic in that it requires the call to be tied to a specific class - thus preventing the code from being reused for subclasses of the instantiated object.

There is a difference between requesting an object and creating one. The first is an abstraction; it should be designed into a class and should be under the class's control. The second is a low-level implementation detail. Java's new keyword, the standard way to obtain objects, provides only the latter. Thus, most requests for objects end up being nonpolymorphic heap allocations, whether this is a good idea or not. In short, new should be considered harmful for the same reason that goto is considered harmful although it is an indispensable low-level tool, it must be used with care or hidden behind abstractions.

Although I think that new should be judiciously used, I don't think the author makes the case for a revised syntax. Not every design pattern (in this case Factory) needs to be a built in feature of the language. At some point in the code, somebody has to decide where and how a object of a specific class needs to be instantiated. Whether one puts it in-line, or hides it behind an abstraction, there's still a method that has to go about allocating the object.
Posted to OOP by Chris Rathman on 3/2/02; 11:05:21 AM

Dan Shappir - Re: Java's new Considered Harmful  blueArrow
3/4/2002; 2:26:07 AM (reads: 1231, responses: 0)
Not completely on-topic but when I first saw Java one of the first things that was a turnoff for me was the fact that to create a new foo type object you have to write:

foo bar = new foo();

instead of simply:

foo bar();

or something like that. Petty I know, but it just seems like way too much typing to accomplish a very basic and common operation.

Alex Moffat - Re: Java's new Considered Harmful  blueArrow
3/5/2002; 7:43:55 AM (reads: 1186, responses: 0)
Also not really on topic but... In the development I do at work the most basic and common operation is looking through existing code to fix bugs or add enhancements. I suspect this is true for most commercial java development whether you're looking through code you or someone in your team wrote or through xerces or xalan or jdom or some other package. Here's where there is the potential conflict though. Brief to write and "more than one way" will help early adoption for a language but MAY conflict with easy to read and understand, which is what I want when faced with 250k lines to maintain. (of course the usual whipping boy here is perl, so I'll point my finger that way also)

Dan Shappir - Re: Java's new Considered Harmful  blueArrow
3/5/2002; 8:30:18 AM (reads: 1191, responses: 0)

I noted the following style:

foo bar();

not because I think its perfect but because its familiar to C++ programmers as the syntax for defining new objects on the stack. BTW this syntax is less than ideal because it can confuse the C++ compiler (can be read both as an object definition and as a function declaration).

I don't think this type of shortcut undermines readability, quit the opposite. I don't see how writing foo twice in the same line improves understanding. Yes, the Java syntax allows you to write:

foo bar = new subfoo();

where subfoo is a subclass of foo, but when is the last time you have written such a line of code? (personally, in 15 years of programming I've never).

One of the things I do like about Java, at least when compared to C++, is the variable declaration syntax ( int[] a >> int a[] ). This thing with the new is just a minor inconvenience that goes to Java's verbosity that has been complained about here before. Verbosity is not a good thing IMO unless you are a COBOL fan

Alex Moffat - Re: Java's new Considered Harmful  blueArrow
3/5/2002; 6:36:50 PM (reads: 1197, responses: 0)
I wasn't particularly complaining about foo bar(); as just drifting off topic towards the thought that designing a language for ease of maintenance might be an interesting gedankenexperimente.

Actually I seen not so much foo bar = new subfoo(); as things like InterfaceFoo bar = new FooImplementation() quite a bit. Writing Map m = new HashMap(); can be quite useful when the important thing is the Map interface and not the HashMap implementation, helps keep the rest of the code honest (waiting for generics I am).

BTW, how did you know I liked COBOL :)

Dan Shappir - Re: Java's new Considered Harmful  blueArrow
3/6/2002; 2:17:04 AM (reads: 1179, responses: 1)

As long as we are staying off topic, here is another anecdote in this vain. Frank's favorite languages, VB and VB.NET support the following syntax for declaring and initializing an object reference:

Dim bar As foo
Set bar = New foo

Well almost, since in VB.NET the Set instruction is gone. VB.NET also allows folding this into a single statement:

Dim bar As foo = New foo

BTW, the Dim instruction is a holdover from the BASIC days of yore. It stands for dimension as it was used for declaring arrays and specifying their dimension (other variable types were not declared).

Both dialects also support the following syntax:

Dim bar As New foo

which is sort of close to what I would like, only this statement has a widely different meaning in VB and in VB.NET. In VB.NET this is simply another shorthand for declaring and initializing the reference. In VB this statement results in an object that is created on demand. That is, this declaration does not create a new instance. Instead, before each use of bar it is tested for being a null reference. If it is still null, a new instance is created and assigned to it. This is useful because VB apps are usually event driven and this allows objects to be created only if and when they are actually required.

Frank Atanassow - Re: Java's new Considered Harmful  blueArrow
3/6/2002; 11:35:17 AM (reads: 1253, responses: 0)
Frank's favorite languages, VB and VB.NET support the following syntax

I feel like an evil god who appears whenever his name is mentioned.