The Artima Developer Community
Sponsored Link

Weblogs Forum
Have Generics Killed Java?

62 replies on 5 pages. Most recent reply: Aug 8, 2010 6:43 PM by Eric Armstrong

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 62 replies on 5 pages [ « | 1 2 3 4 5 | » ]
Morel Xavier

Posts: 73
Nickname: masklinn
Registered: Sep, 2005

Re: Have Generics Killed Java? Posted: Jul 23, 2010 7:32 AM
Reply to this message Reply
Advertisement
> The guy that heads up the JRuby project has been working
> on a statically-typed language with a Ruby syntax.
>
> http://www.mirah.org/wiki/MirahFeatures
>
> And some samples: http://www.mirah.org/wiki/MirahSamples
>
> Besides being a statically-typed language with a very
> dynamic, rubyesque feel to it, it doesn't have any big
> runtime.

A much earlier effort in that vein is StrongTalk, a Smalltalk with an optional, incremental static type system (interestingly, StrongTalk is also a very fast Smalltalk implementation but the runtime completely ignores the static types and use dynamic type feedback instead):

aMessage: aStringBlock <[^Str]> ^<Int>
| result <Int> |
result := aStringBlock value size.
^result


Interestingly, StrongTalk's type system supports type unions so you could write `<Int|Str>` for a method which returns either a string or an integer, as well as complex "inference clauses".

Eric Armstrong

Posts: 207
Nickname: cooltools
Registered: Apr, 2003

Re: Have Generics Killed Java? Posted: Jul 23, 2010 1:26 PM
Reply to this message Reply
Max Lybbert wrote:
> As others have said, the problem with
>
>
for (Map.Entry<K, V> e : map.entrySet())

>
> isn't the generics; it's the lack of type inference. In
> C# it's
>
>
foreach (var e in map)

>
Aha! THAT hits the nail on the head. The example speaks clearly. It is the lack of type inferencing that is driving me crazy. I don't mind declaring things--I just really mind code in which the intent isn't clear.

Thanks for that.

Mark Thornton

Posts: 275
Nickname: mthornton
Registered: Oct, 2005

Re: Have Generics Killed Java? Posted: Jul 23, 2010 1:58 PM
Reply to this message Reply
Java's generics are not just syntactic sugar because the type isn't completely erased.

class Order {
  private List<Widget> items;
}


It is possible to discover at runtime that the items field is a list of Widget's. I use this to create a mapping to a database with minimal repetition of information.

Nemanja Trifunovic

Posts: 172
Nickname: ntrif
Registered: Jun, 2004

Re: Have Generics Killed Java? Posted: Jul 23, 2010 2:21 PM
Reply to this message Reply
> Eric, I don't think many are going to be able to reply to
> your post because it's fractally wrong. Pretty much
> everything in it is wrong including the premises, each and
> every argument and the conclusion. But to correctly reply
> to it would need a complete deconstruction taking hours,
> and I would guess most people don't have the time, drive
> and money to do that.
>

Exactly! Without going into any details, let's just say that the lack of type-safe containers and the need for constant downcasting was my main source of frustration when I was using Java (admitedly, that was some 10 years ago).

Neeraj Sangal

Posts: 4
Nickname: nsangal
Registered: May, 2005

Re: Have Generics Killed Java? Posted: Jul 23, 2010 2:31 PM
Reply to this message Reply
Java is not dead, so at least the title is clearly wrong.

Of course, there is some truth to the point that is being made. I too believe generic have made the language harder to understand. However, unlike this article, I believe that only a part of the problem is syntax. I still don't claim to understand Enum<T extends Enum<T>> and it isn't because the syntax is difficult.

However, I do have sympathy for the language designers. I remember that once Java came out, the C++ gurus went around arguing that since it didn't have generics, java was clearly inferior. Many others chimed in criticizing the lack of typing in collections. Now, I am waiting for the time when some ask for a pre-processor.

It seems that the natural evolution of languages is to accumulate features that are often marginal and, in some cases, downright harmful to all but a handful of developers. The blame for languages acquiring undesirable features is shared by many. In part, we the users are also to blame - we demand new features or stay quiet when others demand features that ultimately take the language in directions that it was never designed for. Yet, Java, inspite of its flaws, remains a better language than most of the other "flavor of the month" languages.

Neeraj Sangal
http://blog.lattix.com

Eric Armstrong

Posts: 207
Nickname: cooltools
Registered: Apr, 2003

Re: Have Generics Killed Java? Posted: Jul 23, 2010 9:03 PM
Reply to this message Reply
Neeraj wrote:
>
> I do have sympathy for the language designers...
>
Moi aussi. The need to continually add features, while at the same time ensure backwards compatibility, has been good in some respects, horrible in others.

Every 10 years, I think a language owes it to itself to call a halt to anything but bug fixes and performance improvements, and start with a fresh slate for the next version. (A heck of a lot is learned in 10 years!)

Karl Peterbauer

Posts: 1
Nickname: scotty69
Registered: Jul, 2010

Re: Have Generics Killed Java? Posted: Jul 23, 2010 9:28 PM
Reply to this message Reply
No, generics have not killed Java. Yes, current Java generics are a real beast. They are a intellectual challenge, and too often erasure raises its ugly head. Hopefully we get the diamond operator in JDK 7 for mitigating the missing type inference capabilities.

But their implementation is a really brilliant piece of software engineering with respect to backwards compatibility. Today, I'm happily generifying code I've written a decade ago with JDK 1.3. It makes the code more readable and understandable. Internal data structures such as a map of lists of strings indexed by a string key can be declared as a Map<String, List<String>>. This is sound and solid structural information for any other developer maintaining the code (or myself), and any attempt to put anything else into the map is punished by the compiler with an error.

Agreed, Java generics are definitely not the holy grail. But dynamic languages are not the answer either, because they don't even try to capture structural contracts. One step further, many dynamic languages allow to dynamically add methods to an object. Really nice for quickly hacking existing code, but a maintenance nightmare, because now functional contracts are also gone, forcing a developer to understand the full codebase to know which part of an object's functionality is available at runtime.

Eric Armstrong

Posts: 207
Nickname: cooltools
Registered: Apr, 2003

Re: Have Generics Killed Java? Posted: Jul 23, 2010 10:09 PM
Reply to this message Reply
Karl wrote:
>
> But their implementation is a really brilliant piece of
> software engineering with respect to backwards
> compatibility.
>
Have to agree with that. I have nothing but respect for the sheer intellectual achievement.

> Today, I'm happily generifying code I've
> written a decade ago with JDK 1.3. It makes the code more
> readable and understandable.
>
This seems to be their primary raison d'etre. But what I've found in the "specification" process (where I write runnable tests before writing the code) is that such things are fully documented in the (readable) tests. (See my post on Readable, Runnable Tests, or words to that effect.)

> Many dynamic languages let you dynamically add methods
> to an object. Really nice for quickly hacking
> existing code, but a maintenance nightmare, because now
> functional contracts are also gone, forcing a developer to
> understand the full codebase to know which part of an
> object's functionality is available at runtime.
>
Yeah. It's a dual edge sword, especially if it's done willy-nilly, anywhere in the code. But best practices suggest doing such things as close to "up front" as is practical.

With modules, such things are nicely encapsulated, too. So if one module needs an extension to String, and another module needs a different extension, each is free to add the extension it needs.

I understand the potential danger. But there is really no substitute. When I'm knee deep in code and find that I really *need* some feature the designers left out of the original class, it's just not practical to subclass String, rewrite every method to use the Subclass, and modify every
API to recast incoming and outgoing Strings.

(I know. I can hear you shuddering. But I love open classes. But then, I have become a big fan of writing readable, runnable tests before writing code.)

Morel Xavier

Posts: 73
Nickname: masklinn
Registered: Sep, 2005

Re: Have Generics Killed Java? Posted: Jul 24, 2010 2:10 AM
Reply to this message Reply
> I understand the potential danger. But there is really no
> substitute. When I'm knee deep in code and find that I
> really *need* some feature the designers left out of the
> original class, it's just not practical to subclass
> String, rewrite every method to use the Subclass, and
> modify every
> API to recast incoming and outgoing Strings.

Good thing there is no need to do that though (I'd go as far as to say that doing that makes few if any sense). You can either use external modules (utility functions) or, if the languages supports it, C#-style statically typed and type-safe extension methods.

Max Lybbert

Posts: 314
Nickname: mlybbert
Registered: Apr, 2005

Re: Have Generics Killed Java? Posted: Jul 24, 2010 1:13 PM
Reply to this message Reply
Eric Armstrong wrote:
> Max Lybbert wrote:
> > As others have said, the problem with
> >
> >
for (Map.Entry<K, V> e : map.entrySet())

> >
> > isn't the generics; it's the lack of type inference. ...
> Aha! THAT hits the nail on the head. The example speaks
> clearly. It is the lack of type inferencing that is
> driving me crazy. I don't mind declaring things--I just
> really mind code in which the intent isn't clear.
>
> Thanks for that.

I can't take all the credit. Somebody here at Artima corrected me on this issue a few years ago.

Max Lybbert

Posts: 314
Nickname: mlybbert
Registered: Apr, 2005

Re: Have Generics Killed Java? Posted: Jul 24, 2010 1:25 PM
Reply to this message Reply
> > Many dynamic languages let you dynamically add methods
> > to an object. Really nice for quickly hacking
> > existing code, but a maintenance nightmare, because now
> > functional contracts are also gone, forcing a developer
> > to understand the full codebase to know which part of an
> > object's functionality is available at runtime.
>
> Yeah. It's a dual edge sword, especially if it's done
> willy-nilly, anywhere in the code. But best practices
> suggest doing such things as close to "up front" as is
> practical.
>
> With modules, such things are nicely encapsulated, too. So
> if one module needs an extension to String, and another
> module needs a different extension, each is free to add
> the extension it needs.
>
> I understand the potential danger. But there is really no
> substitute. When I'm knee deep in code and find that I
> really *need* some feature the designers left out of the
> original class, it's just not practical to subclass
> String, rewrite every method to use the Subclass, and
> modify every API to recast incoming and outgoing Strings.
>
> (I know. I can hear you shuddering. But I love open
> classes. But then, I have become a big fan of writing
> readable, runnable tests before writing code.)

I'm not sure I understand the issue here. I take it the "subclass String and add your extension" is meant as a strawman. Every programmer I know would write a StringHelper class with a static method that takes a String and provides the functionality.

However, Ruby monkey patching, C# extension methods, C++'s nonfriend free functions, Java static methods in different classes that take the object (eg., the StringHelper class), and similar mechanisms really are syntactic sugar for the same thing: a method that takes an object and does stuff with it. I have a hard time seeing any mechanism as inherently better than the others so long as the code organization doesn't get squirrelly.

John Zabroski

Posts: 272
Nickname: zbo
Registered: Jan, 2007

Re: Have Generics Killed Java? Posted: Jul 28, 2010 11:01 AM
Reply to this message Reply
> Great post Eric. I've got two comments, though.



Seriously, bill?

How can it be a "great post" with WRONG statements like:

> <p>Generics force the programmer to do at something the
> computer is
> entirely capable of doing at run time (type verification).
> And if
> we have learned anything at all about computers, it's to
> let them
> do what they're good at!</p>

Stop putting front-page article material that will miseducate our youth.

STOP IT.

Eric Armstrong

Posts: 207
Nickname: cooltools
Registered: Apr, 2003

Re: Have Generics Killed Java? Posted: Jul 28, 2010 1:59 PM
Reply to this message Reply
John Zabroski wrote:
> (unflattering things)
>
Sorry you took issue with it, John. But I believe the underlying assertion is correct: That with the advent of generics, Java took a serious hit with respect to readability and elegance.

As others were kind enough to point out, it is clear that type inference is a necessary concomitant, if readability is to be restored. In retrospect, type inferencing should have been included at the same time.

I also stand by my assertion that tracking details is what the computer does best. Forcing the programmer to do it is not a good idea for productivity, and more than it is for readability.

In this case, another version of "let the computer do it" is type inferencing. So while the statement could clearly be improved, it is not exactly "wrong".

Morgan Conrad

Posts: 307
Nickname: miata71
Registered: Mar, 2006

Re: Have Generics Killed Java? Posted: Jul 28, 2010 2:14 PM
Reply to this message Reply
> Great post. Java is making some progress in this area with
> inference of generics, e.g. the following is scheduled for
> JDK7:
>
>
> List<String> strings = new ArrayList<>();
> 



Yeah! I pushed for this years ago. Actually, I pushed for even less typing

List<String> strings = new ArrayList();


Frankly, I used to, and sometimes still do, code like this and just ignore the warning message. Depends how much of a stickler the rest of your team is.

Cedric Beust

Posts: 140
Nickname: cbeust
Registered: Feb, 2004

Re: Have Generics Killed Java? Posted: Jul 28, 2010 2:23 PM
Reply to this message Reply
> > Great post. Java is making some progress in this area
> with
> > inference of generics, e.g. the following is scheduled
> for
> > JDK7:
> >
> >
> > List<String> strings = new ArrayList<>();
> > 

>
>
> Yeah! I pushed for this years ago. Actually, I pushed
> for even less typing
>
>
> List<String> strings = new ArrayList();
> 


This is not backward compatible, which is why the expert group went with the "diamond syntax" (<>).

--
C├ędric

Flat View: This topic has 62 replies on 5 pages [ « | 1  2  3  4  5 | » ]
Topic: How Much Unit Test Coverage Do You Need? - The Testivus Answer Previous Topic   Next Topic Topic: Are Dynamic Languages Going to Replace Static Languages?


Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2014 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us