Argument vs. parameter naming: Preference or convention?
However trivial this may seem, I can't help my curiosity. In your experience, what are some readability considerations when it comes to naming arguments and parameters? Arguments are in function calls and thus may serve a different human-readable purpose than parameters in function definitions. When should an argument name be more specific than its corresponding parameter name and vice 开发者_高级运维versa, or should they carry the same name when possible?
Do you know of any conventions or criteria that would help decide one way or the other? Or would you say this is left to the realm of preference?
Thanks in advance.
Parameter and argument as terms are usually used interchangeably, but if we want to get more specific, then it usually boils down to this:
A parameter is just a name in function's definition.
An argument is an object that will be passed to that function.
So, let's say you've got a function that accepts a database connection:
public void doDatabaseStuff (DatabaseConnection dbConnection) {...}
Here, dbConnection
is a parameter.
When you actually use this function, if you're using an Oracle database, you can name that DatabaseConnection
as oracleDbConnection
and use it like this:
DatabaseConnection oracleDbConnection = // some code to actually obtain that connection
doDatabaseStuff(oracleDbConnection);
Here, we have two things:
dbConnection
is the parameter nameoracleDbConnection
is the argument name
From this, we can see that the parameter name is usually more "generic" than the argument name, but it doesn't have to be so. You can also just use dbConnection
as the argument name, but oracleDbConnection
is more meaningful.
ummm....When using a 3rd party/or framework method, you may want to make the argument name more succinct/ other than this, surely you would want these to match for consistency's sake...
精彩评论