views:

833

answers:

9

Hello.

I'm a fairly new C++ programmer and I would like to hear the arguments for and against naming parameters within the class declaration.


Here's an example:

Student.h

#ifndef STUDENT_H_
#define STUDENT_H_

#include <string>

using namespace std;

class Student
{
    private:
     string name;
     unsigned int age;
     float height, GPA;

    public:
     Student(string, unsigned int, float, float);

     void setAge(unsigned int);
};

#endif /*STUDENT_H_*/

vs.

#ifndef STUDENT_H_
#define STUDENT_H_

#include <string>

class Student
{
    private:
     string name;
     unsigned int age;
     float height, GPA;

    public:
     Student(string name, unsigned int age, float height, float GPA);

     void setAge(unsigned int age);
};

#endif /*STUDENT_H_*/

Student.cpp

#include "Student.h"

Student::Student(   string name,
      unsigned int age,
      float height,
      float GPA) :

      name(name),
      age(age),
      height(height),
      GPA(GPA) {}

void Student::setAge(unsigned int age) { this -> age = age; }


I cannot decide. On the one hand, I feel that it is redundant to name the variables in both the declaration (.h) and the definition (.cpp). Especially since you have to worry about updating the names in both places so that they match. On the other hand, without names, it can often be confusing to determine what variables the parameters correspond to just by looking at the declaration.

So, what are your thoughts?

+3  A: 

Put the names in both places, clarity is the reward you get for the task of maintaining the signatures in two places.

Binary Worrier
+12  A: 

It is much better to use the parameter names in the declaration, and use good parameter names. This way, they serve as function documentation. Otherwise, you will have to write additional comments in your header, and it is always better to use good parameter/variable names than to use comments.

Exception: when a function must have a certain signature for external reasons, but the parameters are not actually used. In this case, you should not name them in the implementation either.

Gorpik
Exception: when the parameters are obvious or the types are self-documenting, e.g., `Point3D(T, T, T)` and `Pair(Key, Value)` are both abundantly clear.
Jon Purdy
+2  A: 

Put the names in both - you will thank yourself for it later.

anon
+1  A: 

Even if redundant, I find that it is better to have parameter names in both places. This is typically because, changing a parameter name often has semantic consequences. Missing it in the header helps screw up the documentation (which is where I tend to put most of the comments i.e. API specifications) and missing it in the implementation helps me forget what why that particular parameter has such an odd name.

The only time I forego a parameter name is when I have to implement a third party library callback and I am not using one of the parameters. Even then I'd do:

 my_callback(int idx, Context* /*ctx*/)  { ...

so that I know the signature well.

dirkgently
A: 

Yes, it is not necessary to name the parameters in .h file. A header file is supposed to represent an interface, so it need not have unneeded details.

HTH

Abhay
It seems like the consensus on the question you linked is to not use an underscore for member variables, which I don't. While some people might find it annoyingly verbose, I tend to use the "this" keyword as in the example I posted above. I'm not sure what you were trying to illustrate with that link.
Scott
True - and "float, float" is NOT the interface. "float height, float GPA" is the real interface.
MSalters
Sorry, i have removed the underscore link. It was meant for someone else :-)@MSalters: I agree that parameter names add to the meaning an interface provides, but IMHO relying on that is not the ideal way. What if someone renames them differently in the header and then in the impl. file. It will be more confusing. From a maintainance perspective, i feel leaving the names out of the header is helpful.
Abhay
Disagree completely, the interface is not just the function signature, but the intended meaning: paramter names are part of the interface. Changing the name in one place but not the other is an error, even if the compiler will not flag it.
David Rodríguez - dribeas
+3  A: 

Intellisense/autocomplete/whatever similar is in development environments usually only sees the declaration and will only show it as autocomplete. So if you don't declare names in the declaration the users will not see them in autocomplete unless they go and read the source. That's perhaps tolerable, but not very convenient.

sharptooth
Oh, by all means, I put the names in the declaration. Perhaps my post wasn't clear enough, but I was trying to get reasons for or against *additionally* putting names in the header.
Scott
Oops! Scratch that. I read "definition" as opposed to "declaration" in your answer. Thanks for the information.
Scott
A: 

If you ever release your code as a librray, with associated .h file, your users will never see the definition, only the declaration, adding an exztra documentation burden on yourself.

Visage
A: 

On the one hand, I feel that it is redundant to name the variables in both the declaration (.h) and the definition (.cpp). Especially since you have to worry about updating the names in both places so that they match.

You don't need the names to match literally. The header file, which specifies the interface, works a bit like an imperfect contract (imperfect because it does not contain preconditions and postconditions, unless you write them down in comments) and a "caller's guide". The caller of the class will want to know what the parameters are, in 99% of the cases. At least so that he knows what's going on. So you must choose a parameter name that makes sense for the caller. This doesn't need to be identical to the name in the cpp. However this doesn't matter much, because I'm used to copy/past the function signatures from the .h to the .cpp in the first place. For me, programming in C++ implies this manual part.

On the other hand, without names, it can often be confusing to determine what variables the parameters correspond to just by looking at the declaration.

That's the good hand.

Daniel Daranas
A: 

I suppose it depends on how descriptive your variable types are. If your method signature contains types used for multiple purposes, then it's useful:

double calculateTax(int, string);

If the types are descriptive, then including the names is redundant.

Money calculateTax(Order, State);

I'd prefer not to maintain the names in two files.

brianegge