Sign in to follow this  
Mybowlcut

[C++] Function parameter naming across files

Recommended Posts

I recently stumbled upon something that could have potentially caused me a lot of trouble. It was similar to the code below except it was components of a rectangle.
class A
{
public:
void Function(std::string s1, std::string s2);
};

void A::Function(std::string s2, std::string s1)
{
std::cout << "Your name is " << s1 << " " << s2 << std::endl;
}

int main()
{
A a;
a.Function("John", "Smith");
}

// Outputs: Your name is Smith John




I've never had this problem in my ~5 years of programming with C++ and was curious if people avoid naming the parameters of the function declared in the header file for this reason.. or if some people go even further and don't use .cpp files at all?

Also, do any newer compilers pick up this type of error and give a warning?

Cheers.

Share this post


Link to post
Share on other sites
Quote:
Original post by Mybowlcut
I've never had this problem in my ~5 years of programming with C++ and was curious if people avoid naming the parameters of the function declared in the header file for this reason..

Personally I never stumbled over that reasoning. The point of avoiding is, afaik, that they are often not needed or do not add information, especially (but not exclusively) in pretty typed code, like

class Circle { 
public:
Circle (Radius, Position);
};


. The less your write, the less you have to maintain.

On the other hand, sometimes, especially in intellisense+co. situations, explicit naming can be useful, like

class WavelengthAmplitudePair { 
public:
WavelengthAmplitudePair (float wavelength, float amplitude);
};


. Of course, having unique types "Wavelength" and "Amplitude" would have been even better as for letting the compiler check stuff that can otherwise only be found via human inspection or testing.

Quote:
or if some people go even further and don't use .cpp files at all?

I imagine that some do. But I don't recommend this, you'll soon get into include and dependency hells.

Quote:
Also, do any newer compilers pick up this type of error and give a warning?

To my knowledge, no. Sidenote: Errors are distinct from warnings ;)


Share this post


Link to post
Share on other sites
Quote:
Original post by phresnel
Quote:
Original post by Mybowlcut
I've never had this problem in my ~5 years of programming with C++ and was curious if people avoid naming the parameters of the function declared in the header file for this reason..

Personally I never stumbled over that reasoning. The point of avoiding is, afaik, that they are often not needed or do not add information, especially (but not exclusively) in pretty typed code, like

class Circle { 
public:
Circle (Radius, Position);
};


. The less your write, the less you have to maintain.

On the other hand, sometimes, especially in intellisense+co. situations, explicit naming can be useful, like

class WavelengthAmplitudePair { 
public:
WavelengthAmplitudePair (float wavelength, float amplitude);
};


. Of course, having unique types "Wavelength" and "Amplitude" would have been even better as for letting the compiler check stuff that can otherwise only be found via human inspection or testing.

So would you find a function declaration without named parameters in a commercial library? I'd guess no, seeing as they're rigorously tested before and after release so mistakes like mine above wouldn't be run into as often...

Do you (personally) use names? I do because I admittedly rely on intellisense. There are probably other reasons but I can't think of them...

Quote:
Original post by phresnel
Quote:
or if some people go even further and don't use .cpp files at all?

I imagine that some do. But I don't recommend this, you'll soon get into include and dependency hells.

Oh, I hadn't thought about that.

Quote:
Original post by phresnel
To my knowledge, no. Sidenote: Errors are distinct from warnings ;)

Argh, I knew someone would say this haha.. you know what I mean. :p

Share this post


Link to post
Share on other sites
Quote:
Original post by Mybowlcut
Do you (personally) use names?

It depends. Even if I *should* have unique types for all entities (think of e.g. radiometric units for my ray tracer), I don't have actually; idea came too late in development. Hence, where disambiguation is needed, or were it adds important information (e.g. struct Length { Length(float meters); }). But were it doesn't add information, I often don't use them.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this