Monday, December 15, 2008

Reading XmlNode.AppendChild Method documentation

While reading XmlNode.AppendChild documentation i found myself thinking of this:

If I have the following code, what would be its result?

XmlNode parentNode = new XmlNode(...);
XmlNode childNode = new XmlNode(...);

parentNode.AppendChild(childNode);
parentNode.AppendChild(childNode);

Console.WriteLine(parentNode.Childs.Count)


By reading the code i would tell that we would get two children. But by reading the documentation you can find that it will be one. Take a careful look at the following sentence in official Microsoft documentation.


If the newChild is already in the tree, it is removed from its original position and added to its target position.



How the in the world I can understand from the method name "AppendChild" that it not only appends, but removes and then appends?

So yes, naming your functions correctly is very important for clean code.

No comments: