Much ado about null (Stop fighting Null)

Lately, I’ve seen a lot of talk about null come across my radar - mainly how  it’s a big mistake (the “billion dollar mistake”) and should be avoided.

I saw this article on hacker-news today called “The Definitive Reference to why Maybe is better than Null.” The point is that using a “Maybe” construct requires the developer to think about what happens when there is no value. In languages that have null (like Java, Groovy and Scala), it makes it harder to have NullPointerException’s, but not impossible.

Maybe is much like Scala’s Option. It has two implementations (accessed using static methods); one representing an absence of value (Maybe.nothing()), and one wrapping a non-null value (Maybe.defititely(value)). 

There is at least one implementation of Maybe in Java (by Nat Pryce). This Maybe also implements the Iterable interface so that multiple Maybe’s can be joined (using Guava’s concat method for example). This also allows for the following construct:

for (String value : maybeValue) doSomethingWith(value);

If you’re interested, I’ve made my own fork of Nat Pryce’s Maybe and deployed it to Maven central.

Guava also has the similar class, Optional. Optional also has static methods to create “absence” or “presence” of a value (Optional.absent() and Optional.of(value)).

The Java 8 standard lib will have a similar built-in Optional class that seems to take inspiration from both Maybe and guava.

As _why says somewhere is his recently “printed” manuscript, he was tired of “constantly fighting null.” Well, with Maybe, maybe you don’t have to ;-)

Google Guava

Google started releasing some internal Java code as open-source under the name Google-collections. This became much more than only collection support and was rebranded as guava.

Guava contains a lot of extremely useful code.

It adds a bunch of very useful Collection-related classes and interfaces:

  • Collections2` - Utility methods for filtering, transforming, and getting all possible permutations of Collections.
  • BiMap` - A Map that goes both ways (one-to-one mapping where values can map back to keys).
  • Multimap` - A Map that can associate keys with an arbitrary number of values.
  • Multiset` - A set that also keeps tracks of the number of occurances of each element.
  • Table` - Uses a row and column as keys to values.

For every Collection type, it also has a static utility class with useful methods, for example:

  • Lists: newArrayList, asList, partition, reverse, transform`
  • Sets: newHashSet, filter, difference, union`
  • Maps: newHashMap, newTreeMap, filterKeys, filterValues, asMap`

It also contains some concurrency support, such as the following:

ListenableFuture

A ListenableFuture allows you to register callbacks to be executed once the computation is complete, or if the computation is already complete, immediately. This simple addition makes it possible to efficiently support many operations that the basic Future interface cannot support.

~~~~~~~~~~

ListeningExecutorService srv = MoreExecutors

.listeningDecorator(Executors.newFixedThreadPool(10));

ListenableFuture<Rocket> rocket = srv.submit(new Callable<Rocket>(){

  public Rocket call() {

    return launchIntoSpace();

  }

});

Futures.addCallback(rocket, new FutureCallback<Rocket>() {

  // we want this handler to run immediately after we launch!

  public void onSuccess(Rocket rocket) {

    navigateToMoon(rocket);

  }

  public void onFailure(Throwable thrown) {

    launchEscapePod();

  }

});

~~~~~~~~~~

Guava also contains tons of helpful utilities for general software development, such as the following:

EventBus

Allows publish-subscribe-style communication between components without requiring the components to explicitly register with one another (and thus be aware of each other).

CacheBuilder

Builds caches that can load and evict values. Caches are tremendously useful in a wide variety of use cases. For example, you should consider using caches when a value is expensive to compute or retrieve, and you will need its value on a certain input more than once.

BloomFilter

Bloom filters are a probabilistic data structure, allowing you to test if an object is definitely not in the filter, or was probably added to the Bloom filter.

Guava has great documention available on the [google-code wiki].

Tags: java code guava

Clouds at JavaOne

javaone2012:

Clouds Represented at JavaOne

Sorry for the long delay on this post!

Many different vendors were present at JavaOne. These are the PaaS (Platform as a Service) vendors that I noticed.

CloudFoundry

Cloud Foundry was originally founded by Chris Richardson, author of POJOs in Action. It was then bought by Spring, which was bought by VMWare. Cloud Foundry appears to still be in “beta” but supports a lot of languages and frameworks, including Scala, Play, Spring, Grails, node.js, and Rails.

Cloudbees

Cloudbees runs a host of different PaaS services, including a continuous integration (build) service using the Jenkins build manager. They also host SVN or git code repositories and have free accounts (for <= 2GB and <= 3 users).

Jelastic

Jelastic is a winner of the 2012 Duke’s Choice award and a favorite of Java-creator James Gosling. It runs your Tomcat, database (Postgres, MySQL, or MongoDB) and only charges you for what you “use” (on average and only in certain increments). However, your free trail only lasts about 14 days, so be prepared to pay after that.

Tags: cloud javaone

Modern Java: Java 7 and Polyglot Programming on the JVM

Tell you neighbors, tell your friends… I’m writing a book, Modern Java, to be published on leanpub and I’d love to get your feedback. You can get a free sample of the first few chapters covering the new features of Java 7.

It’s going to cover Java 7, Java 8, Groovy, Grails, Scala, Akka, Play, and a bunch of other stuff, but I’d love to know what you’d like to see.

In fact, that’s the whole point of leanpub, to publish a book before it’s finished and get feedback from the community. So that’s why I’m publishing now, even though it’s only around 50% done. Everyone who purchases a copy will get all the future updates and you can get it in PDF, ePub, or mobi (kindle) formats.

Tags: book java

Why use non-Java JVM languages?

There are many different reasons to use other languages on the JVM. You’re able to quickly change code in production or development and not have to recompile your codebase. This can make development faster and more flexible. Also, other languages have features that Java does not have that can increase developer productivity and make new things possible (such as closures, mixins, and meta-programming).

Although these languages are completely different from Java, they still run on the JVM and can interoperate with Java-based libraries.

The trade-off when using these other languages tends to be performance. However, the value of developer time gained is generally much more than the cost of the performance lost. In addition, Java 7 has added some features to the JVM to enhance the performance of dynamic languages (invokedynamic).

Here’s a short list of some popular JVM languages compared with features of Java - whether they are statically typed or dynamically typed: 

  • Java | Static
  • Scala | Static
  • Groovy | Either
  • Clojure  | Dynamic

Scala is a very concise and flexible language with both functional and object-oriented features. It has Type inference to make it very concise and Traits for multiple-inheritance (or mixins). It has implicits for meta-programming.

Groovy is very similar in syntax to Java, but with closures, dynamic typing, meta-programming (via metaClass) and many other things added. Java code is generally valid Groovy code. Groovy is interpreted at runtime, but in Groovy 2.0 the ability to compile to byte-code and enforce type-checking were added to the language (using @StaticCompile and @TypeChecked).

Clojure is a reboot of Lisp on the JVM, with its macro system and code-as-data philosophy. If you’re not familiar with Lisp, its syntax is very different from other languages, but it can be very powerful when used by a skilled programmer.

Polyglot programming

Of course, the great thing about programming today is that you don’t need to strictly limit yourself to one language. You can “use the right tool for the job” every time and become a polyglot programmer.

It is an important consideration whenever you start a new project. There are strong opinions on both sides of this issue, but you need to make your own decision. When making the decision, consider the following:

  •  How many junior level people will be modifying the code?
  •  Is everyone on the team familiar with this language?
  •  Is this project more like scripting, or more like business logic?

If there are lots of junior developers or the project is business logic, you may want to stick to a statically typed language.

2020 Programming/Tech - Predictions

Okay, so making predictions of technology in the near future is easy. It’s gets more difficult when you predict 5 years or more, but it’s also important to think ahead in this ever changing world, especially as a tech-person/programmer. 2020 is seven years from now. Here are my predictions (take with a healthy portion of salt):

8-cores and 16-cores become commonplace

Dual-cores are already showing up in our smart-phones, and due to the simple progression of an updated “Moore’s law”, we should have 16-cores in smart-phones and tablets by 2020. This will make concurrent programming more mainstream as many of us already know. So learning about modern concurrent programming approaches (agents, immutability, etc.) is important.

Multi-touch Becomes Standard

It seems almost self-evident that multi-touch will become the standard interaction model. Tablets and smart-phones are soaring in popularity, and multi-touch is even moving into the dwindling desktop and laptop markets. It makes sense then to stop assuming mouse and/or keyboard interaction if you deal with any sort of user-interface design.

3D Printing goes Mainstream

The cost of 3D printers is shrinking exponentially (as low as $500 at this time), while their abilities are improving.  Given the huge possibilities and fun that can be derived from a 3D printer, I think they will become as ubiquitous as the home ink-jet printer is today.

Linux has Arrived

With Ubuntu, Android, and ChromeOS, Linux is already forging ahead into mainstream technology. Does this effect you? Probably not if you’re a web-developer, but if you develop apps for Windows or OSX, I’d want to diversify if I were you. Linux (in its many different forms) might just (finally) disrupt the OS market.

Smart phones will be Old Hat

By 2020, it will have been 13 years since the first iPhone came out. To some people smart-phones are already old-hat; the big new thing is Google Glass.  By 2020 there will probably be several competitors to Google Glass, so the “glasses” market might look much like the smart phone market of today. Perhaps some brain-reading technology will be integrated by then.

Tags: tech future

Windows or Mac? or neither?

As Apple takes more and more of the OS market from Windows, there needs to be a valid alternative for people who dislike both Apple and Windows. Google has made a push in this direction, not only with Android tablets, but with Chrome OS (a browser/cloud based operating system) and the Samsung Chromebook. Meanwhile, Ubuntu is attempting to bring together the design sense of Apple, the affordability of Windows, and the openness of Linux (combined with their own commercial cloud offering, Ubuntu One).

With Apple’s launch of iCloud there’s a parallel battle for who’s going to store your data in the cloud. Will it be:

As more of our lives become digital, I believe data backup (in the cloud) will become increasingly important. From a consumer’s stand point, I’m glad to see there’s a lot of competition.

Tags: linux windows

This article is pretty close to my thoughts on Gamification.

Tags: gamify

Gamification

Although Gamification is a new buzz-word, it represents a concept as old as airline loyalty programs and buy-10-get-one-free programs.

The purpose of gamification is to engage customers and encourage the right behavior from them in a natural way.

“A good extrinsic motivation is a good map to intrinsic motivation. The better adesigner knows his players, resulting in a better game design, the less it will feel tothe player like being on a wheel, and the more it will feel like it was her idea to beginwith. That’s the holy grail of gamification: a game so well designed that the player’sactions just feel normal. We believe it can be done in almost any experience.” - Gamification by Design

In fact, saying the word “gamification” to our users could have a negative effect. They will probably think “This is serious; this is not a game.” But if you were to say, our product is easy to learn - or just let the product speak for itself - you will get a different response.

Tags: gamify ux

This is an old post, but still pertains today when considering what language to use. Dynamic languages, like Groovy, cannot support automatic refactoring like statically-typed languages do (Java, Scala, etc.). For some, this is a deal breaker. For me, it’s not a deal breaker, but it’s an important consideration.

Tags: languages ide