Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I realize that it's a contrived example to demonstrate what you're asking, but your example <em>does</em> contain a naming problem which I'll point out:</p> <pre><code>int a; // &lt;---- right here int b; // &lt;---- and here int value = getValue(a,b); // &lt;--- and a little here private int getValue(int a, int b) { int value = a+b; return value; } </code></pre> <p>The <em>problem</em> isn't in whether or not the variable names match or don't match what they're called in the method. The <em>problem</em> is that the variable names aren't called anything meaningful. This is considerably more of an issue than what you're asking.</p> <p>Let's re-factor your method to make the example slightly less contrived...</p> <pre><code>int a; int b; int value = GetSum(a,b); private int GetSum(int firstValue, int secondValue) { return firstValue + secondValue; } </code></pre> <p>The method is a bit cleaner now and more intuitively reflects its purpose. Now we re-ask the question... Should <code>a</code> and <code>b</code> be renamed to match the ones in the method?</p> <p>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 <code>a</code> and <code>b</code>? 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:</p> <pre><code>int milesToFirstDestination; int milesToSecondDestination; </code></pre> <p>or:</p> <pre><code>int heightOfPersonInInches; int heightOfStepstoolInInches; </code></pre> <p>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.</p> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload