Method Parameter id similar to Class Field Best Practice?
I have some methods which their parameters are related to the fields, and have the same ids or similar ids.
Some programmming languages doesn't allow this, some do, which do you consider a "Best Practice" (for cross-language) ?
(The example is C++ alike, but apply to any progr. lang.)
Example:
public class AnyClass {
private string FilePath = "";
public void assignPath(string FilePath) { ... }; // <-- same as field member
public void assignPath(string AFilePath) { ... }; // <-- has a prefix
public void assignPath(string filePath) { ... }; // <-- different case
}
Cheers.
UPDATE: add "cros开发者_开发技巧s language"
One thing for sure. Don't use parameter names starting with capital letter. I haven't seen those as best practices in any language I know. Same for fields that are not public.
I like the third option in your example.
In general I believe "PascalCase" is used to denote static fields, and "camelCase" is used for instance fields. Also, as a general rule of thumb all method arguments should probably be "camelCase" or just "lower" if possible (I think a short truncation for a method argument is fine due to the limited scope).
However, I don't think having your method parameter arguments match exactly to instance field names is ideal in any situation. As far as prefixes go, any Hungarian notation should probably be avoided.
精彩评论