It is
not a style choice.
The variable name given in the function should make sense IN THE FUNCTION. Here is an example function:
1 2 3 4
|
int addTwoNumbers (int firstNumberToAdd, int SecondNumberToAdd)
{
return (firstNumberToAdd + SecondNumberToAdd);
}
|
This is a very simple function for example purposes. You can look at it and just be reading the first line, understand what the input parameters are for. It is easy to understand, because I chose names for the parameters that make it clear what they do in the function.
Now, I have the function. I am writing a program that involves, for example, cats in boxes. I have variables:
1 2
|
int catsInBoxOne;
int catsInBoxTwo;
|
This code is easy to understand. Each variable is the number of cats in a box.
Later, I need to know how many cats I have in total, so I use my function:
int numberOfCats = addTwoNumbers(catsInBoxOne, catsInBoxTwo);
This code is easy to understand. It is adding
catsInBoxOne
and
catsInBoxTwo
. Everything is easy to understand because I picked sensible variable names.
Are you seriously suggesting that I rename my variables
catsInBoxOne
and
catsInBoxTwo
to
firstNumberToAdd
and
SecondNumberToAdd
? That would make the code harder to understand, and would create the impression that these variables are just numbers I want to add, instead of what they actually are. If I ever did anything other than adding them together, would be confusing; what happens if I have a different function for doing something else:
1 2 3 4
|
int subtractTwoNumber(int numberToSubtractFrom, int numberToSubtract)
{
return (numberToSubtractFrom - numberToSubtract);
}
|
and I am going to apply this function as well to my variables
catsInBoxTwo
and
catsInBoxOne
. Now what do I name them? I'm going to use both functions. Should I rename my variables (according to your naming scheme where variable names match function parameters)
numberToSubtractFrom
and
numberToSubtract
, or
firstNumberToAdd
and
secondNumberToAdd
?
Functions can be used many times. That's why we have them. If I made my variables match the names used in the function, how could I ever use the function repeatedly?
1 2 3 4
|
int catsInBoxOnefirstNumberToAdd;
int catsInBoxTwosecondNumberToAdd;
int catsInBoxThree;
int catsInBoxFour;
|
What do I rename catsInBoxThree and catsInBoxFour? I can't rename them firstNumberToAdd and secondNumberToAdd, because those names are already being used.
Functions exist to make my life easier. If I write a function that I use again and again, I do not want a function that I might have written years ago to force me to make my code harder to understand. That's insane.
I will just assume that it is just a style choice. |
It is NOT a style choice.
It is NOT a style choice.
It is a fundamental part of the reason we have functions and to misunderstand that is to misunderstand why we have functions. Right now, you're missing the point.