views:

116

answers:

6

I'm thinking that the answer is probably 'no' if the program is small and there are a lot of methods, but what about in a larger program? If I am going to be using one variable in multiple methods throughout the program, is it smarter to:

Come up with a different phrasing for each method (to eliminate naming conflicts). Use the same name for each method (to eliminate confusion) Just use a global variable (to eliminate both)

This is more of a stylistic question than anything else. What naming convention do YOU use when passing variables?

+4  A: 

When passing variables between methods ... there are no naming conflicts, since the variable is local in each method, the same name in both methods eliminates confusion.

def printIt(num):
    print(num)

def f():
    num = 2
    printIt(num)
RSabet
might be good to modify num within printIt, then also print num after the call to printIt to show that the num in f() hasn't changed.
Wallacoloo
I think the requester is aware that the variable is local inside the method. He seems to ask that if you should use 'num' everywhere, is this acceptable or not in large programs and why not ?
Alex Boschmans
+3  A: 

I tend to reuse variable names in local functions a lot, as long as they are mid-process. Usually 'result' for a db lookup or a calculation that is used intermediary and is needed to generate or construct the end-result of the method.

Only rarely have I used global variables, in fact only if I must.

Alex Boschmans
+2  A: 

Python's scoping rules pretty much free you from the need to use different or identical names for variables just to satisfy language needs.

Variable name choice should be driven primarily by readability and maintainability concerns. In general, it is good to use just one name to identify a particular kind of thing throughout the code. This kind of consistency is helpful to readers. Naming variables, methods, and classes is one of the biggest tools you have to make your intent clear---to both yourself and others.

There is a large (and not entirely consistent) literature about naming things. A couple of things to consider:

  1. Names with small scopes can be short names that do not necessarily tell the whole story about the variable, since the entire context for the variable is easily visible. i,j,k as counters in loops are maybe the classic example---but you don't see that much in Python with its dandy for item in collection loop construct.
  2. The flip side of this is that names with larger scopes (instance variables in a class, maybe) should have more completely descriptive names, since they show up in places where the initialization/modification context is not visible.
  3. Try not to put 'noise' pieces into your names. frequencyInfo---what, exactly does the Info part add to things?
  4. Don't include the data structure type in the name---the dict in urldict doesn't really help you much.
  5. Use names from the domain you are working in when you can, but don't force it.
  6. Python style leans towards terse names. Careful thinking and choices often result in a name that is brief, but apt.
Pierce
+1  A: 

Don't go inventing ways of indicating which method the name belongs to. If you are coding correctly you will won't be treating other methods variables as local. If you have a hanle on method foobar and want its foo value use foobar.foo.

Variable names should indicate contents of the variable and aid in understanding the code. Referring to a foo by a lot of different names because a lot of methods use foos won't help. Your time may be better spent clarifying cases where a foo here is not a foo there. For those cases you problably need to use different terminology in at least one case. Document the case in your code.

Using the same name for the variable will help tracing where you use key objects in your code. You will also have a lot of generic variables which make code easier to understand: i, j, and k for interators; result for result for intermediate results; etc.

BillThor
+1  A: 

You should use the same name when it means the same thing. For example, if you have a database of "cars", you might want to use the name "car" to refer to a single car in every method or function that accepts a car as an argument or works with car objects internally. Doing this, someone reading the code will begin to say to themselves "ok, this is a car" rather than "hmmm, I wonder what type of thing 'foo' is ...".

So, be consistent in using variable names, and that means you can use the same name many times as long as it has the same meaning. If at all possible, never use the same name to mean two different things. For example, don't use "item" to mean a car in one context and a random item from a list in another.

Bryan Oakley
+1  A: 

There's no such requirement to give variables different names in different methods or function arguments. In fact, it would greatly hurt readability and go against common sense -- when two things are the same, one tries to give them the same name, not different ones.

Paul Hankin