for example:
int a;
int b;
int value = getValue(a,b);
private int getValue(int a, int b)
{
int value = a+b;
return value;
}
is the above practical or is it considered to be bad practice and would cause problem later in the development.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I realize that it’s a contrived example to demonstrate what you’re asking, but your example does contain a naming problem which I’ll point out:
The problem isn’t in whether or not the variable names match or don’t match what they’re called in the method. The problem is that the variable names aren’t called anything meaningful. This is considerably more of an issue than what you’re asking.
Let’s re-factor your method to make the example slightly less contrived…
The method is a bit cleaner now and more intuitively reflects its purpose. Now we re-ask the question… Should
aandbbe renamed to match the ones in the method?Most likely not. The names in the method have been changed to indicate their context. The method is getting a sum of two values, the first one and the second one. So what is the context of
aandb? Are they also known only as the first one and the second one? Or do they convey some other meaning that’s not readily available? Something like:or:
or any other example of two values which would need to be summed for some purpose. If we added that context to the variable names then we most certainly wouldn’t want to add it to the method. The method should be as general-purpose as possible, performing a single task without any concern outside of that task.
In short, it’s neither good nor bad practice, because it’s not something to even consider. There may be times where, by coincidence alone, the names are the same. (This can often happen with private helper methods, for example.) But they’re not the same as a result of a standard or practice to be followed, but rather as a result of coincidentally having the same meaning.