Have you ever wrote code similar with?:

boolean first = true;
StringBuilder result = new StringBuilder("");
for (Item item : collection) {
  if (!first) {
    result.append(", ");
  } else {
      first = false;

I did, and I more often than not need this kind of code, and, as you do, I also dislike it.

It's ugly, hard to figure out what it does unless you stop a bit to think. This is why on my Germanium project I've picked a different strategy. I've created a class named OnceFirstIterator that contains the full "if first item state in it".

So no more if (first) then blah.

How does the end code look? I believe it's awesome, but you be the judge:

Simple list joining

OnceFirstIterator<String> comma = new OnceFirstIterator<String>("", ", ");
StringBuilder result = new StringBuilder("");
for (Item item : collection) {


Query path processing

OnceFirstIterator<String> parameterJoiner = new OnceFirstIterator<String>("?", "&amp;");
StringBuilder queryPath = new StringBuilder("");
for (Item queryItem : queryParameters) {


Basic SQL building

OnceFirstIterator<String> whereKeyword = new OnceFirstIterator<String>(" where ", " and ");
OnceFirstIterator<String> orderByKeyword = new OnceFirstIterator<String>(" order by ", ", ");
StringBuilder sql = new StringBuilder(...);
for (WhereClause whereClause : whereClauses) {
for (OrderByClause orderByClause : orderByClauses) {

So what are you waiting? Here is also a JavaScript implementation:

var onceMany = function(once, many) {
    var firstGenerated = false;
    return {
        next : function() {
            if (firstGenerated) {
                return many;
            } else {
                firstGenerated = true;
                return once;

and some small JSON encoding using it:

    result = "{ ";
    var comma = onceMany("", ", ");
    for (var key in object) {
        result += comma.next() + "'" + key.replace(/'/g, "\\'") + "' : " +  jsonStringify(object[key]);
    result += "}";


sfnet_ops #SourceForge There's instability and frequent 500 errors on the SourceForge site currently. We're working on getting this fixed asap

2 weeks ago via Twitter Web Client • 6 retweets

Fibo The Long Death of Project Hosting Sites ubm.io/1wamlvL via @dr_dobbs #GitHub #BitBucket #CodePlex #SourceForge

2 weeks ago via Twitter Web Client • 1 retweet

edw519 The #1 way for a programmer to be a "team player" is to write code that other programmers can read.

3 weeks ago via Twitter for Android • 430 retweets


The one to rule them all. The browsers that is.


SharpKnight is an Android chess game.


MagicGroup is an eclipse plugin.

Go to top