Archive for November, 2009

How to Work with Git & GitHub 0

Based on Alex’s comment on the Leiningen group, here’s my shiny new setup for working with Git & GitHub on OS X, I’m posting it here then it’ll be easy for me to refer back to in future…

Installing the Tools

  1. install MacPorts if you don’t already have it;
  2. get an up-to-date version of ruby and rubygems with sudo port install rb-rubygems;
  3. install the github tool with sudo port install json github; and
  4. upload your public key to your GitHub account.

Creating a New Project

Run github create project_name to create a public project, append --private to create a private project (you will need a paid account at GitHub for this).

Forking

Run github fork project_owner project_name, for example to create a fork of the Cucumber testing framework run github fork aslakhellesoy cucumber.

Making Changes

Make your changes in a branch, this took me a little while to get used to at first as it’s different from the normal way of working with version control systems like CVS.

$ cd project_dir
$ git checkout -b feature_name

Make your changes, then run git commit -a -m "Comment." to commit them. From time to time push the branch to github so others can see what I’m working on git push origin feature_name.

If the work is in a fork then you’ll also want to send a pull-request back to the original project with github pull-request project_owner feature_name.

All from the command line, neat huh? For more tricks, you can also browse issues with github issues {open|closed}.

Choose Your Own Visual Adventure 0

A really nice set of visualisations of the old Choose Your Own Adventure books, I was a big fan of the English equivalents when I was a kid.

Strawman Arguments and Coding Styles 0

So there’s this blog post over on the Best in Class blog that talks about ceremony in programming languages and compares Clojure with Java on this basis. While I’d agree with the basic premise of the article (that there is less ceremony in Clojure), I’m less keen on the way it’s presented: by way of a needlessly verbose strawman example. To be fair the article does kind of admit that this is what is being done, but it’s still annoying.

With this in mind let’s see how well we can do with the Java version of the code, relying on a better coding style and a couple of freely available libraries (one of the platforms much touted strengths). For the original — 28 line — version of the code I’ll refer you to the original post (but warn you that it’s presented in that well known code storage format, PNG!).

The same code rewritten in a smarter manner, but still using only the core Java libraries. This gets it down to 10 lines of code and also makes the intent of the code clearer. There’s still a fair amount of ceremony about this however: the multiple imports, and all of the class and static main method boilerplate.

import java.util.Arrays;
import java.util.Set;
import java.util.HashSet;
 
class Distinct {
  public static void main(String... args) {
    Set<String> distinct = new HashSet<String>(Arrays.asList(new String[] {
        "foo", "bar", "baz", "foo"
    }));
  }
}

Let’s see if we can’t do a little better with the addition of some open source libraries. Enter Google Collections, a really neat library that improves the collections API from the JDK. We’re now down to 7 lines of code, and 2 of those are just closing braces! In any reasonably size program the class and main statements disappear into the noise, so we’re really saying that we have 2 import statements and a single line of code. That’s not too different from the Clojure version all things considered.

import java.util.Set;
import static com.google.common.collect.Sets.newHashSet;
 
class Distinct {
  public static void main(String... args) {
    Set<String> distinct = newHashSet("foo", "bar", "baz", "foo");
  }
}

It’s interesting to note that the second Java version weighs in at 10 lines of code, versus 8 for the equivalent clojure version; not much of a difference really.

I think that the benefits of Clojure come from it’s functional style, macro system, and excellent concurrency support — not from the fact that you can save a few lines of code here and there.