问题
I am currently studying C# and I really want to get a good coding style from the beginning, so I would like to hear opinions from you professionals on this matter.
Should you always (or mostly) use local variables for conditions/calculations (example 2) or is it just as good/better to use statements directly (example 1)
Example 1.
if (double.TryParse(stringToParse, out dblValue)) ...
Example 2.
bool parseSuccess = double.TryParse(stringToParse, out dblValue);
if (parseSuccess) ...
It would be interesting to hear your thoughts and reasoning at this example.
回答1:
You should use the more verbose style if putting it all in one line would make it too long or complicated.
You should also use a separate variable if the variable's name would make it easier to understand the code:
bool mustWait = someCommand.ConflictsWith(otherCommand);
if (mustWait) {
...
}
In such cases, you should consider using an enum for additional readability.
回答2:
I see a lot of example 1 in production code. As long as the expression is simple, and it's easy to understand the logic of what's happening, I don't think you'll find many people who think it is bad style.
Though you will probably find a lot of people with different preferences. :)
回答3:
Heres the rule I use: Keep it on one line if you can quickly glance over it and know exactly what it's saying. If its too complicated to read as quickly as you could read any other text, give it a local variable. In any case, though, you don't want a really long if statement header. So if it's too long, split it up.
回答4:
I suggest you use a local variable like here:
bool parseSuccess = double.TryParse(stringToParse, out dblValue);
if (parseSuccess) ...
For two reasons:
1. You can use more times the variable without parse your double another time.
2. It makes the code more readable.
Consider this:
if(double.TryParse(string1, out Value1) && double.TryParse(string2, out Value2) && double.TryParse(string3, out Value3) && double.TryParse(string4, out Value4))
{
//some stuff
}
It's too long and it makes the code hard to be read. So sometimes local variabels make the code a lot more readable.
回答5:
The clarity of the source code is an important parameter especially in application maintenance but so is performance.
Insignificant it may seem, sometimes using simple syntax "tricks" of programming languages, we get very good results.
If I think I'll use later in the code somehow the result, I use variables, otherwise I give priority to direct sentences.
回答6:
There's no right option. Both are perfectly acceptable.
Most people choose the first option if you don't have a lot of conditions to concatenate because it results in less lines of code.
回答7:
As you said you are studying C#
So my vote will be this style for you
bool parseSuccess = double.TryParse(stringToParse, out dblValue);
if (parseSuccess) ...
If you are studying you will have lot to learn and the above style clearly tells you that TryParse return a bool, so you won't have to worry or find whats the return type for TryParse
来源:https://stackoverflow.com/questions/11442210/local-variables-or-direct-statements