Java vs Python

Who am I?

  • Cesare Placanica
  • Cisco Photonics NMTG group.
  • Java developer since 2010 (Java 1.5/1.7, Spring, Hibernate and Tomcat).
  • Previously C/C++ embedded RTOSes.
  • Python Wannabe since 2004.

Agenda

  • Round 1 AOP vs decorators.
  • Round 2 Generics vs Duck Typing.

Round 1 Aspect Oriented Programming vs Decorators

http://stackoverflow.com/questions/4551457/python-like-decorators-in-java

Problem Definition

Encapsulation of cross-cutting concers. Logging, AAA, Caching, Profiling.

Java

A Spring Aspect Oriented Programming example.

https://github.com/keobox/dojokaffe/tree/master/fp/spring-elapsed

Code walkthrough in Eclipse.

Python


In [3]:
"Elapsed decorator."

import datetime

def elapsed(func):
    "Elapsed decorator"
    def _wrapper(*args, **kwargs):
        "Decoration function"
        start = datetime.datetime.now()
        ret = func(*args, **kwargs)
        print("Elapsed time", datetime.datetime.now() - start)
        return ret
    return _wrapper

The former was both a Closure and a High Order Function. Did you heard about Functional Programming?


In [6]:
# Usage:

import time

@elapsed
def task():
    "A difficult task."
    print("Processing...")
    time.sleep(2)

task()


Processing...
Elapsed time 0:00:02.002021

The previous was the built-in "Decorator" syntax.

Battery Included!!

Round 2 Generics vs Duck Typing

Problem definition

Write "Generic" code. OK, less naive, in Java, write code in terms of types to-be-specified-later that are checked at compile time when instantiated by the client of a "parametrized" class.

Duck Typing it's a sort of Generic Programming where object's suitability is determined by the presence of certain methods and properties, rather than the actual type of the object.


In [1]:
class Duck:
    def quack(self):
        print("Just a crazy, darn fool duck!")
        
class Man:
    def quack(self):
        print("Are you crazy?!")
        
def porky_pig_shoots_a(quacker):
    quacker.quack()

In [2]:
duffy = Duck()
cesare = Man()

porky_pig_shoots_a(duffy)
porky_pig_shoots_a(cesare)


Just a crazy, darn fool duck!
Are you crazy?!

Although is perfectly possible to use Duck Typing in Java with the Reflection API, Java is not done for this. Since Java 1.5 we have Generics, so let's take a look to them.

Look at Generics issues pointed out by "Effective Java" Item 25.

  • Java Arrays are type safe at runtime only, because covariant and reified. Fixed in Scala: arrays are invariant, immutable lists are covariant.
  • Java Containers are invariant and erased, so it's not possible to write arrays of containers, because arrays of not reified types are not permitted.
  • Arrays and Generics do not mix well.
  • API with Generics and varargs: varargs means arrays. Mitigantion: wildcards, see Item 28.
  • Erasure preserved backward compatibility but is an implementation issue that clutters the language.

Generics was a great addition, but the choice to be backward compatible with previous Java code has produced some implementation problems that surfaced into the language design.

Pitfalls

Other examples of Pitfalls:

  • C++ non virtual destructors.
  • Java and C++ calling virtual methods in constructors.

Some praises to Python Language Design

"...This was my first clue that, in Python, I was actually dealing with an exceptionally good design. Most languages have so much friction and awkwardness built into their design that you learn most of their feature set long before your misstep rate drops anywhere near zero...". "Why Python?" Eric Raymond, 2000, http://www.linuxjournal.com/article/3882

"I always admired the neatness of Python syntax...". Martin Odersky, 2017, https://github.com/lampepfl/dotty/issues/2491

Conclusion

  • Java Language design was driven by performance and backward compatibility.
  • Python Language is a well designed language. But is not fast, as Java.

BTW in Java there's stuff I like.

  • Abstract Base Classes.
  • Interfaces.
  • A terse import system.
  • TreeSet and TreeMap.

Bibliography

  • Effective Java, 2nd edition, Joshua Bloch.
  • The Well Grounded Java Developer, Benjamin J. Evans and Martijn Verburg.
  • Python in a Nutshell, 2nd edition, Alex Martelli.
  • The Quick Python Book, 2nd edition, Vernon L. Ceder.
  • Programming in Scala, 2nd edition, Marting Odersky, Lex Spoon, Bill Venners.