JAW Speak

Jonathan Andrew Wolter

Archive for November, 2008

Introducting JAW’s Very Simple GWT Html Template

with one comment

Reading time: 2 – 4 minutes

Update 2009/7/29: This is a great way to see how GWT generators and deferred binding works, but for real work I recommend Google’s newish UiBinder (formerly called the Declarative UI). See this thread in the google group.


At a recent client of ThoughtWorks’, I was writing complex GWT views. If you’ve ever worked with Google Web Toolkit, you know the pain that this can involve. Instead of creating HTML files of templates, you need to build the DOM programatically with your own widgets, 3rd party widgets, or the basic widgets GWT gives you. I wrote a simple templating system to meet my needs. Maybe it will help you too, please feel free to request features and share if it helps you.

If you try JAW’s Very Simple GWT Template, all you need to do is follow a few conventions and you get the following.

  1. Create a html file with your complex layout. For instance a really fancy 12×12 html table with all sorts of colspans and rowspans for displaying detailed tabular data.
  2. Create a value object (in Java, on the GWT side) that you want to automatically use to dump some attributes into the template.
  3. In the html template, use ${valueObject.someProperty} style notation where you want automatic setting of values into the template.
  4. In the html template, use $${specialDoubleDollarSign} notation where you want to manually bind the template values.

Really, it is very simple and no frills. It solved my need of presenting data in a visually rich way, into html that a designer could produce without any knowledge of GWT. Also, you can embed it into other widgets, or other widgets into it.

I think it’s cool in two ways:

  1. Automatic data binding from your value object, into the ${myObj.property} template slots.
  2. Ability to manually attach complex widgets (with GWT defined event behavior), into the $${doubleDollarSignSlot} template slots.

I’ve created a Google Code project with the source code and some examples (as tests). Please take a look and leave criticism and feedback.

If you’re curious how this is implemented, it’s all with defered binding and generators in GWT. As all code I actually want to work, I’ve test driven it and you can look at the tests for details.

Written by Jonathan

November 29th, 2008 at 6:44 pm

Posted in code, java

Tagged with

Linking to Miško’s, Russ’ and my Testability Guide

with 3 comments

Reading time: 4 – 6 minutes

I had the great pleasure of collaborating with Miško Hevery and Russ Rufer in creating the Guide for Writing Testable Code. Please feel free to check it out, and leave comments here or on his blog.

Here’s a peek at the different flaws to watch out for, and warn your co-workers about. If you see this in code that is getting checked in, bring it to the attention to your team. See Miško’s post for the full list.

Flaw #1: Constructor does Real Work (link)

  • If you have a constructor that is doing “work” you’ve established a contract that everyone who wants to create this object also is forced to wait for that “work” to happen. This becomes a huge problem in small unit tests. Frequently unit tests create many, many instances of the object under test – and any work you do in the constructor slows down tests.
  • The solution? Break up the responsibility into two objects: a builder of factory to do all the heavy lifting to construct the object, and whatever the original object was that had the heavy constructor. When you split the responsibilities up you will have a better object oriented design, and make it easy for more flexible construction. If you’re tempted to create an init() method and put the work in there – avoid this siren song. It’s a smell of mixed responsibilities and a poorly behaving object that isn’t really ready when the constructor completes.
  • Warning signs are:
    • new keyword in a constructor or at field declaration
    • Static method calls in a constructor or at field declaration
    • Anything more than field assignment in constructors
    • Object not fully initialized after the constructor finishes (watch out for initialize methods)
    • Control flow (conditional or looping logic) in a constructor
    • Code does complex object graph construction inside a constructor rather than using a factory or builder
    • Adding or using an initialization block

Flaw #2: Digging into Collaborators (link)

  • Jumping from one object to another to another to get what you want makes for hard to test and hard to refactor code. This is commonly viewed as the Law of Demeter violation, however you can dig into collaborators without breaking the letter of the law. This is hard to work with in tests, because your tests need to do a great deal of setup stuffing objects in objects into other objects, or else you’ll get null pointers.
  • Warning signs are:
    • Objects are passed in but never used directly (only used to get access to other objects)
    • Law of Demeter violation: method call chain walks an object graph with more than one dot (.)
    • Suspicious names: context, environment, principal, container, or manager

Flaw #3: Brittle Global State & Singletons (link)

  • Global state (usually made possible through the static keyword in Java) creates hard to test and modify code. Parallelizing tests is often impossible, and tests that forget to clean up the global state after running interact undesirably with other tests (causing unexpected failures).
  • Warning signs are:
    • Adding or using singletons
    • Adding or using static fields or static methods
    • Adding or using static initialization blocks
    • Adding or using registries
    • Adding or using service locators

Flaw #4: Class Does Too Much (link)

  • Object oriented design allows you to split responsibilities into individually tested objects. Using an object oriented language does not prevent people from writing procedural code. In fact, most times when someone does not want to write tests it is because their code is hard to test.
  • Warning Signs:
    • Summing up what the class does includes the word “and”
    • Class would be challenging for new team members to read and quickly “get it”
    • Class has fields that are only used in some methods
    • Class has static methods that only operate on parameters

There’s typically no magic wand to wave and make hard to test code instantly testable. Engineers need to have an open mind and learn how to write code designed for testability. (In my experience 90% of the time this involves test driven design and test driven development.) These ideas above, and many of Miško’s other posts are a fantastic starting point for adopting that new way of thinking.

Please link it up with your favorite testability mindset readings.

Written by Jonathan

November 29th, 2008 at 5:26 pm

Recommended: etckeeper to keep all your server’s /etc in git (or bzr or hg)

with 4 comments

Reading time: 2 – 3 minutes

I’m using etckeeper to keep my /etc directory in version control (git).

When you make changes to a server, there are always risks. And even if you have a great backup strategy (I recommend backupninja), sometimes you change a config in the wrong way, which can ruin an otherwise great night. Why don’t we have an easy way to version control this very important configuration directory? (Ubuntu is working on it).

Here’s what $ sudo gitk will show you after a few changes in your /etc.
etckeeper with git

It’s no surprise that this comes from Joey Hess, who also wrote how to keep your life in svn. I was going to check into subversion all of /etc, but this has been a better experience so far: it has automatic hooks to do pre and post apt-get update commits. So you can more easily see what each package does to your configuration. Also, etckeeper takes care of permissions where svn can’t.

If you’re ready to install, here’s how you do it. I didn’t use the deb distribution because it is quite out of date, instead I pulled down the newest directly from Joey’s git repository.

$ sudo apt-get install git-core gitk
$ cd ~/
$ git://git.kitenet.net/etckeeper
$ cd etckeeper
( read the README and INSTALL files )
( possibly edit etckeeper.conf if you want to use bazaar or mecurial instead of git )
$ sudo make install  ( It's okay if it warns that bzr support not installed if you're not using it )
# sudo etckeeper init
$ cd /etc
$ sudo git status
$ sudo git commit -m "Initial checkin"

That’s it! Check out the README for day to day examples.

Whenever you use apt to install a program, it checks in any uncommitted changes, and then checks in the /etc after apt finishes. /etc is just a regular git repository (except for security permissions require using sudo), so you can also commit changes anytime you want.

Here’s another person’s take on etckeeper. New to Git? Check out Git for Computer Scientists.

Written by Jonathan

November 23rd, 2008 at 1:52 am

Posted in code

Tagged with ,

Using Hibernate UserType’s for oddly formatted legacy tables (with tests)

with 2 comments

Reading time: < 1 minute

If you are using Hibernate with an existing (ahem, “creative”) database schema sooner or later you will be stuck with a field that Hibernate can’t parse. Say there is a Dimension column which stores the integer dimensions as a string "003X001X010". That would be 3 by 1 by 10 units.

You can implement UserType to transparently convert a proprietorially formatted field into first class objects of your choice.

And you can do it in a testable manner.

I’ll show the tests first, then the custom UserType. Right after the bump. (Click the post permalink to read the rest).

Read the rest of this entry »

Written by Jonathan

November 10th, 2008 at 1:23 am

Posted in code, java

Tagged with

How to Send Picture Mail via SMS / MMS on the iPhone

with 6 comments

Reading time: 2 – 3 minutes

If you have an iPhone but have trouble figuring out how to send people text messages with pictures, or MMS messages, here’s a fix.

Go to the camera app and view the picture you want to send, click the send button and email it to one of the special email addresses below, based on their phone number and carrier. These are the SMS gateways from the carriers so the picture will be converted into a picture mail message and you’ll make somebody’s day (-;

Carrier Email to SMS gateway
Alltel full_phone_number@message.alltel.com
AT&T (Cingular) full_phone_number@txt.att.net
Boost Mobile full_phone_number@myboostmobile.com
Einstein PCS full_phone_number@einsteinmms.com
Sprint full_phone_number@messaging.sprintpcs.com
T-Mobile full_phone_number@tmomail.net
US Cellular full_phone_number@mms.uscc.net
Verizon Wireless full_phone_number@vzwpix.com
(@vztext is another, but it strips out pictures)
Virgin Mobile full_phone_number@vmobl.com

I hope this helps you.

Written by Jonathan

November 8th, 2008 at 4:11 pm

Posted in Uncategorized