Arrays of Wisdom of the Ancients

A fascinating deep-dive into toArray(new T[0]) vs. toArray(new T[size]) by Aleksey Shipilëv:

Performance suggestions are funny things. Their shelf life is limited, especially if you do not understand the background for them and don’t bother to track how the world evolves. Their applicability is frequently questionable, and many suggestions do not have a track to verify the experimental results. That’s why, if you really want peak performance, you have to allocate a dedicated set of persons who are well-versed in vetting the performance advice for you.

But, most of the time, the straight-forward code is fast enough, so stop philosophizing about angels on the head of a pin, and get back to work. 99.9% of applications do not need a dedicated team of performance engineers; they need instead for their developers to focus on writing clear, maintainable code, and a little bit of measurement and tuning to file off the rough edges when the code fails to meet the performance requirements.

Amen.

Collection Pipeline

Martin Fowler:

"The reason people say that collection pipelines aren't a feature of OO programming is that the popular OO languages like C++, Java, and C# didn't adopt Smalltalk's use of lambdas, and thus didn't have the rich array of collection methods that underpin the collection pipeline pattern."

I still remember being struck by the elegance of Smalltalk during a "Programming History" module at University. Afterwards Java just felt a bit 'dirty' in comparison.

Thankfully Java has lambdas under its belt now so stream processing is at least bearable:

Observable.from(events)
    .map(eventDeserializer::deserialize)
    .subscribe(System.out::println);

Exciting times.

Wow, much responsive!

After recently working on a Bootstrap-powered project, I decided to jump in and give my site the overhaul it deserved. I am happy to say that it should now work better regardless of what device you are using.

I had updated the site a few months ago when I moved it from Tumblr and at the time I opted to keep a similar design but as time went on I grew to hate it. I feel like this new lighter look is much easier on the eye and much less gaudy.

There is definitely more work to do and hopefully along the way I will get around to writing more.

Groovy Environment Manager

I needed to install Groovy today and just when I was about to download the distribution I thought: why doesn’t something like rvm or virtualenv exist? I was pleasantly surprised when I discovered GVM.

Get up and running with:

$ curl -s get.gvmtool.net | bash

Travis CI

I set up my first Travis build last night and I cannot get over how simple it was. You can do it too, with any public GitHub repository, you just have to…

  1. Enable your repository on the Travis CI site,
  2. Add the .travis.yml build configuration to the repository - here's mine,
  3. …?

There is no step three, your project is already building!

Externalising properties in Scala

I have recently been working on a server-side app and I needed to externalise some configuration. I'm using Twitter's REST API and I wanted my API access-token and secret to be kept outside of the code. There are two main reasons for doing this, firstly because they should be kept private as they identify my app and the secret is used to sign API requests and secondly, if I ever needed to change them, I would want to do so without touching the code.

Ideally, what I wanted was to supply a configuration file in the Java Property format to my application using a system property, load the file in code and then look-up property values by key. Whilst I could have come up with something myself, I really wanted to focus on writing the app.

After a bit of searching I settled on Typesafe's Config project, a hidden gem which provided exactly what I needed.

Getting Config to do what I wanted was straight forward, the project is well documented and there are plenty of examples to learn from. I'm using SBT to build my project so initially I had to add the dependency to my build file:

libraryDependencies += "com.typesafe.config" % "config" % "0.3.0"

Next, to read an external property file, I needed to specify the system property config.file pointing to the location of the configuration file that contained my properties. For me this meant appending a Java option to my SBT build file:

javaOptions += "-Dconfig.file=../deployment/twitter.properties"

I also needed to fork execution so the option was picked up when running my application in SBT:

fork in run := true

Finally, in my code I needed to use the ConfigFactory to get a Config instance. This is then used to look up properties by key:

import com.typesafe.config.ConfigFactory

val conf = ConfigFactory.load()
val accessToken = conf.getString("accessToken")
val accessTokenSecret = conf.getString("accessTokenSecret")

Notice how the load call on ConfigFactory requires no arguments, this is because Config has a resolution mechanism which first looks for known system properties before searching for a known resource on the classpath. Dead simple.

Apart from loading properties from the file system, Config allows you load from a URL, specify your configuration in JSON or HOCON - a more convient super-set of JSON and supports more complicated scenarios such as configuration inheritence and merging.

Relying on convention over configuration I found Config brilliantly simple to set up and use and urge you to take a look.

Hacking Smart Meters

It's fascinating to see that they could determine what was being watched on the TV just by analysing the meter data.

How To Be More Interesting

Jessica Hagy writing for Forbes:

  1. Go exploring.

Explore ideas, places, and opinions. The inside of the echo chamber is where are all the boring people hang out.

Unfortunately it's one of those articles split over multiple pages, luckily it's just a short one.

Grand Designs

I had grand designs for the past year: I wanted to start a blog, help an Open Source project AND make an iPhone app.

Here I am a year and what happened? Well I've got a blog - but in name only, life got in the way of the Open Source project and I have no iPhone app. I recently came across the following Confucius quote:

The man who chases two rabbits, catches neither.

I wish I had read that a year ago.