Sunday, May 27, 2012

MyBatis Koans: A Path to Enlightenment


/*---[ MyBatis Koans ]---*/

For much of this month, I've been working on developing a set of koans for the MyBatis data mapper framework. I now have enough completed and tested that I'm sending out notices for others to try them out: https://github.com/midpeter444/mybatis-koans

The first koans I ever tried are the excellent and challenging set of Neo4j koans by Jim Webber and colleagues, which was the subject of my first blog post.

Then I tried the Clojure koans (and then finished about half of the even more challenging 4Clojure koans).

A koan is a question or statement to be meditated upon in order to improve and test a student's progress. Among programmers, software koans have become a clever way to learn a software language or tool. As the Ruby koan website says: "The Koans walk you along the path to enlightenment" -- in this case to learn and practice with the MyBatis 3 data mapper framework.

A software koan comes in the form of a broken unit test that you must fix to get it to pass, usually by filling in the blanks or entire missing sections. The koan is intended to teach one or a small set of cohesive features about the language or tool being studied.


/*---[ Rationale ]---*/

The MyBatis data mapper framework doesn't have the mindshare in the Java world that it should, so this and my earlier "User Guide Companion" are my contributions to helping others learn to use it. I hope others will suggest and contribute new koans.

I'm actually not a MyBatis expert and writing these was my way of learning the framework better. Critiques and suggestions for improvements are welcome.

The koans focus on just the MyBatis Persistence Framework. Be aware that the MyBatis project has other features available such as a Migration Tool and code generator.


/*---[ Getting Started ]---*/

On the MyBatis koans github page, I've tried to provide detailed instructions on how to get set up and do each koan. As this is a relational database mapping framework, you will need to set up a database and install the sakila example schema and dataset.

I've tested the koans with PostgreSQL and MySQL. See the GitHub page for details.


[26-July-2012 Update]: Thanks to some help from collaborator Andrei Pozolotin, the koans have been mavenized to make it easier to get dependencies in place. Andrei also added the Java H2 database with the sakila database set up so you can get started without having to install an enterprise grade database.

Sunday, April 8, 2012

Git as RCS: The Joy of Personal Incremental Savepoints


/*---[ Distributed Version Control: The Best of Both Worlds ]---*/


"The enjoyment of one's tools is an essential ingredient of successful work."
— Donald E. Knuth

Not long after I first learned to program and gain enough proficiency with emacs to start to enjoy it, the person who was closest to what I would call a mentor told me that my next step was to learn to use a version control system. Having gotten me started as an emacs user, he suggested that I use RCS: Revision Control System from GNU.

RCS was developed in the 1980s. It is "local-only" - it has no concept of a central server. It also works on single files, not a project of files. It's been a while since I used RCS and I never really deeply mastered it, but I used it as a tool to make sure I had "version backups" of important files that I was working on. I never used it as part of a team.

If I have my history right, CVS, in the open source world, was the next generation version control system after RCS. CVS built a model where you could centrally share version controlled files with members of a team and think in terms of projects, not just individual files. Of course I'm ignoring lots of other version control systems, mostly proprietary. I'm just giving a little history based on my own personal history, as the next thing I learned after RCS was CVS (and then a very brief descent into ClearCase and then SVN and now git).

Nevertheless, with RCS I remember well a sort of thrill at the idea of having this local, personal manager of my history of edits. It could be used as a set of savepoints on the way to completing a program. CVS always felt more heavyweight (which sounds insane compared to ClearCase and some other tools), because it wasn't local. I couldn't put my finger on why back then, but now that I've used git I have achieved the next rung on the path to software guru enlightenment (... is there a special badge for that on coderwall?).

Git has been my first experience with a distributed version control system. Soon after trying it, the primary emotion I experienced was that initial joy of my brief RCS-days. I have this wonderfully powerful intelligent version control system and I can run it completely locally: standalone, off-the-network doing all the things RCS used to do and far far more. All my personal savepoints are back. I can use it as a local lever to help try new things, back out of messes and alter history as appropriate before I push it to some public or shared repo.

This is not possible with the CVS/SVN-type model. There everything you commit is immediately shared. Worse, if your team has a rule that you can't publish to the repo partial code that breaks the build, you can't do any local savepoints. It's analogous to being in a video game where you can only save your progress after you finish some level, rather than incrementally at any point you choose. Perhaps that's an intended extra challenge of some video games, but we need to have our version control system make software development easier, not harder.


/*---[ Lots of small savepoints that git calls commits ]---*/

If you come from the mindset of a non-distributed version control system, like SVN, a commit is a serious things. You are publicly pushing your changes to a central repo off of which (ideally) some automated Continuous Integration tool pulls your changes and runs unit tests, static code analysis tools, perhaps functional tests and even performance tests. You'd better have your act together when you commit or there will be finger-pointing!

A commit in git is far less ominous. It just means I'm saving my work to my local repo, just like saving my progress in that video game. The git equivalent of an SVN-commit is a push. This is the beauty of git. Purists would disagree, but if you are learning git, it can be helpful to think of it as both RCS and CVS combined together with a much more intelligent tree (DAG) based view of commits.


/*---[ Using git rebase, amend and reset to alter local history ]---*/

I struggle with CSS. In fact, one of my all-time favorite blog posts is from Zed Shaw on the joys he experiences with CSS. For fun, I'll quote my favorite passage (with a bit of editing of Zed's flowery language):

My first problem with CSS is simply that it just never does what you tell it to. I say, "make this a column CSS" and it goes, "What? No that should go over here totally on the left and [deleted] you I like apples." I say, "make this fill all of the parent div" and CSS says, "Sharks love tiny needles, and no that will only take up the top part." I say, "Hey, CENTER THIS" and CSS says, "My shoes have centered worms but your heading will stay to the left."

In the battle with CSS, git is your friend. Any time you achieve a small success and get what you want, commit it to your local repo and put in lots of exclamation points of celebration or cursing in the commit message, because later you can alter this history (more on that later). Now with the save point, you can start the next salvo against CSS. At some points, you may be in so far over your head that the only sound tactic is full-scale retreat. git reset is your path back to sanity. Or you can make a branch and try some insane experiment that just might work. Branches are cheap and easy in git.

Another great value of doing lots of little commits is that not only is backing out with a reset not a big deal, but you can do a git diff to see only the very few changes you've recently made and quickly grok the problem. This can help you decide whether to trudge on and change what you have or back out entirely.

In the heat of battle, you might make 20 commits. That's going to look a little messy and honestly just a bunch of repo noise when you finally push that out. git commit --amend and git rebase stand ready to save the day again.


/*---[ A short tutorial on using git "RCS-style" ]---*/

If you haven't used git or these features of git, this might sound intriguing, but how do I actually do all that? Below I present a tutorial to illustrate. For this I assume you have a basic understanding of how git works. If you are new to git or want more information there are plethora of good git books and web sites. Start with these:

To start, I have a very simple git repo with an initial commit of my html and css file:

$ tree
.
|-- css
|   |-- style.css
|-- main.html

$ git log
commit 1fdf8ad40a67140803eea81effaee4a4fabf29f6
Author: Michael <blahblah@gmail.com>
Date:   Sun Apr 1 19:50:23 2012 -0400

    Init. CSS Design in Progress

Now, I open main.html in the browser and style.css in emacs. Fiddle fiddle fiddle. The changes work reasonably well and I commit - only after a few minutes.

$ git add .
$ git commit -m "Intmd commit: Changed width of main divs to 600.
$ git log    # just to show you the messages
commit 27fcb7fef6a2ff007f9ba313db9574d7d0036be9
Author: Michael <blahblah@gmail.com>
Date:   Sun Apr 1 19:52:35 2012 -0400

    Intmd commit: Changed width of main divs to 600.

commit 1fdf8ad40a67140803eea81effaee4a4fabf29f6
Author: Michael <blahblah@gmail.com>
Date:   Sun Apr 1 19:50:23 2012 -0400

    Init. CSS Design in Progress

If I know that I'm going to subsume this commit into one larger commit, then the commit message will be changed later. It's always smart to say what and why you did what you did, but I also often put in "intermediate commit" so I can see that I didn't intend on keeping this little savepoint.

So I continue and make three more commits over the next few minutes. Here's the log after that:

$ git log    
commit 4e4666204e2a82c257e1fdcc5bb5ab5abdf4ca56
Author: Michael <blahblah@gmail.com>
Date:   Sun Apr 1 19:54:38 2012 -0400

    Intmd commit: header: has bottom border, padding decent now.

commit ac05681b6baff0ec3442f0880a00e31f98439241
Author: Michael <blahblah@gmail.com>
Date:   Sun Apr 1 19:53:56 2012 -0400

    Intmd commit: Header region: so far width + height good.

    Set background color to white - may want to adjust later.

commit 27fcb7fef6a2ff007f9ba313db9574d7d0036be9
Author: Michael <blahblah@gmail.com>
Date:   Sun Apr 1 19:52:35 2012 -0400

    Intmd commit: Changed width of main divs to 600.

commit 1fdf8ad40a67140803eea81effaee4a4fabf29f6
Author: Michael <blahblah@gmail.com>
Date:   Sun Apr 1 19:50:23 2012 -0400

    Init. CSS Design in Progress

Now that the basic layout is done, I start the hard stuff. One smart move here would be to make a new branch and futz around on that. If I don't like it, just move back to the main ("master") branch and delete the branch that didn't work. But since this isn't a tutorial on git branching, I'll stick to the master branch.

So on master I get to it. [Work, work, curse, futz, work ~~CSS pain~~]. OK, this isn't working. Let's dump this and get back to where we were. For that we will use git reset. There are actually three flavors of git reset, just to make life interesting.

The three flavors are:

  1. git reset --soft
  2. git reset (same as git reset --mixed)
  3. git reset --hard

Which one do we want? To explain the difference takes a full blog post on its own. You have to understand all the "trees" in git of which there are three (actually there are three locally and at least a fourth one if you have a remote, often centralized, repo).

But here is my simplified explanation. After reading Scott Chacon's blog (the one above) and reading some of the comments to the post, I have set up the following aliases for reset:

uncommit = reset --soft HEAD~
unstage = reset HEAD

git uncommit (my alias for git reset --soft HEAD~) means to move HEAD from pointing at the last commit I did, to its parent (the commit before it). Thus, "undo the last commit to the repo" or "uncommit".

git unstage (my alias for git reset HEAD) means to make the files in the index (staging area) look like what was in the last commit. So if I've done git add to add new changes/files to the index and then I run git unstage, those changes are removed from the staging area and the index now looks like the previous commit (which is what HEAD points to).

Neither of the above commands affect my working directory. To do that I have to issue git --hard reset, which I leave unaliased, since the "hard" should remind me that I am about to overwrite my working directory with the files from HEAD the last commit. (git --hard reset is the same as git --hard reset HEAD: HEAD is assumed if you don't provide an argument to reset.)

Only git reset --hard is destructive and will cause you to lose work, since it will overwrite your working directory, so use this command carefully.

So, which one do I want here? In my case working with the CSS, since I've been keeping savepoints every few minutes, throwing away the last few minutes of non-productive work is a blessing. I can I always go back to the previous savepoint, just like in a video game where if my player dies or loses too many energy points trying to get past some hurdle, I can just hit the reset button and try again.

So we are going to hit the reset button "hard":

$ git status -s
M css/style.css
$ git reset --hard
HEAD is now at 4e46662 Intmd commit: header: has bottom border, padding decent now.
$ git status -s

The second time I run git status -s it shows no output because the working directory has been reset to look like the previous commit.

In this case, because I was only dealing with one file and I had not yet staged it with git add ., I could have also done git checkout -- css/style.css and it would have checked out the last version of style.css from the index. That's the key thing to remember about git checkout - it pulls from the index, not the commit repo.


/*---[ Rolling up lots of little commits ]---*/

So now, suppose after an hour of toil and labor with CSS, I have something close to what I hoped for. In the process I've done 9 commits, with lots of "Intmd commit" prefixes in the comments. I want to remove those little commits and roll them into one big commit before I push them to the remote repo and make them "public" (or least "shared").

Here's an abbreviated view of all 9 commits using:

$ git reflog
74f7e9a HEAD@{0}: commit: Like how it looks now. Ship it!
14efb50 HEAD@{1}: commit: Much pain, but progress: header.css along with adjustments to main.html.
02ed6fc HEAD@{2}: commit: Intmd: header is shaping up. More to do.
85e8dcb HEAD@{3}: commit: Split out header styles from style.css into header.css.
3ab3101 HEAD@{4}: commit: Intmd commit: Got buttons styled well now.
4e46662 HEAD@{5}: commit: Intmd commit: header: has bottom border, padding decent now.
ac05681 HEAD@{6}: commit: Intmd commit: Header region: so far width + height good.
27fcb7f HEAD@{7}: commit: Intmd commit: Changed width of main divs to 600.
1fdf8ad HEAD@{8}: commit (initial): Init. CSS Design in Progress

I'd like to rollup the last 8 commits, so I do:

$ git rebase -i HEAD~8

or I can specify a particular SHA-1 commit id. Here I choose the SHA of the initial commit:

$ git rebase -i 1fdf8ad

I asked for an interactive rebase (-i), so it opens an editor with all the commit messages from those 8 commits:

pick 27fcb7f Intmd commit: Changed width of main divs to 600.
pick ac05681 Intmd commit: Header region: so far width + height good.
pick 4e46662 Intmd commit: header: has bottom border, padding decent now.
pick 3ab3101 Intmd commit: Got buttons styled well now.
pick 85e8dcb Split out header styles from style.css into header.css.
pick 02ed6fc Intmd: header is shaping up. More to do.
pick 14efb50 Much pain, but progress: header.css along with adjustments to main.html.
pick 74f7e9a Like how it looks now. Ship it!

# Rebase 1fdf8ad..74f7e9a onto 1fdf8ad
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.

What this is asking is for you edit the commands in front of each commit. To merge (roll up) an intermediate commit into the previous one, change it to "squash". The key to making this work is to squash all the commits except the first one listed. If you squash them all, you will not get what you expect. Here is the rebase editor screen after I squash ("s") all but the first one listed:

pick 27fcb7f Intmd commit: Changed width of main divs to 600.
s ac05681 Intmd commit: Header region: so far width + height good.
s 4e46662 Intmd commit: header: has bottom border, padding decent now.
s 3ab3101 Intmd commit: Got buttons styled well now.
s 85e8dcb Split out header styles from style.css into header.css.
s 02ed6fc Intmd: header is shaping up. More to do.
s 14efb50 Much pain, but progress: header.css along with adjustments to main.html.
s 74f7e9a Like how it looks now. Ship it!

# Rebase 1fdf8ad..74f7e9a onto 1fdf8ad

Now I save. Git does the rebase and then pops up another editor window that says:

# This is a combination of 8 commits.
# The first commit's message is:
Intmd commit: Changed width of main divs to 600.

# This is the 2nd commit message:

Intmd commit: Header region: so far width + height good.

Set background color to white - may want to adjust later.

# This is the 3rd commit message:

Intmd commit: header: has bottom border, padding decent now.

# This is the 4th commit message:

Intmd commit: Got buttons styled well now.

# This is the 5th commit message:

Split out header styles from style.css into header.css.

# This is the 6th commit message:

Intmd: header is shaping up. More to do.

# This is the 7th commit message:

Much pain, but progress: header.css along with adjustments to main.html.

# This is the 8th commit message:

Like how it looks now. Ship it!

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# Not currently on any branch.
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   new file:   css/header.css
#   modified:   css/style.css
#   modified:   main.html

This is your chance to modify the commit message to the one "squashed" single commit you just created. Here's how I edited the commit message:

Like how it looks now. Ship it!

Header region: so far width + height good.
Set background color to white - may want to adjust later.
header: has bottom border, padding decent now.
Got buttons styled well now.
Split out header styles from style.css into header.css.

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# Not currently on any branch.
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   new file:   css/header.css
#   modified:   css/style.css
#   modified:   main.html

After the rebase finishes I get the message:

 3 files changed, 19 insertions(+), 3 deletions(-)
 create mode 100644 css/header.css
Successfully rebased and updated refs/heads/master.

Now when I run git log, you see that the savepoints I made have all been squashed into one master commit:

$ git log
commit f52b1c2ba8543c999ba00e862af829e440f0b027
Author: Michael <blahblah@gmail.com>
Date:   Sun Apr 1 19:52:35 2012 -0400

    Like how it looks now. Ship it!

    Header region: so far width + height good.
    Set background color to white - may want to adjust later.
    header: has bottom border, padding decent now.
    Got buttons styled well now.
    Split out header styles from style.css into header.css.

commit 1fdf8ad40a67140803eea81effaee4a4fabf29f6
Author: Michael <blahblah@gmail.com>
Date:   Sun Apr 1 19:50:23 2012 -0400

    Init. CSS Design in Progress

Now I'm ready to push it to the central or community repo (such as GitHub).


/*---[ Caveats ]---*/

A caution about rebase - it is very powerful and used for more things than shown here. If you do it wrong, you can create a bit of mess to clean up (but you can always recover, just don't panic). But there are two things to be very careful of:

  1. Don't delete commits from the rebase message message when doing interactive rebase. Note the message in the editor during an interactive rebase that says "# If you remove a line here THAT COMMIT WILL BE LOST."
  2. Do not rebase past where you have already pushed to the central or community repo. If I've done 10 commits, and only the last 2 are local, then don't rebase farther back than the last two commits.

Lastly, I recommend you read the Rewriting History chapter of Pro Git in order to be clear on what you are doing with rebase.

Once you get comfortable with it, rebase gives you freedom to use git with any level of mini-savepoints you want and rewrite your local history to reduce repo noise. Here is a good example of where a version control system is giving you freedom, rather than restricting it.

[Update: 08-Apr-2012]: On Hacker News today there is a link to an article about the "many faces of git rebase". Worth reading in combination with my post to get a feel for how rebase can be used.

Tuesday, March 27, 2012

Refactoring to Java Enums

There are only two books (so far) I've ever read three times in full: Dune by Frank Herbert and The Pragmatic Progammer by Dave Thomas and Andy Hunt. From these you could rightly infer what I consider the pinnacle of SF and programming/software engineering literature. (Cryptonomicon will certainly join this list at some point.)

There is a much larger number of books I've read twice in full, so I'll spare you (and myself) the tedium of listing them here.

Then there are those "reference style" books where you have read parts of them dozens of times and the whole thing through at least once, maybe twice. Two that fall into that category for me are Josh Bloch's Effective Java and Martin Fowler's (et al.) Refactoring.

In fact, I recently just re-read Effective Java cover to cover and decided to read most of Refactoring again. The former, with the 2nd edition, has been updated to use many of the latest features of Java 5 and 6. The latter has not.


/* ---[ Java 5 enums: powerful, underutilized ]--- */

One of the themes in Effective Java (and there are many) is that Java enums are powerful and underutilized. For example, in Item 30 Bloch says:

Java's enum types are full-fledged classes, far more powerful than their counterparts in these other languages [C,C++,C#], where enums are essentially int values....

In addition to rectifying the deficiencies of int enums, enum types let you add arbitrary methods and fields and implement arbitrary interfaces. They provide high-quality implementations of all the Object methods, [and] they implement Comparable and Serializable...

So why would you want to add methods or fields to an enum type? For starters, you might want to associate data with its constants.... You can augment an enum type with any method that seems appropriate. An enum type can start life as a simple collection of enum constants and evolve over time into a full-featured abstraction.

While reading Chapter 1 of Refactoring, I felt the urge to update the example Fowler works through - not only to replace Vectors and Enumerations with Lists and Iterators, but in particular to explore the power of the Java enum.


/* ---[ Refactoring, Ch. 1, in brief ]--- */

So today I focus on the section of Ch. 1 where Fowler is extracting a switch statement into the State pattern and replacing a conditional with polymorphic classes.

You may want to peruse that chapter again to refresh yourself on the context (if you don't own the book and you are a programmer, you should obtain one post-haste), but I will review the part that is relevant to the refactoring of Fowler's code that I did.

Fowler starts with a Movie class that uses the old int constants style to doing enums to distinguish between three types of Movies that can be rented at a video rental store (and thus the example is showing its age in more ways than one).

package refactor;

public class Movie {
  public static final int REGULAR = 0;
  public static final int NEW_RELEASE = 1;
  public static final int CHILDRENS = 2;
  private String _title;
  private int _priceCode;

  public Movie(String title, int priceCode) {
    _title = title;
    _priceCode = priceCode;
  }

  public int getPriceCode() {
    return _priceCode;
  }

  public void setPriceCode(int arg) {
    _priceCode = arg;
  }

  public String getTitle (){
    return _title;
  };
}

At the starting point of the refactoring exercise, there are three classes: Customer, Rental and Movie. A Customer can have multiple Rentals and a Rental (in this case) is allowed to have only a single Movie. Only Customer has any business logic, the other classes are just value objects.

Fowler proceeds to refactor functionality out of Customer, first into Rental and then into Movie. In particular, since the Movie class has the "int enum" of REGULAR, CHILDRENS and NEW_RELEASE, he refactors Movie to provide two pieces of functionality that were originally in the Customer class:

  • getCharge: calculates the charge for renting the movie, which varies by Movie enum type
  • getFrequentRenterPoints: calculates the rental agency's bonus "frequent renter" points, which also varies by Movie enum type

So far he has basically been changing the location of a switch statement based on the int constants in Movie.

  switch (rental.getMovie().getPriceCode()) {
  case Movie.REGULAR:
    thisAmount += 2;
    if (rental.getDaysRented() > 2)
      thisAmount += (rental.getDaysRented() - 2) * 1.5;
    break;
  case Movie.NEW_RELEASE:
    thisAmount += rental.getDaysRented() * 3;
    break;
  case Movie.CHILDRENS:
    thisAmount += 1.5;
    if (rental.getDaysRented() > 3)
      thisAmount += (rental.getDaysRented() - 3) * 1.5;
    break;
  }    

After he has the switch statement in the right class, now he works to replace the conditional with polymorphism. He decides that the right way to do it is not to make subclasses of Movie (e.g., RegularMovie, ChildrensMovie, etc.), because a specific movie can change its type while the application is running (e.g., from "New Release" to "Regular"). Thus, he uses the Replace Type Code with State/Strategy refactoring pattern.

In the end, he creates an abstract Price state class that is used by Movie to implement the state-dependent methods getCharge and getFrequentRenterPoints. Here is the UML diagram:

State Pattern Using Price class

Here is the code for the abstract Price class and its concrete subclasses:

// in Price.java file
public abstract class Price {
  public abstract int getPriceCode();
  public abstract double getCharge(int daysRented);
  public int getFrequentRenterPoints(int daysRented) {
    return 1;
  }
}

// in ChildrensPrice.java file
public class ChildrensPrice extends Price {

  @Override
  public int getPriceCode() {
    return Movie.CHILDRENS;
  }

  @Override
  public double getCharge(int daysRented) {
    double result = 1.5;
    if (daysRented > 3)
      result += (daysRented - 3) * 1.5;
    return result;
  }

}

// in NewReleasePrice.java file
public class NewReleasePrice extends Price {

  @Override
  public int getPriceCode() {
    return Movie.NEW_RELEASE;
  }

  @Override
  public double getCharge(int daysRented) {
    return daysRented * 3;
  }

  @Override
  public int getFrequentRenterPoints(int daysRented) {
    if (daysRented > 1)
      return 2;
    else
      return 1;
  }
}

// in RegularPrice.java file
public class RegularPrice extends Price {

  @Override
  public int getPriceCode() {
    return Movie.REGULAR;
  }

  @Override
  public double getCharge(int daysRented) {
    double result = 2;
    if (daysRented > 2)
      result += (daysRented - 2) * 1.5;
    return result;
  }
}

Note that Price is an abstract class, rather than an interface. Because the getFrequentRenterPoints method is shared between two of the subclasses, it has been pulled up into the abstract base class. When I refactor this code further, you'll see that we can duplicate this behavior with an enum as well.

In the end, Fowler has achieved a clean design that adheres to the open/closed principle. It is open for extension by adding new subclasses to Price and it is closed for modification in that one never needs to modify the Price class in order to add a new type or state.


/* ---[ Refactoring to use Java 5 enums ]--- */

It is easy to see how to use a Java 5 enum for the original code (before Fowler's refactoring). It would just be a matter of replacing this:

public class Movie {
  public static final int REGULAR = 0;
  public static final int NEW_RELEASE = 1;
  public static final int CHILDRENS = 2;

  ...
}

with this:

public class Movie {

  public enum Price {
    REGULAR, NEW_RELEASE, CHILDRENS;
  }
  ...
} 

... and then refactoring the dependent classes to refer to Movie.Price.REGULAR, etc.

And that is how most people, from what I've seen and read, use Java enums. But as Bloch said earlier, Java enums are much more powerful than that. They are type-safe full-fleged Java immutable (final) classes where each enum entry (REGULAR, NEW_RELEASE, etc.) is a singleton for that entry. They can have constructors, implement arbitrary methods and implement interfaces. In fact, you can even put abstract methods on the "base" enum to require the concrete enum singleton entries to implement a method (as we'll see in my example below).

Here is the end product of my refactoring starting from Fowler's end product:

public enum Price {
  REGULAR {
    @Override
    public double getCharge(int daysRented) {
      double result = 2;
      if (daysRented > 2)
        result += (daysRented - 2) * 1.5;
      return result;
    }
  }, 
  CHILDRENS {
    @Override
    public double getCharge(int daysRented) {
      double result = 1.5;
      if (daysRented > 3)
        result += (daysRented - 3) * 1.5;
      return result;
    }
  }, 
  NEW_RELEASE{
    @Override
    public double getCharge(int daysRented) {
      return daysRented * 3;
    }

    @Override
    public int getFrequentRenterPoints(int daysRented) {
      if (daysRented > 1) return 2;
      else        return 1;
    }
  };

  public abstract double getCharge(int daysRented);

  /**
   * Default implementation of getFrequentRenterPoints for all 
   * types in the enum. May be overridden by specific enum types
   * if they give fewer or higher numbers of bonus points.
   * 
   * @param daysRented number of days the movie was rented
   * @return number of bonus points
   */
  public int getFrequentRenterPoints(int daysRented) {
    return 1;
  }
}

If you haven't used the advanced features of enums, this may look surprising.

First, notice that you can declare methods in the "main body" of the enum, and that they can even be abstract. In this case, I have created the getCharge and getFrequentRenterPoints methods. This means that all specific (singleton) entries of the enum have these methods. In the case of the abstract method, the compiler will require you to implementat that method in each entry body.

Which brings me to the second point - enum entries can have bodies that are specific to that entry and not shared with the other entries. Methods outside the entries are common to all entries and methods inside an entry are specific to the entry.

The zone of "inside an enum entry" is demarcated by a matching pair of curly braces after the declaration of the entry's name (e.g., REGULAR). This is just like the notation for a class body.

You create specific data fields and methods inside the enum entry body. In this case these entries don't have any state to retain (their name is the representation of the state needed), so I only have methods, not fields.

If one did need fields, how you would populate them with user/client-provided data? You would define a constructor, which would go in the section outside the entries (but inside the enum class body of course). Effective Java has a nice example of when you might need to have enum constructors.


/* ---[ Using the Price enum ]--- */

If you scroll back up and review the UML diagram, you'll see that the Movie class has both getCharge and getFrequentRenterPoints methods. In my refactoring the Movie class does the same thing. The only difference is that Price is an enum, not a regular Java class.

public class Movie {
  private String _title;
  private Price priceCode;

  public Movie(String title, Price priceCode) {
    _title = title;
    this.priceCode = priceCode;
  }

  public Price getPriceCode() {
    return priceCode;
  }

  public String getTitle() {
    return _title;
  }

  double getCharge(int daysRented) {
    return priceCode.getCharge(daysRented);
  }

  public int getFrequentRenterPoints(int daysRented) {
    return priceCode.getFrequentRenterPoints(daysRented);
  } 
}

Users of the Movie class would then need references the standalone Price enum when creating a Movie:

Movie m1 = new Movie("Grease", Price.REGULAR);
Movie m2 = new Movie("Cars", Price.CHILDRENS);


/* ---[ Analysis ]--- */

So is this better? When are enums more appropriate?

First of all, Java 5 enums are always more appropriate than using the "int enum" pattern. Their primary purpose is to replace that pattern. They bring type-safety to those constants, they bring a toString() method that prints out their name, they implement Comparable and Serializable and allow for the addition of arbitrary behavior.

Java 5 enums are appropriate when you need singleton behavior from each entry - only one copy of the REGULAR, CHILDREN and NEW_RELEASE enum objects will ever be created. In fact, they are such perfect singleton implementations, both in terms of proper initialization and thread-safety, that Josh Bloch recommends them as the best way to implement the Singleton pattern in Java now. If you want it to be a true singleton, then you only create one "entry", which you might call INSTANCE, like so:

public enum MySingleton {
  INSTANCE {
    // put instance fields and methods here
  };
}

Creating a singleton with guaranteed creation thread-safety and no known ways to create two (such as by Serialization attacks) has never been so easy.

Of course, as singletons, enums inherit some well-known pitfalls of the Singleton pattern, including the fact that they are difficult to test, as you can't dependency inject a mock or stub version of them. With a good dependency injection framework, like Spring, singletons are frequently not needed any more.

In any case, an enum with extra behavior is appropriate when you need behavior without state or where the state of each enum entry would the be same for any objects using them. In the case of my refactored version, the only state the Price enum needs is to know its type (which is what the enum is for) and do some calculations based on that state. There is only a need for one of them in the entire app, no matter how many Movie objects I need to create, so using a set of singleton immutable enums works. And the behavior of those enums does not leverage any external dependencies or resources - they just do some simple arithmetic and return an answer, so I don't need to mock or stub them for testing.

So finally, what about the open/closed principle? Well, with enums in my refactored version, one could argue that they do violate this principle. It isn't open for extension, since enums are final and cannot be subclassed. And it isn't closed for modification, as new entries of the enum cannot be created without editing the Price enum java source code file directly.

True critique. Enums do not support the open/closed principle and thus should only be used in situations where you have (and preferably own) the source code and can modify it yourself or when you actively want to prevent anyone from creating any other types. It is closed/closed intentionally.

But is my refactoring really worse than Fowler's on this criterion? Actually no, because even though his Price class follows the open/closed principle, the Movie class does not - it still expects its clients to indirectly reference the Price class via its int constants. To add a new int constant one would have to edit the Movie java file source code. So Fowler's overall refactored design doesn't follow the open/closed principle either.

If we truly wanted to follow that principle, we would have to refactor to some third design. I'll leave that as an exercise for any reader that might be interested in trying that out.


/* ---[ Antipattern: double bad ]--- */

A final side note: Fowler's example code has a smell in it that he didn't correct: using double to handle monetary values. A refactoring should also be done to use long, BigDecimal or a self-constructed Money class instead.

And that's the joy of refactoring - with new language features evolving and the list of code smells you are aware of growing, you will frequently be able to go back to old code and find a way to improve it.

Sunday, March 11, 2012

MyBatis: A User Guide Companion


/*---[ Rationale ]---*/

One of the frustrations I have about reading guides on how to learn a new software technology is that they tend to focus on showing a snippet of code that they want to talk about rather than all the code in context. I have seen others comment that this seems to be a trend in many programming books and online guides these days and was not the case in days when Kernighan and Ritchie wrote the seminal The C Programming Language.

The MyBatis 3 User Guide is a case in point. It is easy for someone new to it to quickly get confused because they don't understand the context of the snippets. Simple complete working examples that you can try on the command line or in Eclipse would solve this problem. The authors also sometimes refer to getting the example source code, but finding that is not trivial. I ended up pulling their source code from their SVN repo and that has a couple of different test apps mixed together (the blog and jpetstore are two).

They do provide the JPetStore full code "sample", but it is large and intertwined with Stripes and Spring, so not a place for a newbie to start.

So, since I really like MyBatis and want to promote people learning this, here is my contribution to present the simplest possible MyBatis setup that will allow you to read the MyBatis3 documentation and have working code to allow you to try out its examples.

There are a number of good tutorials for MyBatis (exhibit A and exhibit B) on the web, but they either typically jump right into a full fledged example that can be overwhelming or are a full stack example including servlets and app servers, etc. when you just want to understand the basics and try it out isolation. That's the gap I'm trying to fill in this tutorial companion.


/*---[ "User Guide Companion" Overview ]---*/

This is a companion to, not a substitute for, the MyBatis 3 User Guide, so make sure you download that and the mybatis code bundle from the MyBatis website.

This tutorial companion comes in two parts. Since I'm a big believer in "provide an example of the simplest thing that works" - that's what part 1 is: a bare bones, but fully working, setup of a MyBatis-based system. Part two is the set up you'll need for the Blog example that the MyBatis 3 User Guide largely uses. By having this foundation, you can tweak and test a working system as you read through the User Guide.

Here I assume you know how to put jar files on your CLASSPATH either from the command line or in an IDE like Eclipse.

Download the latest MyBatis bundle from the MyBatis website here. Put mybatis-3.x.x.jar in your CLASSPATH (Java Build Path > Libraries in Eclipse). You will need to also download the JDBC jar for the database you are using.

I provide .sql files for creating tables and initial data sets for both PostgreSQL and MySQL. You can get all the code I reference here from my GitHub repo.



Tutorial Companion Part One: MyBatis101 - The Simplest Thing That Could Work

I call this first application "MyBatis101" and I've created a directory with that name. In that directory, I have created 7 files, including an Ant build.xml file, so in the end it looks like this:

MyBatis101$ tree
.
|-- build.xml
|-- MyBatis101.sql
|-- src
    |--MyBatis101-config.xml
    |-- mybatis101
        |-- Main.java
        |-- Mapper.java
        |-- MyBatis101-mapper.xml 
        |-- User.java


/*---[ First: Set up a Database ]---*/

First let's set up a very simple database with one table, having two columns and two rows of data.

As I said above, I will show this in both PostgreSQL and MySQL. I don't describe how to install and set up those databases. If you need to start there, here are links to good documentation:

In my setup I am using PostgreSQL 9.1 and MySQL 5.1.

I also show these using a Linux command line, but it should work the same on Mac and Windows. I list all the files one by one below, but to avoid copy and paste, be sure to pull them from my from my GitHub repo:

git clone git@github.com:midpeter444/MyBatis-UserGuide-Companion-Code.git


/*---[ PostgreSQL ]---*/

Create the MyBatis101 database and check that it is there:

$ createdb MyBatis101
$ psql --list
                                   List of databases
    Name    |    Owner    | Encoding | Collation  |   Ctype     
------------+-------------+----------+------------+-------------
 depot      | midpeter444 | UTF8     | en_US.utf8 | en_US.utf8 
 MyBatis101 | midpeter444 | UTF8     | en_US.utf8 | en_US.utf8 
 postgres   | postgres    | UTF8     | en_US.utf8 | en_US.utf8 
 template0  | postgres    | UTF8     | en_US.utf8 | en_US.utf8 

 template1  | postgres    | UTF8     | en_US.utf8 | en_US.utf8 

(5 rows)


Create the file MyBatis101.sql:

drop table if exists users;

create table users (
  id integer,
  name varchar(20)
);

insert into users (id, name) values(1, 'User1');
insert into users (id, name) values(2, 'User2');


Create the tables and load test data into the MyBatis101 database and check that it is there:

$ psql MyBatis101 < MyBatis101.sql
$ psql MyBatis101
psql (9.1.3)
Type "help" for help.

MyBatis101=> \d
          List of relations
 Schema | Name  | Type  |    Owner    
--------+-------+-------+-------------
 public | users | table | midpeter444
(1 row)

MyBatis101=> select * from users;
 id | name  
----+-------
  1 | User1
  2 | User2
(2 rows)


/*---[ MySQL ]---*/

Create the MyBatis101 database and check that it is there:

$ mysql -p
mysql> create database MyBatis101;
Query OK, 1 row affected (0.03 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| MyBatis101         |
| mysql              |
+--------------------+
3 rows in set (0.03 sec)


Create the file MyBatis101.sql: (Same file as above)


Create the tables and load test data into the MyBatis101 database and check that it is there:

$ mysql -p MyBatis101 < MyBatis101.sql
$ mysql -p MyBatis101

mysql> show tables;
+----------------------+
| Tables_in_MyBatis101 |
+----------------------+
| users                |
+----------------------+
1 row in set (0.00 sec)

mysql> desc users;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | YES  |     | NULL    |       |
| name  | varchar(20) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.01 sec)

Now that the databases is set up with a table and a little sample table, now we can try out MyBatis.


/*---[ The MyBatis Set Up Files ]---*/

Create the MyBatis101-config.xml in the src directory:

Note: Use the correct driver and url according to which database you are using.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
    PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>

  <environments default="development">
    <environment id="development">
      <transactionManager type="JDBC"/>
      <dataSource type="UNPOOLED">
        <property name="driver" value="org.postgresql.Driver" />
        <property name="url" value="jdbc:postgresql:MyBatis101" />
        <!--  <property name="driver" value="com.mysql.jdbc.Driver" />   -->
        <!--  <property name="url" value="jdbc:mysql://localhost:3306/MyBatis101" /> -->        
        <property name="username" value="" />
        <property name="password" value="" />
      </dataSource>
    </environment>
  </environments>

  <mappers>
    <mapper resource="MyBatis101-mapper.xml" />
  </mappers>

</configuration>


Create MyBatis101-mapper.xml in the src/mybatis101 directory:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="mybatis101.Mapper">

    <select id="getUser" parameterType="int" resultType="mybatis101.User">
        select * from users where id = #{id}
    </select>

</mapper>


Create src/mybatis101/Mapper.java:

package mybatis101;

public interface Mapper {
  User getUser(Integer id);
}


Create src/mybatis101/User.java:

package mybatis101;

public class User {

  private Integer id;
  private String name;

  public Integer getId() {
    return id;
  }

  public void setId(Integer id) {
    this.id = id;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }
}


Create src/mybatis101/Main.java:

package mybatis101;

import java.io.Reader;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class Main {

  private static SqlSessionFactory sessionFactory;

  public static void initSessionFactory() throws Exception {
    Reader rdr = Resources.getResourceAsReader("MyBatis101-config.xml");
    sessionFactory = new SqlSessionFactoryBuilder().build(rdr);
    rdr.close();
  }

  // uses the old iBATIS style of lookup
  public static void lookUpUserOldWay() throws Exception {
    SqlSession session = sessionFactory.openSession();
    try {
      User user = (User) session.selectOne(
          "mybatis101.Mapper.getUser", Integer.valueOf(1));
      System.out.println(user.getName());  // should print out "User1"

    } finally {
      session.close();
    }
  }

  // uses the new MyBatis style of lookup
  public static void lookUpUser() throws Exception {
    SqlSession session = sessionFactory.openSession();

    try {
      Mapper mapper = session.getMapper(Mapper.class);
      User user = mapper.getUser(2);
      System.out.println(user.getName());  // should print out "User2"

    } finally {
      session.close();        
    }
  }

  public static void main(String[] args) throws Exception {
    initSessionFactory();
    lookUpUserOldWay();
    lookUpUser();
  }
}


Create build.xml ant script (not required if you are doing this in Eclipse) and run it:

<project name="MyBatis101" default="run" basedir=".">
  <!-- Template based on: http://sourceforge.net/apps/mediawiki/import-ant/index.php?title=Snippets -->
  <description>Build script for simple MyBatis101 app</description>

  <!-- load environment variables as properties -->
  <property environment="env"/>

  <!-- default folder location properties -->
  <property name="src.dir" value="src"/>
  <property name="build.dir" value="bin"/>
  <!-- TODO: EDIT THESE -->
  <property name="lib.dir" value="/home/midpeter444/java/lib"/> 
  <property name="jdbc.jar" value="postgresql.jar"/>
  <!-- <property name="jdbc.jar" value="mysql-connector-java.jar"/> -->

  <!-- project classpath -->

  <path id="project.classpath">
    <!-- compiled classes -->
    <pathelement location="${build.dir}" />
    <!-- libraries -->
    <fileset dir="${lib.dir}">
      <include name="${jdbc.jar}" />  
      <include name="mybatis.jar" />    <!-- TODO: EDIT THIS -->
    </fileset>
  </path>

  <!-- basic -->

  <target name="init">
    <mkdir dir="${build.dir}"/>
  </target>

  <!-- compile -->

  <target name="prepare-resources" depends="init">
    <copy todir="${build.dir}" overwrite="true">
      <fileset dir="${src.dir}" includes="**/*.xml" />
    </copy>
  </target>

  <target name="compile" depends="init,prepare-resources"
          description="Compile java classes">
    <javac
        srcdir="${src.dir}"
        destdir="${build.dir}"
        includeantruntime="false"> <!-- to overcome misfeature in An t1.8 -->
      <classpath refid="project.classpath" />
    </javac>
  </target>


  <!-- run on console -->

  <property name="run.main-class" value="mybatis101.Main"/>
  <property name="run.args" value=""/>

  <target name="run" depends="compile"
          description="Run MyBatis101 program">
    <java classname="${run.main-class}" fork="true">
      <arg line="${run.args}" />
      <classpath>
        <path refid="project.classpath" />
      </classpath>
    </java>
  </target>
</project>
$ ant run
Buildfile: /home/midpeter444/databases/postgresql/mybatis-learn/MyBatis101/build.xml

init:

prepare-resources:
     [copy] Copying 2 files to /home/midpeter444/databases/postgresql/mybatis-learn/MyBatis101/bin

compile:

run:
     [java] User1
     [java] User2

BUILD SUCCESSFUL
Total time: 2 seconds

I'm not going to explain much about this code. Read it along with the User Guide or the MyBatis Getting Started tutorial and it should start to make sense pretty quickly.




Tutorial Companion Part Two: Blog App in the MyBatis3 User Guide

The MyBatis101 code was intended just to get your feet wet with MyBatis. In this section, we briefly peruse the companion code as part of the "blog" application that is mostly referenced in the MyBatis3 User Guide. Note: the references to the blog database structure and codebase are not consistent in the MyBatis3 User Guide, so I've done the best I can at finding something close to most examples.

This code is intended as starter code - it is a fully working example, with a Main.java that exercises a couple of MyBatis query mappings and one insert mapping. I have also provided one JUnit 4 test that only tests one of the query mappings.

This code along with the User Guide could be used as a sort of poor man's koan - a series of exercises to be filled out with more functionality.

The routine will be the same as above, we just have more of it. Again, pull all the files from my GitHub account.

I have created a directory called "blog" and the code base structure I provide looks like this:

blog$ tree
.
|-- blogdb-ddl-mysql.sql
|-- blogdb-ddl-postgres.sql
|-- blogdb-dml.sql
|-- build.xml
|-- src
    |-- main
        |-- java
            |-- org
                |-- mybatis
                    |-- example
                        |-- Author.java
                        |-- AuthorMapper.xml
                        |-- Blog.java
                        |-- BlogMapper.java
                        |-- BlogMapper.xml
                        |-- config.properties
                        |-- Configuration.xml
                        |-- Main.java
    |-- test
        |-- java
            |-- org
                |-- mybatis
                    |-- example
                        |-- BlogMapperTests.java

We start by creating the database and tables for the Blog and Author classes they discuss in the MyBatis3 User Guide.


/*---[ PostgreSQL ]---*/

Create the database:

$ createdb blogdb


Create the DDL for the blog and author tables and save it in a file called "blogdb-ddl-postgres.sql":

DROP TABLE    IF EXISTS blog;
DROP TABLE    IF EXISTS author; 
DROP SEQUENCE IF EXISTS blogdb_blog_seq;
DROP SEQUENCE IF EXISTS blogdb_author_seq;
CREATE SEQUENCE blogdb_blog_seq;
CREATE SEQUENCE blogdb_author_seq;

CREATE TABLE author (
  id               integer PRIMARY KEY DEFAULT nextval('blogdb_author_seq') NOT NULL,
  username         varchar(255) NOT NULL CHECK (username <> ''),
  hashed_password  varchar(255) NOT NULL CHECK (hashed_password <> ''),
  email            varchar(100) NOT NULL CHECK (email <> ''),
  bio              text
);

CREATE TABLE blog (
  id          integer PRIMARY KEY DEFAULT nextval('blogdb_blog_seq') NOT NULL,
  title       varchar(255) NOT NULL CHECK (title <> ''),
  author_id   integer NOT NULL references author(id)
);


Create some fake data to load into the tables and saved it in a file called "blogdb-dml.sql":

INSERT into author (username, hashed_password, email, bio)
VALUES('aaron1', 'aaron1', 'aaron@pobox.com', 'Aaron is "The Dude".');

INSERT into author (username, hashed_password, email)
VALUES('barb2', 'barb2', 'barb@pobox.com');

INSERT into author (username, hashed_password, email, bio)
VALUES('carol3', 'carol3', 'carol@pobox.com', 'Carol is an avid atom-smasher and street luger.');

INSERT into blog (title, author_id)
VALUES('Why I am "The Dude"', (select id from author where username='aaron1'));

INSERT into blog (title, author_id)
VALUES('A Day in the Life of "The Dude"', (select id from author where username='aaron1'));

INSERT into blog (title, author_id)
VALUES('Sanity is my strong suit', (select id from author where username='barb2'));

INSERT into blog (title, author_id)
VALUES('I are smart?', (select id from author where username='carol3'));

INSERT into blog (title, author_id)
VALUES('The Large-Hadron Collider will not create a black hole that ends the universe', (select id from author where username='carol3'));


Run the DDL and DML scripts against the database:

$ psql blogdb < blogdb-ddl-postgres.sql
$ psql blogdb < blogdb-dml.sql


/*---[ MySQL ]---*/

Create the database:

$ mysql -p
mysql> create database blogdb;
Query OK, 1 row affected (0.03 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| MyBatis101         |
| blogdb             |
| mysql              |
+--------------------+
4 rows in set (0.03 sec)


Create the DDL for the blog and author tables and save it in a file called "blogdb-ddl-mysql.sql":

DROP TABLE    IF EXISTS blog;
DROP TABLE    IF EXISTS author; 

CREATE TABLE author (
  id               integer NOT NULL AUTO_INCREMENT PRIMARY KEY,
  username         varchar(255) NOT NULL CHECK (username <> ''),
  hashed_password  varchar(255) NOT NULL CHECK (hashed_password <> ''),
  email            varchar(100) NOT NULL CHECK (email <> ''),
  bio              text
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;

CREATE TABLE blog (
  id          integer NOT NULL AUTO_INCREMENT PRIMARY KEY,
  title       varchar(255) NOT NULL CHECK (title <> ''),
  author_id   integer NOT NULL,
  FOREIGN KEY (author_id) REFERENCES author(id)
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;


Create some fake data to load into the tables and save it in a file called "blogdb-dml.sql": (Same file as in the PostgreSQL section.)


Run the DDL and DML scripts against the database:

$ mysql -p blogdb < blogdb-ddl-mysql.sql
$ mysql -p blogdb < blogdb-dml.sql


/*---[ Work Through The User Guide ]---*/

Next, start by looking at Main.java in src/main/java/org/mybatis/example. Follow each of its steps to see how the MyBatis system works. This example includes a complex mapping - what MyBatis calls a resultMap. On p. 29 of the User Guide they go into some detail about it, as it is one of the most important features of MyBatis.

From this code base you should be able to work through the entire User Guide, trying out new features as you go. Make sure to write tests for everything you do. I've intentionally made the JUnit test very bare bones so that is the place you can learn by doing.

After doing this, I recommend trying out one of the MyBatis tutorials on the web, such as either of those that I already mentioned above:

Good luck and feedback is welcome.


[Update 28-May-2012]: I've recently published a set of koans to learn MyBatis, so consider trying those out as well. My May 2012 blog entry describes these and how to get started.

Friday, March 2, 2012

Technical Podcasts I Listen To

The internet is amazing. If information yearns to be free (and plentiful), then information is in its heyday. I've seen some predictions that formal institutions of learning, such as universities, will need to adapt or die. A motivated person could literally get the equivalent of a computer science degree from buying some key books and using information freely available on the web, including webinars, videocasts, podcasts, tutorial sites and stackoverflow. I spend a lot of time at InfoQ, for instance, watching the excellent lineup of software engineering video lectures they make available. There is Google Code University to sink your time into. And MIT and Stanford are leading the way with free online courses of high quality (so I hear - I haven't taken one yet ...).

In any case, besides this paean to the internet, what I mean to list here are the current computer science/software engineering podcasts I currently listen to or have heard about and intend to get to. Like the video casts on InfoQ and (to a lesser degree) Google Tech Talks on youtube, there are more than I can keep up with.

Here's my list with my subjective rating from 1 to 10 (best). My rating is based on how useful it is to me as a software engineer first and second as a software/electronic device user. And I'm biased towards Linux, the JVM, JavaScript and Ruby. My interest in all things Windows and .NET is pretty low.

ShowSubjective Rating: 1-10 (best)
Software Engineering Radio10
Ruby Rogues9
Think Relevance Podcast9
Mostly lazy (Clojure)8
Teach Me to Code8
JavaScript Jabber8
Basement Coders (Java heavy)7
Adam Glover Podcasts - IBM developerWorks7
Security NowBumped to 7.
Been really getting into these lately and some excellent stuff on how the internet works
FLOSS Weekly6
Pragmatic Programmer's Podcast6
A Minute with Brendan (Eich)6
Herding Code (Windows centric)5
(their episode on git was excellent)
Java Spotlight4
The JavaScript Show4
Java Posse4
All About Android3
Linux Outlaws2
Computer Science Podcast??? - haven't listened yet
Curly Brace Cast??? - haven't listened yet
Hacker Media??? - haven't listened yet
Deductive Developers??? - haven't listened yet
Linux Trivia Podcast??? - haven't listened yet
Paul dot com (security related)??? - haven't listened yet
YayQuery??? - haven't listened yet
ArsTechnica PodcastBrand new: ??? - haven't listened yet

I'll update this as I get time to listen to more or find others that I like. I welcome any additions you know about.

Sunday, February 19, 2012

Factories and Builders, Idioms and Patterns

I have to admit to being confused at times between the various meanings of the word "Factory" in object oriented design. For example, recently I was reviewing an Item in Josh Bloch's Effective Java where he said that static factory methods are not an implementation of the Gang of Four Factory pattern. In the next Item he then says that the Builder "pattern" (idiom?) he displays can be used as part of the Gang of Four Abstract Factory pattern. But then the Gang of Four have a Builder pattern as well - how does that relate?

So that's five somewhat related concepts:

  • Static Factory Methods
  • The Builder idiom recommended by Josh Bloch in Effective Java
  • The GoF Builder Pattern
  • The GoF Factory Pattern
  • The GoF Abstract Factory Pattern

In this blog entry I review these five ideas and how they relate.


/* ---[ Static Factory Methods ]--- */

The Static Factory is a simple idiom for encapsulating creational details into a single place accessible to other classes. For example:

// imports from Google Guava shown
import com.google.common.collect.Maps;  
import com.google.common.collect.ImmutableList; 

// the old-fashioned, hardcoded way
List<Integer> lint1 = new ArrayList<Integer>();
lint1.add(1);
lint1.add(2);
lint1.add(3);

// the first two are from Guava's static 
// factory methods
List<Integer> lint2 = Lists.newArrayList(1, 2, 3);
List<Integer> lint3 = ImmutableList.of(1, 2, 3);
List<Integer> lint4 = Collections.unmodifiableList(lint1);

On lines 12 - 14, we use static factory methods to create Lists. There are at least two benefits to this:

  1. We don't have to repeat the generic parameter on the right hand side
  2. We allow the static factory to determine the best type of concrete List class to create

In fact, in this case the three concrete classes created by the static factory methods all differ:

final PrintStream stdout = System.out;
stdout.println(lint1.getClass()); // java.util.ArrayList
stdout.println(lint2.getClass()); // java.util.ArrayList
stdout.println(lint3.getClass()); // com.google.common.collect.RegularImmutableList
stdout.println(lint4.getClass()); // java.util.Collections$UnmodifiableRandomAccessList

I won't spend much time on why using static factories is Good Design - Josh Bloch's very first entry in Effective Java -- which every Java developer has read, right? -- spends 6 pages on it. But I will mention one of my favorite benefits though (quoting): unlike constructors, they are not required to create a new object each time they are invoked. Caching is good.


/* ---[ The Builder Idiom ]--- */

In the Getting Started documentation for DBUnit, there is a curious bit of openness about not knowing how to complete their API design. In talking about doing row ordering with their SortedTable object, they have this code snippet:

SortedTable sortedTable1 = new SortedTable(table1, 
                                           new String[]{"COLUMN1"});
// must be invoked immediately 
// after the constructor
sortedTable1.setUseComparable(true); 

It is followed by this statement in italics:

The reason why the parameter is currently not in the constructor is that the number of constructors needed for SortedTable would increase from 4 to 8 which is a lot. Discussion should go on about this feature on how to implement it the best way in the future.

This is the problem of non-atomic object creation where you have to build up the state of the object piece by piece. When there are a lot of pieces, creating all possible constructors to cover the permutations makes it a nightmare for both the library writer and API user.

So one solution is to have an empty constructor and the set neccessary state via a series of setters, which is the JavaBeans spec. But that is very problematic. The object is in an incomplete state until all relevant adders and setters have been called and if the object escapes before the user constructs everything correctly, then you have a source of bugs. With the JavaBeans model, there is no clearly defined point at which to check whether the object's invariants have been violated before it is sent off to the world.

So you get comments in the API like "you must invoke this setter immediately after the constructor in order for the object to work correctly", as with the DBUnit case.

The Builder idiom is a nice solution to this problem, including cases where you want to create immutable objects. It allows objects to be constructed with any number of variable attributes and settings. Some may be required, others are optional. And you can construct the object in a multi-step, self-documenting fashion, which is one of the nice features of the JavaBeans model, but without the possibility of leaving an object in an inconsistent state.

A typical way to use the builder idiom in Java these days is to combine it with a fluent interface. One starts by getting a reference to a builder, which is often an inner class of the class that the builder creates. Then one calls a series of functions on the builder to tell it how to set up the object and finally invoke a build() method to return the desired object.

For the DBUnit example, the builder idiom could be used this way:

SortedTable sortedTable1 = SortedTable.
  newBuilder(table1, new String[]{"COLUMN1"}).
  setUseComparable(true).
  build();

// alternative way without having required fields in 
// the builder constructor
SortedTable sortedTable1 = SortedTable.newBuilder().
  setTable(table1).
  setColumns(new String[]{"COLUMN1"}).
  setUseComparable(true).
  build();

I prefer to put required fields in the builder constructor, since it emphasizes that they are required, but it is not absolutely necessary, especially if there a lot of required fields. One of the beautiful things about the builder idiom is that the build method is the perfect place for the Builder to enforce any invariants that must be met in order to create a valid object.

Builder could be used rather using a variety of different static factories. For example, here is a fictitious example of how Guava could have used a Builder pattern to create a List with various attributes:

// fake API - not runnable code !
List<Integer> lint1 =
  Lists.newBuilder().unmodifiable().
    add(1).add(2).add(3).
    build();

List<Integer> lint2 =
  Lists.newBuilder().immutable().
    add(1).add(2).add(3).
    build();

List<Integer> lint3 =
  Lists.newBuilder().synchronizedColl().
    maxSize(5).add(1).add(2).add(3).
    build();

But for collections, it is more typical to use the static factory pattern, as we saw in the first section.

Where the builder idiom particularly shines is when you are constructing objects with a variable and potentially complex set of attributes. A good example: Guava uses the builder idiom to create a Cache object:

Cache<Key, Graph> graphs = CacheBuilder.newBuilder()
     .concurrencyLevel(4)
     .weakKeys()
     .maximumSize(10000)
     .expireAfterWrite(10, TimeUnit.MINUTES)
     .build(
         new CacheLoader<Key, Graph>() {
           public Graph load(Key key) throws AnyException {
             return createExpensiveGraph(key);
           }
         });

Another aspect of the builder idiom that is particularly satisfying is that it is a great way to create immutable objects that have more than a couple optional attributes. For example, suppose you are modeling an inventory item that only directly requires custodian and quantity, but can take many different optional attributes, such as status, location, container-id, RFID-tag, etc. If you want to create immutable inventory objects, the builder pattern is a pleasant way to manage this:

// custodian (thornydev) and location are required, 
// so go in the builder constructor
InventoryItem item = InventoryItem.
  newBuilder("thornydev", "location 123").
  status("Available").
  quantity(35).quantityUnit(InventoryItem.KILOGRAMS).
  build();

In the build method, the Builder can check that key business rules have been met. For example, quantity and quantityUnit must either both be set or neither set, otherwise build will throw an IllegalStateException.


/* ---[ The Builder Pattern ]--- */

OK, now that we've reviewed the idioms, let's analyze the formal GoF patterns and see how they compare.

The Builder Pattern is actually quite similar to the builder idiom. In the GoF version, the Builder is an interface. A concrete version is created to create a product. In the idiom, the Builder doesn't have an interface, since it is tightly coupled to creating a particular product (and in Java is often implemented as an inner class of that object).

The advantage of using an abstraction, in this case an interface, is, as usual, to be able to transparently swap out a different Builder implementation, or to allow multiple different types of entities to use a common interface. An example of the latter is found in the JDK's java.lang.Appendable.

Appendable is an interface that has three flavors of the same method, append. BufferedWriter, PrintStream and StringBuilder, to name a few, implement its interface. StringBuilder is a nice, though simple, example of the Builder pattern - you build up the string bit by bit, can do some morphs, reversals, substrings, or other sorts of changes and then produce an immutable String, threadsafe and ready for production use. In this case toString is the "build" method:

String s = "devil";
StringBuilder sb = new StringBuilder();
sb.append(s).append("ish").  // now is "devilish"
  replace(5, 7, " e").       // now is "devil eh" 
  reverse().toString();      // => "he lived"

The GoF book illustrates a more sophisticated use of the Builder pattern. They create an RTFParser than will convert RTF text to other formats. I've updated their example a bit and redrawn it:

This kind of looks like the Abstract Factory pattern (see below). So why is this a Builder? Because it will be used to construct one document (say in HTML format) by calling its builder methods multiple times in some arbitrary order in a multi-step process to create that document. For example:

HTMLConverter c = new HTMLConverter();
Document d = c.
  convertBold(string1).
  convertParagraph(line1).
  convertHeader1(string2).
  convertParagraph(line2).
  // ... etc.
  .build();

Note: what I'm calling the builder idiom in this article has also been referred to as the "revised builder pattern". As noted here, the two variants use the same approach with different emphasis. The builder idiom (revised builder pattern) usually tightly couples a builder to a specific concrete class, with the intent of simplifying the construction of an object with a complex set of attributes. The GoF Builder pattern is more about providing an abstraction to build various types of entities, akin to Abstract Factory in that sense.


/* ---[ The Factory Pattern ]--- */

The Factory Pattern uses classic Object Oriented (OO) reuse through inheritance, along with all the shortcomings and foibles of inheritance-based design

The essence of the Factory Pattern is to delegate to a subclass the creation of an entity that can vary based on application needs. It typically uses either an abstract or concrete class, not a pure interface, to provide a default implementation and handle logic that will be common to all subclasses.

A trivial example from the JDK is Object#toString. Object, a concrete class, provides a basic toString method that subclasses can accept as-is or tailor to their needs.

A slightly less trivial example is java.lang.Number#intValue (and floatValue etc.). These methods are abstract and have to be implemented in a way appropriate to the subclasses. Number provides an implementation of a few common methods and the rest are delegated to subclasses, which includes Integer, Double, BigInteger and AtomicLong.

The above examples are not really "factories" as I normally think of them, though one could argue they are factories of Strings and primitives (but it's a weak argument, which is why I think of Factory as just OO inheritance-based design).

Here is the GoF UML class diagram of the Factory pattern.

In many cases, the Factory pattern starts to blur into the Template pattern for me. Both use OO inheritance-based reuse of methods defined in the parent class. As a side note, the Template Pattern may be a more valid and robust version of inheritance than is often practiced.

The Template Pattern can be implemented via the Factory Pattern, as the GoF book states: Factory methods are usually called within Template Methods (p. 116).

The excellent Head First Design Patterns book does exactly this in their implementation of a Factory method (see my comments in the code below):

package headfirst.factory.pizzaaf;

public abstract class PizzaStore {

  // the abstract method to be implemented in subclasses
  // such as NYPizzaStore or ChicagoPizzaStore
  protected abstract Pizza createPizza(String item);

  // this is the (unacknowledged) Template pattern here
  // delegating the factory method createPizza to a
  // concrete subclass
  public Pizza orderPizza(String type) {
    Pizza pizza = createPizza(type);
    System.out.println("Making a " + pizza.getName());
    pizza.prepare();
    pizza.bake();
    pizza.cut();
    pizza.box();
    return pizza;
  }
}


/* ---[ The Abstract Factory Pattern ]--- */

The Abstract Factory Pattern is intended for situations where you need to create families of related "products". For example, a widget library needs to create multiple related widgets - buttons, labels, frames, pick lists, text fields, scrollbars, etc. If you want to offer different "skins" or look-and-feels (is that an outdated term now?), you could use the Abstract Factory Pattern.

The canonical example provided by the Gang of Four is a WidgetFactory interface that can be implemented by any number of concrete Factories to produce all the various widgets with a defined Look-and-Feel.

From this quick look we can immediately see two differences between Factory and Abstract Factory:

  1. Abstract Factory uses a pure interface, while Factory has concrete methods and may or may not have any abstract methods.
  2. Abstract Factory is useful when you need to create multiple different categories of things (like widgets) that are related (say by look-and-feel). Factory produces one thing (a PizzaStore).

So you could think of an Abstract Factory as creating a factory of little factories. And here's where we can start to tie together some the threads of this investigation. What patterns or idioms can the "little factories" of the Abstract Factory use?

Well, they can use the static factory idiom, if the thing they are creating is easily bound to one method call with no or few parameters. Or it could use a Builder (either form) to construct products that need to be constructed in a multi-step fashion or with lots of variable attributes. Or we could use the Factory method to be able to swap different concrete implementations as needed.

Abstract Factory Example from the JDK

In JDBC, one obtains a connection to a datastore by calling DriverManager.getConnection. This is a static factory method that returns an implementation of java.sql.Connection. Connection is a pure interface that gets implemented by JDBC library implementers, so there is one for each flavor of database. So Connection, in this case, is an Abstract Factory interface and the specific implementations by database vendors are the concrete classes. For example, with PostgreSQL's JDBC driver, the concrete Connection class is org.postgresql.jdbc4.Jdbc4Connection (if you are using the JDBC4 version).

So what "products" are created by the "little factories" in the Connection class? Many: Statement, PreparedStatement, CallableStatment, SavePoint, Blob, Clob and a few others, each one, of course, differing from those in other JDBC implementations.


/* ---[ Summary ]--- */

So, to finish, a few summarizing points:

  • The static factory idiom is very common in Java. Using it precludes the possibility of delegating responsibility to a subclass for creating specific types of objects. Therefore, use Static Factory when you are sure that you only need this one implementation of the factory.
  • The GoF Builder Pattern and "revised builder" (aka builder idiom) are basically the same, except in whether they are tightly coupled to the object they are creating. In either cases, use a builder when you want to be able to flexibly create objects that need multi-step set up, particularly when you need to handle multiple optional attributes.
  • The Factory Pattern uses inheritance to allow different implementations of a specific method intended to be overridden subclasses.
  • The Abstract Factory Pattern creates an interface whose implementations use composition to create a series of little factories to produce related items or products. Those little factories can use any of the previous patterns to do their object creation.

Friday, February 10, 2012

Thesis, Antithesis, Synthesis: Thoughts on Debuggers


/*---[ Thesis and Antithesis ]---*/

Not long ago, there was a posting on slashdot from a self-proclaimed hacker (in the good sense) asking for advice on how to become a professional software engineer to target jobs in the corporate IS world.

"Learn to use a debugger" was one piece of advice he got. Sounds good to me, I thought, although I'm not sure hackers don't know how to use debuggers - gdb, the emacs Grand Unified Debugger and the DDD debugger were all arguably written by hackers. But the "learn to use a debugger" guy went on to ridicule a co-worker of his that had never grokked how to become one with his debugger and preferred print statements. This was such a deep lapse in his skillset and mentality that in their view he couldn't keep up with the awesomeness of the debugger crew and they were glad when he finally left the company.

At my job, I overhead a bathroom conversation (a great place to eavesdrop) where a similar view was taken: "I don't think he knows what he's doing - he put println statements everywhere".

I was recently rereading parts of The Practice of Programming by Brian W. Kernighan and Rob Pike. To provide a little counterpoint, I will quote something they had to say about this debate:

As personal choice, we tend not to use debuggers beyond getting a stack trace or the value of a variable or two. One reason is that it is easy to get lost in details of complicated data structures and control flow; we find stepping through a program less productive than thinking harder and adding output statements and self-checking code at critical places. Clicking over statements takes longer than scanning the output of judiciously-placed displays. It takes less time to decide where to put print statements than to single-step to the critical section of code, even assuming we know where that is. More important, debugging statements stay with the program; debugging sessions are transient.

Perhaps Kernighan and Pike are just hackers.

And just as I was writing this blog, Uncle Bob Martin (re)tweeted this note: Using a debugger is a code smell.


/*---[ Synthesis? ]---*/

That being said, the world is full of wonderful things, and I recently ran across something new to me on the excellent InfoQ website: recording debuggers.

The InfoQ article points to two companies that offer recording debuggers for the JVM:

I did a little investigation into Chronon's debugger. It does instrumentation of the Java bytecode instruction set of your code to record everything that happens in the JVM occur over the life of an application in all threads. No source code changes are required. They use an analogy of a “flight data recorder” for a Java program.

They claim the instrumentation is lightweight enough to be able to run it in production and certainly during formal QA testing. It basically creates a dump file of application state that can be opened in the recording debugger and played backwards and forwards. For example, you can pick a variable and see all state changes it ever had during the life of the program and then jump to any of those timepoints. The stack traces will show you all current threads and you can jump between them to see what state was there.

No breakpoints are required (or even possible), since you are just moving around in a program that has already run and looking at snapshots of time. You can do things like “show me all times which method X was called and from where” and then jump to any of those to see program state and execution at that point.

The other big selling point is that you do not have to recreate the full environment in which the bug occurred – you are just watching the state changes in the JVM so you don’t need to hook up to the database, the network, the message queues or whatever else to reproduce the issue.

This could be especially powerful for debugging multi-threaded apps, which is quite difficult using traditional debuggers.

Perhaps this is the synthesis of the divide expressed in the opening section of this essay. I haven't tried one yet, but it is definitely on my todo list for the future. I'd love to hear from anyone who has tried using a recording debugger.