Java Generics Adoption: How New Features are Introduced Championed or Ignored

24
Java Generics Adoption How New Features are Introduced, Championed, or Ignored Chris Parnin Georgia Tech Christian Bird Microsoft Research Emerson Murphy-Hill North Carolina State

description

Adding features to a programming language is a difficult prospect. Politics, committees, compatibility, and egos don’t make it any easier. And once a feature has finally made it and shipped, there is a whole other question of how does it plays out? Did designers get it right, does adoption occur? Does the promises of the new feature actually solve the software developer’s pain? How do developers decide to use a new feature, do they plan to migrate old code, coordinate coding standards?

Transcript of Java Generics Adoption: How New Features are Introduced Championed or Ignored

Page 1: Java Generics Adoption: How New Features are Introduced Championed or Ignored

Java Generics AdoptionHow New Features are Introduced, Championed, or Ignored

Chris Parnin Georgia Tech

Christian BirdMicrosoft Research

Emerson Murphy-HillNorth Carolina State

Page 2: Java Generics Adoption: How New Features are Introduced Championed or Ignored

Quick refresher

List myList = …;String aStringFromMyList = (String)myList.get(0);

class List{ Object get(int i){…}} Cast (C)

Raw Type R

Page 3: Java Generics Adoption: How New Features are Introduced Championed or Ignored

Quick refresher

List myList = …;String aStringFromMyList = (String)myList.get(0);

class List<T>{ T get(int i){…}}

Generic Type <T>

Page 4: Java Generics Adoption: How New Features are Introduced Championed or Ignored

Quick refresher

List<String> myList = …;String aStringFromMyList = myList.get(0);

class List<T>{ T get(int i){…}}

static <T> T head(List<T> l){return l.get(0);

};

Parameterized Type <P>

Generic Method <M>

Page 5: Java Generics Adoption: How New Features are Introduced Championed or Ignored

but life is not so simple…

Page 6: Java Generics Adoption: How New Features are Introduced Championed or Ignored

public interface Identifiable<T extends Identifier<? extends What>, What> { public T getObjectID(); public Class<? super What> type();}public interface Identifier<T> { public long getID(); public Class<? super T> type();}interface X <SubjectType extends Identifiable, RelationshipType extends Enum<RelationshipType> & Related, ObjectType extends Identifiable>{} static class A< SI extends Identifier<? extends SubjectType>, OI extends Identifier<? extends ObjectType>, SubjectType extends Identifiable<SI, SubjectType>, RelationshipType extends Enum<RelationshipType> & Related, ObjectType extends Identifiable<? extends OI, ? extends ObjectType>> implements X<SubjectType, RelationshipType, ObjectType> {}

Page 7: Java Generics Adoption: How New Features are Introduced Championed or Ignored

public interface Identifiable<T extends Identifier<? extends What>, What> { public T getObjectID(); public Class<? super What> type();}public interface Identifier<T> { public long getID(); public Class<? super T> type();}interface X <SubjectType extends Identifiable, RelationshipType extends Enum<RelationshipType> & Related, ObjectType extends Identifiable>{} static class A< SI extends Identifier<? extends SubjectType>, OI extends Identifier<? extends ObjectType>, SubjectType extends Identifiable<SI, SubjectType>, RelationshipType extends Enum<RelationshipType> & Related, ObjectType extends Identifiable<? extends OI, ? extends ObjectType>> implements X<SubjectType, RelationshipType, ObjectType> {}

“As I sit and stare in stunned horror at this monster”

"Writing generified classes is rocket science"

“Probably more time and brainpower was spent fussing with generics than any other single thing”

Page 8: Java Generics Adoption: How New Features are Introduced Championed or Ignored

why are these people so angry?

As a community, we often don’t formally evaluate our claims after we

put them out into the world.

Page 9: Java Generics Adoption: How New Features are Introduced Championed or Ignored

Outcome?

GoslingIdea

1994

Guy Steele Debate

1998

Java 5Implement

and Release

2004

Developer Usage

2011

Page 10: Java Generics Adoption: How New Features are Introduced Championed or Ignored

To investigate what happened to generics, let’s look at the claims and complaints made

about generics

Page 11: Java Generics Adoption: How New Features are Introduced Championed or Ignored

Did programmers adopt and use generics?

“I suspect we will see a lot of Java 5 code that continues to do things the bad old way as a result and

contend with the mish-mash that results” (2005) – lambda the ultimate

Page 12: Java Generics Adoption: How New Features are Introduced Championed or Ignored

Did programmers adopt and use generics?

“There seems to be some debate over refactoring to utilize java generics within my current team … a holy war going on about what should and should not be done …” – stackoverflow

Will teammates all agree to use generics?

Page 13: Java Generics Adoption: How New Features are Introduced Championed or Ignored

Did programmers adopt and use generics?

Will teammates all agree to use generics?

“Do you want to go back to casting-from-Object in every container access and dealing with the resulting runtime-revealed type errors?” – stackoverflow

Does generics reduce runtime errors?

ClassCastExceptions are rarely seen in bug reports!Caught with local testing

before check-ins, etc…

Does generics reduce casts?

Page 14: Java Generics Adoption: How New Features are Introduced Championed or Ignored

Did programmers adopt and use generics?

Will teammates all agree to use generics?

Does generics reduce casts?

RQ 1 Adoption

RQ 2Coordination

RQ 3Casts

Page 15: Java Generics Adoption: How New Features are Introduced Championed or Ignored

Study Setup

500 million LOC

<P> <M> (C) <T> R

Entities of Interest Per Commit Database for Analysis

Lifetime of 20 open-source projects: Before and After generics.

RQ 1 Adoption

RQ 2Coordination

RQ 3Casts

Page 16: Java Generics Adoption: How New Features are Introduced Championed or Ignored

532 developer’s generic usage?

No generics!

RQ 1 Adoption

RQ 2Coordination

RQ 3Casts

We use generic collections!

We use generics methods or

classes!

Page 17: Java Generics Adoption: How New Features are Introduced Championed or Ignored

Developers with higher commit frequency?

RQ 1 Adoption

RQ 2Coordination

RQ 3Casts

Higher ratio of generics collection

usage (42%)

But generic methods or classes stay

about the same.

Page 18: Java Generics Adoption: How New Features are Introduced Championed or Ignored

2002 2004 2006 2008 2010

05

00

10

00

15

00

20

00

25

00

Squirrel−SQL

Date

Typ

e In

tro

du

ctio

ns/

Re

mov

als

GenericRaw

Never used generics

2002 2004 2006 2008 2010

05

00

10

00

15

00

20

00

25

00

Squirrel−SQL

Date

Typ

e In

tro

du

ctio

ns/

Re

mov

als

GenericRaw

RQ 1 Adoption

RQ 2Coordination

RQ 3Casts

Page 19: Java Generics Adoption: How New Features are Introduced Championed or Ignored

2002 2004 2006 2008 2010

05

00

10

00

15

00

20

00

25

00

Squirrel−SQL

Date

Typ

e In

tro

du

ctio

ns/

Re

mov

als

GenericRaw

2002 2004 2006 2008 2010

05

00

10

00

15

00

20

00

25

00

Squirrel−SQL

Date

Typ

e In

tro

du

ctio

ns/

Re

mov

als

GenericRaw

Another programmer

starts contributing…

Then a champion emerges!

Then others follow.

The champion even migrates

old code.

RQ 1 Adoption

RQ 2Coordination

RQ 3Casts

Page 20: Java Generics Adoption: How New Features are Introduced Championed or Ignored

2000 2001 2002 2003 2004 2005 2006 2007 2008 2009

-

(C)

<P>

Casts (normalized) should decrease with generics.

jEdit

We found no correlation for jEdit

RQ 1 Adoption

RQ 2Coordination

RQ 3Casts

Page 21: Java Generics Adoption: How New Features are Introduced Championed or Ignored

2000 2001 2002 2003 2004 2005 2006 2007 2008 2009

-

(C)

<P>

jEdit

2000 2001 2002 2003 2004 2005 2006 2007 2008 2009

Squirrel-SQL

(C)

<P>

We found a strong correlation for Squirrel-SQL

RQ 1 Adoption

RQ 2Coordination

RQ 3Casts

Casts reduced before developers start to introduce generics.

Page 22: Java Generics Adoption: How New Features are Introduced Championed or Ignored

But, overall, only Squirrel-SQL had a strong correlation, and 6 with moderate correlation.

RQ 1 Adoption

RQ 2Coordination

RQ 3Casts

Page 23: Java Generics Adoption: How New Features are Introduced Championed or Ignored

Other findings

Very little refactoring to generics (typically under 10%)

Half of generics instantiated with just one type

Generics support modest reductions in duplication

90% of generic usage is java.util collections

IDE support has little relation to generic adoption date

Generic methods are rare!

Page 24: Java Generics Adoption: How New Features are Introduced Championed or Ignored

A Metaphor

Regulates Drugs Regulates Programming Features

Monitors Adverse Reactions

Measures treatment effectiveness

Monitors Allergic Reactions

Measures impact and migration costs