[TriLUG] names of parameters passed down a chain of functions
Steve Litt
slitt at troubleshooters.com
Fri Jul 3 16:37:19 EDT 2009
On Friday 03 July 2009 04:33:05 pm Joseph Mack NA3T wrote:
> When I learned programming, I was told that a variable
> should have a different name when it arrives in a function
> (as a parameter). When the new variable is passed to the
> next function, it should have yet another name. It would be
> nice if the names were related, so that the reader would
> connected the various names for the parameters (eg
> image->pixels->my_pixels). However I don't have a sensible
> consistent naming scheme if I do this.
>
> The alternative is to use the same name at all levels and
> let scoping rules handle it for you. This sounds the
> simplest way of handling it.
>
> What do you do?
>
> Thanks Joe
Most languages treat arguments as local variables, so $mystring inside a
subroutine does not interfere with $mystring elsewhere in the program. If you
are programming in such a language (C, Perl, Python and Ruby are examples),
then having the same name is probably best, IF AND ONLY IF the same name does
not mislead the application programmer.
As an example of misleading, let's say you write a generic sort routine in
program that sorts a list of people by their score on a test. The following
would be misleading:
void mysort(void *people, (void *compare)(void *, void*))
Instead, even though in the application the people array might be
called "people", within the generic sort I'd call it "array":
void mysort(void *array, (void *compare)(void *, void*))
SteveT
Steve Litt
Recession Relief Package
http://www.recession-relief.US
Twitter: http://www.twitter.com/stevelitt
More information about the TriLUG
mailing list