views:

89

answers:

4

Lets say I have the following code

public static string GetXMLValue()
{
    XDocument settingsFile = XDocument.Load("Settings.xml");
    return settingsFile.Element("Settings").Element("GenericValue").Value;
}

It simply reads an XML Settings file and returns the GenericValue value. It can't be any simpler than that. Now my question is, would it provide any benifit (readability, performace, syntactically, maintainablitiy, etc.) to first place the return value in a string variable then return? Or is it best left the way it is?

+1  A: 

From a readability perspective, assigning the values to a variable and returning it would definitely help.

Dienekes
@Dienekes: yes add the variable, or yes leave it the way it is?
Adkins
^^ Updated the answer.
Dienekes
+4  A: 

There are a couple situations in which I see value in creating an auxiliary variable:

  • I want to assert something about it as a precondition (e.g. not empty string; a minimum/maximum length; etc.)
  • I am having trouble and I want to debug the value more easily.

Even in the absence of these, for such a nontrivial expression, I would create a local variable, to make the function more readable.

Daniel Daranas
Would there be any kind of performance hit in simply creating random variables simply for the purpose of readability?
Adkins
Your point about debugging is important. You cannot see the value returned by the function in the debugger unless it is assigned to a variable.
Martin Liversage
@Adkins, worrying about the performance hit of using an extra local variable in such a simple function would be premature optimization.
Daniel Daranas
It will likely compile to identical code anyway. Plus, the function is very trivial.
DeadMG
@Daniel: Isn't it better to optimize your code as you are writing instead of spending a bunch of time doing it once you are done?
Adkins
@Adkins: But this is not an optimization in the first place. You won't notice anything and as @DeadMG said, the compiled code will probably be identical.
Daniel Daranas
+7  A: 

To be honest, the simplicity of the methods makes it readable even in "one" line:

public static string GetXMLValue() 
{ 
    return XDocument
             .Load("Settings.xml") 
             .Element("Settings")
             .Element("GenericValue")
             .Value; 
} 
ck
I agree. For such a simple method, anything other than this is overkill.
Bigfellahull
But with this style isn't the readability and maintainability for future programmers who inherit this program going to suffer? I can read it fine, but I am just trying to get a feel for what other developers think about this.
Adkins
If they can't read this clearly, they aren't cut out to be programmers. Adding more text to set up variables makes it more cluttered, this is the most clear and consice it can be.
ck
However I do conceed that debugging this isn't straightforward, however as long as the Xml is fairly simple, this shouldn't really need to be debugged, **AND** you can debug chunks, like `XDocument.Load("Settings.xml").Element("Settings")`
ck
+2  A: 

would it provide any benifit [...] to first place the return value in a string variable then return? Or is it best left the way it is?

The function is so simple it just does not matter, so don't lose sleep about it. Once the function becomes more complex, you can always rethink this.

If for example you later need to run checks on the value before returning it, or want to log it for auditing reasons, a separate variable will make sense. Until then, leave it as it is.

As an aside:

What I find much more questionable is that you are reading an external resource (file) in a getter method. Invoking operations that can have side effects (such as reading a file) in a getter is bad style IMHO. That way for example every caller of the getter will have to handle IOExceptions from reading the file.

Consider changing this, for example by passing in the information via the constructor (either read the file from the constructor, or pass in an object that takes care of supplying the information). This will decouple your design, and simplify e.g. reuse and unit testing.

sleske
You could use the quote tags for quoting instead of the code tag, thats what it is made for (i still like fancy checkered text) ;)
atamanroman
This isn't actually the function that I am using. I understand what you are saying and the file is actually loaded with exception handeling in the constructor. I just added that in here so people wouldn't ask about where the file came from. The class is simply designed to return information from the settings XML.
Adkins
@fielding: I am actually not sure what you are referring to. Could you give an example please.
Adkins
@adkins im referring to sleskes post, he used code tags to quote text instead of using the quote tags. This makes the quote harder to read.
atamanroman
ah. Here I was trying to figure out what on earth you were talking to in relation to my code :D
Adkins
@fielding: Fixed quoting; thanks for pointing this out.
sleske