Reputation: 1804
What is better coding practice and why?
string str1 = textBox1.Text;
string str2 = textBox2.Text;
void FunctionName (str1, str2);
Or
void FunctionName(textBox1.Text, textBox2.Text);
Upvotes: 0
Views: 258
Reputation: 2070
I think coding standards rules that enforce one approach over the other are often too draconian. I tend to use the approach that is most READABLE in each individual circumstance. That might even vary within the same method!
In the example you give, then I think the second option is actually more readable and understandable - it very clearly shows the relationship between the function call and the two controls.
In @O.D's example where extra things are being 'done' to the control values before they are passed in it becomes a little harder to see what's happening, and pulling 'confused' variables out and naming them in a way that explains what you're trying to achieve makes the code more readable (and self documenting).
Generally I'd say:
Upvotes: 3
Reputation: 3140
First off it doesn't make a difference in regard to speed of processing. Both will take the same amount of time. Option2 is the one i would choose as it is easier to read IMO.
In certain situations it is useful to place values into variables simply to allow you to place a breakpoint on them when you are debugging code. It doesn't really apply to this example but there are situations where i would choose option1 just for this reason.
Upvotes: 2
Reputation: 101231
There is no need to declare separate variables when you don't plan to do anything with it. So I say the latter is better.
In the former example, you are doing unecassery work and adds more complexity to your code.
Now, if you plan to do some processing on those values, you could choose to use the former method. But you should give them more descriptive names than str1
and str2
to communicate their purpose.
Upvotes: 8
Reputation: 37566
I would say the 1st Option is better, if anyone needs to do any operation on the String before passing it to the function he will tend to do this on the function call which causes nested calls over time which makes it more and more unreadable, as it would look like:
FunctionName(AnotherFunc(textBox1.Text), AnotherAnotherFunc(textBox2.Text));
etc.
having the string between makes it easier to make changes which stil readable.
Upvotes: 4
Reputation: 9011
The former version will be required in case you perform some sort of sanity check on the user input. For example, you could check the textbox text for string.Empty
, etc. In your case, creating another variable does not make sense and it redundant.
From the point of view of .NET, the execution will not change as it will be optimized during the JIT.
For example,
string str1 = textBox1.Text.Trim();
string str2 = textBox2.Text.Trim();
if(!string.IsNullOrEmpty(str1) && !string.IsNullOrEmpty(str2))
{
FunctionName (str1, str2);
}
or
string str1 = textBox1.Text.Trim();
string str2 = textBox2.Text.Trim();
if(ValidateInput(str1) && ValidateInput(str2))
{
FunctionName (str1, str2);
}
Upvotes: 2
Reputation: 2005
Form the performance point of view it will be the same because compiler will optimize it so it doesn't matter what practice you choose.
Upvotes: 2
Reputation: 14640
Personally, I'd go with option 2, seeing as it has less code so it is easier to read and understand for another developer coming to the code (much) later on.
I'd imagine the compiler would optimise the first one into the second anyway (but I'm not sure that is true).
Upvotes: 2