I'm currently creating a program that write some XML, but due to the library I'm using I have to create many variables like:
XmlDocument xmlWriter = new XmlDocument();
XmlNode root = xmlWriter.CreateElement("score");
xmlWriter.AppendChild(root);
XmlNode apiKey = xmlWriter.CreateElement("APIkey");
apiKey.InnerText = "placeholder";
root.AppendChild(apiKey);
I have many elements to create just as the API Key
, resulting in a lot of variables. So i thought of surrounding each element with braces just for readability like so:
XmlDocument xmlWriter = new XmlDocument();
XmlNode root = xmlWriter.CreateElement("score");
xmlWriter.AppendChild(root);
{
XmlNode apiKey = xmlWriter.CreateElement("APIkey");
apiKey.InnerText = "placeholder";
root.AppendChild(apiKey);
}
apiKey.DoSomething() // Doesn't work because it's out of scope
I believe it's saving some memory and helping me later because the variables don't exist anymore (useful for IntelliSense)
You could say "Just use a function" but I only use this code in this particular part (and calling a function with parameters take some nearly negligeable time but anyway...)
What's your opinion about this ?