Alex headshot

AlBlue’s Blog

Macs, Modularity and More

EclipseCon Europe and book codes

2014, conference, eclipse, eclipsecon

Unfortunately I wasn’t able to make EclipseCon Europe this year in Ludwigsberg. It sounds like it was a great conference, with the announcement of Eclipse Cloud Development and the new release of Orion.

To celebrate, I have managed to arrange a deal with my publishers for 25% off the retail price of Eclipse Plug-in Development for Beginners and Mastering Eclipse Plug-ins. Head to one of the two URLs and use the code to get it:

Codes are valid until 2nd November 2014.

GeekOut London

2014, conference, geekout

Today I attended the GeekOut UK conference, the first time (that I’m aware of) that it has been run in this country. Thanks to Simon Maple and the team who made it happen. It was hosted at the Shoreditch Villiage Hall, home of many small tech conferences.

First up was Martijn Veyburg who talked around code in IDEs and how it hasn’t changed much. He asked whether IDEs could focus on things other thank plain text representations, and linked to presentations such as Editing ASTs and videos such as this one. (Granted, most IDEs are textual — but in years gone past some IDEs did present code in non-textual form, but they gave way almost exclusively to command line tools like Ant that expected the files to be textual to be processed.) He also hoped that it would be possible for the IDEs to represent different kinds of techniques - from concurrent algorithms to reactive pipelines visualised as well as queueing systems like chronicle queue.

All in, an inspiring talk for what we might do in the future but optimistic none the less. Eclipse users might find looking at Code Recommenders might provide a guided view as to providing collaborative understanding of what code is popular.

Next up was Josh Long, who presented on Spring Boot as a runtime that could quickly host network services in an embedded fashion without needing a standalone web app server to host the content. More interesting for me was the introduction to start.spring.io which has a web page that acts like a maven archetype but in a way that can download a generated project infrastructure as a zip. Although having this functionality as a web page is neither repeatable or reliable, getting an initial project bootstrapped (with a collection of ‘click here to add Spring Foo’ dependencies) makes it a lot easier to get going.

Finally Josh introduced the metrics gathering (using Codahale metrics) built into Spring Boot Actuator - this adds a collection of metrics which can be used to add both JVM health information and also measuring the number of requests a particular set of URLs receives. More information is available at the actuator service guides.

The third session was presented by Aslak Knutsen who talked about JBoss Arquillian. He demoed the ability to write tests creating shrinkwrap archives — like the JAR or WAR archives in a build process would - but at runtime, similar to creating a Zip using java.util.zip and friends. The goal is to be able to spin up a partial webapp (e.g.) to install into a container such as a Jetty or Tomcat runtime to perform in-container testing but without requiring the entire server to be republished. There’s more information on the ShrinkWrap API as well as a more general guide to writing tests with Arquillian. Unfortunately, I think this is an example of the omega problem again though.

With that, Simon Maple took us up to lunch with an interactive demonstration of both JRebel and XRebel. JRebel provides automated code deploys by having an agent attached to a Java process during development, and wrapping the classes with methods that can delegate to internal representations of the classes in a way that they can be redeployed without taking down a server. He gave several excellent demos including being able to do refactorings that would not otherwise be possible. In addition JRebel has support for watching well-known-files (like a spring context file) and being able to perform a diff on the contents and take corrective action to be able to change the state of the system. Several plug-ins are available for systems that are able to be hot redeployed in this way, and an SDK is available which can be used to write plug-ins for systems that are either proprietary or otherwise not supported in JRebel out-of-the-box.

The XRebel tool looks interesting as well; again, it has a Java agent that can be bootstrapped into the runtime. The XRebel plugin amends web pages to inject an HTML menu on each page, and run-time monitoring to look for things like the size of the HTTP storage session (useful for poorly architected web apps that have massive sessions for no good reason) as well as monitoring the number and length of SQL statements that are executed. When demo’ing it against the sample JavaEE PetStore application, Simon demonstrated how XRebel was able to identify a memory leak leading to growth of the session size as well as several inefficient SQL queries. Support for rest services and NoSQL databases is currently available with more planned in future.

After lunch, Simon Ritter came up and talked about Java 8. Most of this is the goodness that already exists; streams, lambdas, method references, effectively final, default and functional interfaces etc. For those that haven’t upgraded to Java8 yet it was a good whistle-stop tour of what would be good; for me, I discovered the existence of (Long|Double)(Adder|Accumulator) classes that I hadn’t come across in my Java8 travels so far.

After Simon came Holly Cummins, who works on the Liberty Profile and loves to automate things. Lots of good stories here, including:

  • Do it once; do it a second time and take notes; do it a third time and automate
  • Treat your servers like cattle, not pets
  • If you care about it, automate it
  • Automate because you care that it’s done right
  • Automate because it’s fun

She also proposed thinking about when it’s not good to automate (and linked to XCKD automation)

  • Who can maintain/manage the automation
  • Will it be robust
  • How much will it cost
  • What is the completion criteria
  • Is it still worth it and does it save time
  • It’s dumb to automate dumb things
  • Automation allows you to do dumb things at a greater speed
  • Code formatting is the most controversial thing that you can automate

(I interviewed one of Holly’s colleagues, Erin Schnabel, on the development of the Liberty profile at InfoQ recently.)

Next up was Trisha Gee talking about how to build a killer web app using AngularJS and MongoDB. Though I’ve seen her talk before I was mesmerised by the way she fluently went through the application and came up with the implementation. If you’ve not seen it before, there’s a previous version of the presentation from Goto Chicago and the source code for Cafelito is available on GitHub. The Morphia drive for MongoDB (which provides persistence of JSON documents and provides bindings to Java classes, much like Hibernate does for SQL databases) seems like something worth paying attention to.

After Trisha was Chris Kelly on GitHub. I didn’t think this was the best GitHub presentation I’d seen (but then, I’ve seen a lot of them) but I did find out some keyboard shortcuts that I didn’t know before; for example, y will expand the URL to its canonical form (i.e. replace master with the appropriate commit hash) and l can be used to select either a single line or a range of lines (e.g. 10-15). This will append #L10-15 to the URL, such as …/LICENSE.html#L32-35.

I also found t will allow you to search for a file name in a repository; unfortunately it doesn’t work with extensions (GitHub currently uses . as a separator, so it’s not seen in searches) and w will allow you to switch branches/tags. Both of these are great if you’re trying to debug what is happening in an open-source environment where you have source in GitHub.

Finally Sven Peters finished off the day with coding culture at Atlassian. A slightly different version of the presentation is available at his site, but was an inspirational overview of how you can build effective teams by becoming a DOmocracy and shipping code quickly. They even have a quarterly company-wide ship-it day, which is a competition to show off what you can do in 24h.

I think he summarised the whole thing with “Just say no to crappy code”.

All in all, GeekOut UK was great fun, with a good balance of speakers and presenters. It was also great being a single day; sometimes it’s a lot easier to get a day pass to attend a conference rather than a full week. The fact that it’s much reduced means that a lot of conference cruft - keynotes, finding out where all the talks are - became moot. I’ll definitely be there next time it runs.

Swift - classes

2014, mac, swift

In this episode of the swift introduction series we’ll be looking at classes in swift.

Swift is a language that supports both functional and object oriented concepts. Classes are defined with the class keyword and the body is surrounded in braces.

Objects can have instance variables specified as either var or constants with let inside the block. As with other variables, the type can be explicitly declared or can be inferred by an initial value:

Instance variables
1
2
3
4
5
Welcome to Swift!  Type :help for assistance.
  1> class Person {
  2.   var firstName = ""
  3.   var lastName:String?
  4. }

Non-optional values need to be initialized in-line, or in an initializer.

Once the class is defined then can be created using a type constructor. Instance properties can be accessed using dot syntax:

Instantiating classes
1
2
3
4
5
6
7
8
9
10
11
  5> var alex = Person()
alex: Person = {
  firstName = ""
  lastName = nil
}
  6> alex.firstName = "Alex"
  7> alex
$R0: Person = {
  firstName = "Alex"
  lastName = nil
}

Class types use pass-by-reference semantics; two variables can point to the same instance, so if one variable is used to change the value, then it is seen through the other variable as well:

Class types are pass by reference
1
2
3
4
5
6
7
8
  8> var alblue = alex
alblue: Person = {
  firstName = "Alex"
  lastName = nil
}
  9> alblue.firstName = "AlBlue"
 10> alex.firstName
$R1: String = "AlBlue"

Instance functions can be added to a class. As a result, it’s possible to add a fullName method that returns the concatenation of the first and last names:

Instance methods
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 11> class Person {
 12.   var firstName = ""
 13.   var lastName:String?
 14.   func fullName() -> String {
 15.     return "\(firstName) \(lastName!)"
 16.   }
 17. }

 18> var alex = Person()
$R2: Person = {
  firstName = ""
  lastName = nil
}

 19> alex.firstName = "Alex"
 20> alex.lastName = "Blewitt"
 21> alex.fullName()
$R3: String = "Alex Blewitt"

A special kind of function can be created which is used at class creation time, called the initializer. Fields that are assigned in the initializer do not need to be initialized at the declaration. However, they do need to be declared for a type perspective:

Initializer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 22> class Person {
 23.   var firstName:String
 24.   var lastName:String
 25.   init(first:String, last:String) {
 26.     firstName = first
 27.     lastName = last
 28.   }
 29.   func fullName() -> String {
 30.     return "\(firstName) \(lastName)"
 31.   }
 32. }

 33> Person()
error: missing argument for parameter 'first' in call
Person()
      ^

 33> Person(first:"Alex", last:"Blewitt")
$R4: Person = {
  firstName = "Alex"
  lastName = "Blewitt"
}

The arguments are implicitly named; if you try and call it without passing in names (such as Person("Alex", "Blewitt")) then swift will complain that the labels are missing. This can be fixed by using an underscore _ in front of the labels of the init, which instructs swift to not consider them named:

Dealing with positional instance initializers
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 34> Person("Alex", "Blewitt")
error: missing argument labels 'first:last:' in call

 34> class Person {
 35.   var firstName:String
 36.   var lastName:String
 37.   init(_ first:String, _ last:String) {
 38.     firstName = first
 39.     lastName = last
 40.   }
 41.   func fullName() -> String {
 42.     return "\(firstName) \(lastName)"
 43.   }
 44. }

 45> Person(first:"Alex", last:"Blewitt")
error: extraneous argument labels 'first:last:' in call
Person(first:"Alex", last:"Blewitt")
      ^~~~~~~        ~~~~~

 45> Person("Alex", "Blewitt")
$R5: Person = {
  firstName = "Alex"
  lastName = "Blewitt"
}

With swift 1.1 (released as part of Xcode 6.1) the initializers can now be failable, implying that they can return nil if the arguments do not permit creation.

Failable initialzers
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 46> class Person {
 47.   var firstName:String
 48.   var lastName:String
 49.   init?(_ first:String, _ last:String) {
 50.     firstName = first
 51.     lastName = last
 52.     if first == “” || last == “” {
 53.       return nil
 54.     }
 55.   }
 56.   func fullName() -> String {
 57.     return "\(firstName) \(lastName)"
 58.   }
 59. }

 60> Person(“”,””)
$R6: Person? = nil
 61> Person("Alex", "Blewitt")
$R7: Person? = {
  firstName = "Alex"
  lastName = "Blewitt"
}

Since the initializer may return nil the return type is treated as an Optional which means that it can be used in an if let construct or equivalent. Note that in swift 1.1 there is an error message “all stored properties of a class instance must be initialized before returning nil from an initializer” which is a little bit crazy; it’s like saying that you have to be able to fully construct the object anyway even if the arguments don’t make sense. Hopefully that will be fixed in a future version.


We’ll go into more detail about classes in future episodes. In next week’s episode, we’ll look at how to create structs in swift. To subscribe to this series, add the Swift tag feed to your reader.