A few months ago, Hannes shared with us some recycled code. In that installment, we learned that one of his team mates has… issues with strings. And naming things. And basic language features.

These issues continue.

For example, imagine if you well that you have a file. This file contains certain characters which you want to remove. Some need to be converted into other forms, like the letter “G” should become “6”, and “B” should become “8”. Think through how you might solve this.

private bool CleanFile(string FileName)
{
        bool result = false;
        try
        {
                string s1 = string.Empty, s2 = string.Empty;
                FileStream aFile = new FileStream(FileName, FileMode.Open);
                using (StreamReader sr = new StreamReader(aFile, System.Text.Encoding.Default))
                {
                        while (sr.Peek() > -1)
                        {
                                s1 += sr.ReadLine();
                        }
                        sr.Close();

                        s2 = s1.Replace("INS", Environment.NewLine + "INS");
                        s1 = s2.Replace("␟", "");
                        s2 = s1.Replace("-", "");
                        s1 = s2.Replace("_", "");
                        s2 = s1.Replace("G", "6");
                        s1 = s2.Replace("B", "8");
                        s2 = s1.Replace(" ", "");
                        s1 = s2.Replace("^", "");
                        s2 = s1.Replace("•", "");
                        s1 = s2.Replace(":", "");
                        s2 = s1.Replace("<", "");
                        s1 = s2.Replace(">", "");
                        s2 = s1.Replace(".", "");
                        s1 = s2.Replace("£", "");
                        s2 = s1.Replace("/", "");
                        s1 = s2.Replace("(", "");
                        s2 = s1.Replace(")", "");
                        s1 = s2.Replace("A", "");
                        s2 = s1.Replace("?", "");// still empty - can be used
                }
                using (StreamWriter sw = new StreamWriter(FileName))
                {
                        sw.Write(s2);
                        sw.Close();
                }
                result = true;
        }
        catch (Exception ex)
        {
                MessageBox.Show(ex.Message);
                result = false;
        }
        return result;
}

I’m going to ignore the pair of alternating elephant in the room for a moment, because I want to pick on oone of those little pet-peeves: the exception handling. A generic exception handler is bad. An exception handler that does nothing but show the error in a message box is also bad. Especially in a private method.

We could puzzle over the use of both s1 and s2, we could roll our eyes at doing a close inside of a using block (which automatically does that for you), but look at this line, right here:

                        s2 = s1.Replace("?", "");// still empty - can be used

What does that mean? What could it mean? Why is it there? This sounds like something that should be featured on Screenshots of Despair. Still empty - can be used. It’s like a mantra. The left side and the right side of my brain keep trading it back and forth, like s1 = s2.Replace(…) but nothing’s being replaced.

I’m still empty. I can be used.

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