Wednesday, 14 October 2015

Day 2

Functional Refactoring

(Urma and Warburton)

Many similar functions with slightly different arguments lists encourage copy-paste code and cry out for refactoring. Solution is to pass a Predicate as an argument and to change the predicate for each use-case:
myMethod(List list, Predicate P) {
  ...
  if (p.test(item))
    doStuff

Predicate can be a lambda function:
  myMethod(list, item -> (getItem == ORACLE));

These are first-class functions that can be treated as a value. Heavy use of final is needed to make members immutable.

One nice touch was the use of java.util.Optional to avoid having to install layers of defensive programming to protect against NullPointerException.

Functional Programming with Clojure

(John Stevenson)

Clojure is a functional language that derives from Lisp and plugs straight into the JVM. Has dynamic typing, functions always return something (so no void), members are immutable and stateless so no thread-safety worries. It allows for arbitrary perl-like data structures and the arithmetic engine uses prefix notation (+ 1 2 3 4 5 -> 10). One interesting feature is that fractions like / 22 7 are not resolved immediately but are kept until the result is actually required. So if you later multiply by 7 again, you get exactly 22 back and no rounding errors.
Unfortunately, we never did get to the functional part...

Lambda Puzzlers

(Peter Lawrey)

This was a good one:

This gives a compiler error about an unhandled IOException:
submit -> {
  doStuff();
}

This doesn't:
submit -> {
  doStuff();
  return null;
}

Why not?

Answer: The first one could implement Callable or Runnable - we don't know. If it implements Runnable, it's not allowed to throw a checked exception so we have to handle it. The second one returns - so it must implement Callable, which is allowed to throw a checked exception so we're OK. Phew.

HTML5 and Java EE7

(Geertjan Wielanga)
Entertaining talk about how HTML5 is really now an application framework (for example, a input selection list is now automatically filtering - no need for FilterInputSelectors from dojo, or whatever). With CSS3 and conditional display of content it is now possible to write single-page applications that will display on any client (so without if-else over user agent). 

He ran through the bewildering list of JS frameworks and libraries and advised simply to forget about volatility. The average lifespan of a framework is 2 years so by the time you have to modify anything you will be better just rewriting the app in whatever is the latest framework then. This is the WONTA paradigm; Write Once, Never Touch Again. So forget about upgrading and backwards compatibility and all that.


No comments:

Post a Comment