In many languages, Strings are not just the "arrays of characters" that our Introduction to Programming professor told us about. More often than not, they're immutable and sometimes magical: you can't really combine them, you can only conjure new ones. Since creating a new object is usually costly, it's not optimal to use a java.lang.String if you're going to do lots of concatenations.

Someone Oscar works with realized this. As he puts it, "You cannot accuse this developer of being oblivious to the performance advantage of using StringBuffer."

Here's the optimized code, in all it's glory:

  StringBuffer sb = new StringBuffer();

  sb.append("<input type=\"TEXT\" name= \"" + cellName);
  sb.append("\" value=\"" + cellValue + "\" ");
  sb.append("popupStyle=\"omni\" ");

  sb.append("onFocus=\"" + id + ".focusFunct(this);return;\" ");
  sb.append("onBlur=\"" + id + ".blurFunct(this);return;\" ");
  sb.append("onClick=\"" + id + ".FOCUS=this.id; "
      + "showText(event,this,'"
      + "control.getName() + "','"
      + "control.getAttr("HEADING").getObject() + "');

  sb.append("onmouseover=\""
      + id + ".mouseoverFunct(this); showValue(event,this,'");
      + control.getName() + "')\" ");
  sb.append("onkeydown=\"" + id + ".check(event,this,"
      + String.valueOf(column) + ","
      + String.valueOf(row) + ")\" ");

  sb.append("TYPE=\"" + TYPE + " \" ");
  sb.append("CELL=\"" + CELL_TYPE + "\"" ");
  sb.append("REF=\"" + cellReference +"\" ");
  sb.append("SUBTYPE=\"" + SUBTYPE + "\" ");

  sb.append( OWNER + "=\"" + control.getName() + "\" ");
  sb.append("EXTRA_TEXT=\"" + TEXT + "\" " );

  sb.append("position=\"" + getPosition() + "\"" ");

  sb.append(">\n");

  String outputHTML = sb.toString();

 

As an aside, I'm not saying that copy-and-paste happened here. I'm just saying that the DRY principle shouldn't apply if the code is optimized. I guess using functions and containers is overrated because of the performance penalty.

[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!