Finding bad code in some old system you’ve come to maintain is one thing. Being tasked with adding bad code to a new system is a whole other type of pain. Paul G was lucky enough to experience this first hand.

After an hour or two of arguing that there is a better way of doing this, Paul wrote, I was overruled by the longest serving developer and, as a result, forced to write the ‘Remove Special Chars Except Quote Ampersand Apostrophe Open Bracket Close Bracket Comma Hyphen Full Stop Comma Forward Slash’ function. Seriously.

public static string //ED: Linebreaks added as not break site layout
RemoveSpecialCharsExceptQuote
AmpersandApostropheOpenBracketCloseBracketComma HyphenFullStopCommaForwardSlash (string p_string) { //34 | 38 | 39 | 40 | 41 44| 45 | 46 | 47 StringBuilder sb = new StringBuilder(); for(int i=0; i<p_string.Length; i++) { int chrCode = Convert.ToInt32(p_string[i]); if ((chrCode < 15) || (chrCode > 64 && chrCode < 91) || (chrCode == 34) || (chrCode == 38) || (chrCode == 39) || (chrCode == 40) || (chrCode == 41) || (chrCode == 45) || (chrCode == 46) || (chrCode == 47) || (chrCode > 96 && chrCode < 123) || (chrCode > 191 && chrCode < 256)) { sb.Append(p_string.Substring(i, 1)); } else if ((chrCode == 39)|| (chrCode == 96)) { sb.Append(Convert.ToChar(39)); } } return sb.ToString(); }

Paul adds: every string in the system is routed through these functions and accessed via an ObjectFormatter class with a single method (“Format”) which has a single argument (“Object obj”). The Format method is called via remoting, looks up the object’s type in an XML configuration file, and calls the configured formatter via reflection.

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