Tuesday 20 August 2013

Top Most The Death of Java - Generics

Java has been dying a slow death for some years now. Competition from higher productivity languages has forced Java to market "Java as a platform", rather than Java as a language. Businesses can take advantage of the benefits of the JVM while having the freedom to choose from a variety of programming languages.


The classic challenge of any software is to add new features and paradigms while maintaining compatibility with existing solutions. Over time this challenge results in either the death of the software or a new release that forces users to "freeze" their upgrades until they complete a "conversion".

Neither of these options are appreciated by users.



Generics are a good example of how Java is dying. What seemed like a good idea has, in my opinion, backfired. To understand this, we need to understand how we got here.

Type-safety
Many years ago IBM introduced a programming language designed to replace all others with a single language, and called it PL-1 (Programming Language One). PL-1 was designed to be powerful, yet it failed for a simple reason: it was very easy to code bugs.

Languages evolved to improve this situation, and one of the most successful features was type safety. Simply put, type safety meant variables could be declared as having a type (string, integer, etc.) and the compiler would produce errors if the programmer tried to use that variable as a different type. Strongly typed languages forced programmers to specify the type of all variables.

Interestingly, bugs were reduced by restricting what a programmer could do.

Java's type safety
The term strongly typed became muddled over the years, and zealous programmers engaged in lengthy debates arguing over semantics. Java is, by design, strongly typed. However it is also Object Orientated, using the classic "everything descends from a single object" paradigm. So there is nothing preventing programmers from declaring variables to be of type Object, which neatly defeats type safety.

Collections
Java 3 provided the Collections framework, an implementations of common data structures (lists, sets, maps and bags). Designed for maximum usability, collections store references to many types by declaring much of it's api to be of type Object.

What was the problem with Collections?
Collections were not type-safe. Let's say a developer creates a collection of strings. Later on she (or someone else) starts storing numbers in the same collection. This usually results in bugs since the existing code assumes that the collection contains strings.

Generics come to the rescue!
With the release of Java 5, the generics feature fixed this hole for collections (and in general). Programmers could write API's that support any type, yet enforced type safety at compile time. Awesome, right?

Pragmatism gets in the way
There are two serious flaws and one minor issue with generics.


Minor issue: Generics only work at "compiler time". It is possible to break generics at runtime. This may sound like it is more than a "minor" issue, but see below.

Flaw #1: Adding generics to code makes it more verbose, more difficult to read, and reduces productivity.

Flaw #2: In over 12 years I've performed many hundreds of Java code reviews and observed thousands of bugs reports. In all this time, I have never seen a bug that generics would have prevented.  I'm sure there have been some, but I haven't seen any.

Trade-off
The trade-off is reduced productivity, less readable code and more verbose code, to prevent an issue that never happens. Yet every Java shop I know of requires generics!

What sounded like a good idea, in my view, has only hurt the language.



LinkWithin

Related Posts Plugin for WordPress, Blogger...

Labels

Core Java programming core java interview question Core Java Faq's Servlets coding database jsp-servlet spring Java linux unix interview questions java investment bank Web Services Interview investment bank mysql Senior java developer interviews best practices java collection tutorial RMI SQL Eclipse FIX protocol tutorial tibco J2EE groovy java questions SCJP grails java 5 tutorial jdbc beginner error and exception Design Patterns Java Programming Tutorials fundamentals general object oriented programming xml Java Programs Hibernate Examples Flex JAMon Java xml tutorial logging Jsp Struts 2.0 Sybase and SQL Server debugging java interviews performance FIX Protocol interview questions JUnit testing WebSphere date and time tutorial experienced java IO tutorial java concurrency thread Ejb Freshers Papers IT Management Java Exapmle Java Script SQL and database tutorial examples Scwcd ant tutorials concurrency example and tutorial future state homework java changes java threading tricky Agile Business of IT Development JSTL Java JSON tutorial Java multithreading Tutorials PM Scrum data structure and algorithm java puzzles java tips testing tips windows 8 5 way to create Singleton Object Architect Interview Questions and Answers Architecture Architecure Bluetooth server as swing application that searches bluetooth device in 10 meter circle and show all devices. You can send file to any bluetooth device. C Programming CIO Callable Statement in Java Circular dependency of Objects in Java Comparable Example in Collection Custom annotation in Java Developer Interview Divide and rule example in java Drupal Example of Singleton Pattern FIX protocol ForkJoin Example in Java 7 Get data from dynamic table with Java Script Git HTML and JavaScript Health Hello World TCP Client Server Networking Program Hibernate Basics Hibernate Interview Question Answer J2EE Interview Question And Answers J2ME GUI Program JEE Interview QA JMS interview question Java J2EE Hibernate Spring Struts Interview Question Java System Property Java Threads Manager Portlets Provident Fund Read data from any file in same location and give the required result. Reading Properties File in Java Redpoint Rest WebService Client Rest Webservice Test SAL join with ven diagram SCP UNIX COMMAND SSL Singleton Pattern in Java Spring Bean Initialization methods and their order Spring Interview Questions Struts Struts 2.0 Basics Struts 2.0 Design Pattern Submit Html Form With Java Script On The Fly Unix executable For Java Program XOM DOM SAX XP books computers core java; core java; object oriented programming data structure; java investment bank; design pattern dtd duplicate rows in table get browser name with jquery grails podcast inner class java beginners tutorial java cache java networking tutorial java spring java util; java collections; java questions java.java1.5 linked list mailto function with all browser oracle database oracle duplicate rows orm schema social spring mvc questions struts transaction tricks tweet windows xslt