Like most of his past jobs, Don R started at a new company with high hopes and low expectations. And also like many of his past jobs, his dreams were quickly whisked away. This time, it happened on his first support ticket.

"This'll be an easy one," his programmer-turned-manager boss said, "this used to happen quite a bit, and I've fixed it a few times. Sometimes we'll get descriptions with extra spaces, and when we send a datafeed to the processing company, it can mess up their systems. But I'm sure you'll be able to figure it out, it's in the clean-up routines."

Don's boss was right, it was pretty easy to find. The code in question looked as follows....

// pull double spaces
itemDesc = 
    stringReplace(stringReplace(
    stringReplace(stringReplace(
    stringReplace(stringReplace(
    stringReplace(stringReplace(
    stringReplace(stringReplace(
    stringReplace(stringReplace(
    stringReplace(stringReplace(
    stringReplace(stringReplace(
    stringReplace(stringReplace(
    stringReplace(stringReplace(
    stringReplace(stringReplace(
    stringReplace(stringReplace(
      itemDesc, 
    "  ", " "),"  ", " "),
    "  ", " "),"  ", " "),
    "  ", " "),"  ", " "),
    "  ", " "),"  ", " "),
    "  ", " "),"  ", " "),
    "  ", " "),"  ", " "),
    "  ", " "),"  ", " "),
    "  ", " "),"  ", " "),
    "  ", " "),"  ", " "),
    "  ", " "),"  ", " "),
    "  ", " "),"  ", " "),
    "  ", " "),"  ", " ");
    

Curious as to why he chose twenty four replacements, Don took a peek in source control at the previus version...

// pull double spaces
itemDesc = 
    stringReplace(stringReplace(
    stringReplace(stringReplace(
    stringReplace(stringReplace(
    stringReplace(stringReplace(
      itemDesc, 
    "  ", " "),"  ", " "),
    "  ", " "),"  ", " "),
    "  ", " "),"  ", " "),
    "  ", " "),"  ", " ");
    

And then he went to the version before that...

// pull double spaces
itemDesc = 
    stringReplace(
    stringReplace(
    stringReplace(
    stringReplace(
      itemDesc, 
    "  ", " "),
    "  ", " "),
    "  ", " "),
    "  ", " ");
    

A pattern was starting to emerge. He went back another version....

// pull double spaces
itemDesc = stringReplace(stringReplace(
    stringReplace(itemDesc, "  ", " "),
    "  ", " "),
    "  ", " ");
    

And another version...

// pull double spaces
itemDesc = stringReplace(
    stringReplace(itemDesc, "  ", " "),
    "  ", " ");
    

Until finally, he reached the original....

// pull double spaces
itemDesc = stringReplace(itemDesc, "  ", " ");

As tempted as Don was to simply add in another bunch of stringReplace calls just for fun, he replaced the whole thing with this.

// pull double spaces
regexReplace(itemDesc, "[ ][ ]+", " ");
[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!