« Excellent Blog on Testing | Main | Niklaus Wirth books »

Comment Smell

Here's some code from the rather interesting blog "Extreme Programming - Simulating an XP project":

public string SaveToXml()
{
    XmlDocument document = new XmlDocument();

    //Create a root node called task
    XmlElement rootNode = document.CreateElement("Task");
    document.AppendChild(rootNode);

    //If the name exists, store this information
    if (this.Attributes.Contains("Name"))
    {
        //Append the name information as an attribute
        XmlAttribute nameAttribute = document.CreateAttribute("Name");
        nameAttribute.InnerText = this.Attributes["Name"].ToString();
        rootNode.Attributes.Append(nameAttribute);
    }

    //If the creation date exists, store this information
    if (this.Attributes.Contains("CreationDate"))
    {
        //Append the creation date information as an attribute
        XmlAttribute creationDateAttribute = document.CreateAttribute("CreationDate");
        creationDateAttribute.InnerText = this.Attributes["CreationDate"].ToString();
        rootNode.Attributes.Append(creationDateAttribute);
    }

    //If the due date exists, store this information
    if (this.Attributes.Contains("DueDate"))
    {
        //Append the due date information as an attribute
        XmlAttribute dueDateAttribute = document.CreateAttribute("DueDate");
        dueDateAttribute.InnerText = this.Attributes["DueDate"].ToString();
        rootNode.Attributes.Append(dueDateAttribute);
    }
}

It's a rather typical example for a "Comment smell":

  • The comments are not needed because the code communicates very clearly what is done (even to someone like me who's not an XML-Guru at all)
  • If the comments are not needed, we should get rid of them because they pollute the code plus they will get out of sync with the code in the long term (Can you say "Cut/Copy/Paste"? :-)

Plus, the comments are hints on potential refactorings like extract method.

TrackBack

TrackBack URL for this entry:
http://www.extragroup.de/mt3/mt-tb.cgi/1903

About

This page contains a single entry from the blog posted on September 26, 2006 7:33 PM.

The previous post in this blog was Excellent Blog on Testing.

The next post in this blog is Niklaus Wirth books.

Many more can be found on the main index page or by looking through the archives.

Powered by
Movable Type 3.33