Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why comment parameter names rather than leave it as it is

Tags:

c++

winapi

Sometimes I see code like this:

LRESULT OnPaint(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)

Why comment parameter names rather than leave it as it is?

like image 369
EFanZh Avatar asked Feb 07 '12 11:02

EFanZh


People also ask

Do parameter names and argument names have to be the same?

The caller's arguments passed to the function's parameters do not have to have the same names.

What's the difference between a parameter and an argument?

The values that are declared within a function when the function is called are known as an argument. Whereas, the variables that are defined when the function is declared are known as a parameter.

What is the difference between a parameter and a function?

Function parameters are the names listed in the function's definition. Function arguments are the real values passed to the function. Parameters are initialized to the values of the arguments supplied.

What is the difference between a parameter and an argument Linkedin?

A parameter is a variable in the declaration of a function. An argument is the value of this variable that gets passed to the function.


4 Answers

I think there are/were some compilers which issued a 'argument not used' warning when, well, the arguments were not used.

Especially in message handler functions like the one you showed, the arguments are often not necessary to perform the task. So by commenting the argument names out, you could get rid of the warnings.

On the other hand, if you later want to use some of the arguments, it's useful to have the names at hand, so commenting out is easier than just deleting the names.

like image 168
MartinStettner Avatar answered Oct 05 '22 23:10

MartinStettner


One reason I see for doing this is that you explicitly want to tell other programmers not to use the parameters, but leave them in comments for a description of their intent. I know this doesn't make sense now, but read on.

I'll use a different example:

class A
{
public:
   virtual void foo(int someProperty);
};

class B : public A
{
public:
   virtual void foo(int /*someProperty*/);
};

Say you want this for a specific case where you want B::foo() to do some extra stuff, and then call A::foo() with the parameter 0. You have to keep the same function signature, so polymorphism works, but, inside B::foo(), you're not actually using the parameter. Nor do you want to use it in the future. It's basically a statement of intent, saying "the logic of this method should not depend on someProperty".

B::foo(int/*someProperty*/)
{
    //do some stuff
    A::foo(0);
}

With the parameter name commented out, you can't really use it (unless you get down to some hacking). But the commented name tells you something about the parameter you pass to A::foo() - its 'someProperty' from A.

Now, I don't agree with the syntax, but this can be a possible explanation.

like image 21
Luchian Grigore Avatar answered Oct 05 '22 22:10

Luchian Grigore


If the parameters are really not needed, you don't need to name them

If you don't name them, at first glance you may not understand why they are there in the first place.

If you name them, some compilers will you warn you about unused parameters.

Leaving the names as comments is a middle ground between the two approaches.

like image 30
R. Martinho Fernandes Avatar answered Oct 06 '22 00:10

R. Martinho Fernandes


If that is the beginning of the definition of onPaint() I have seen commented out argument names used to avoid "unreferenced formal parameter" compiler warnings (these warnings will only appear at a high warning level, level -W4 for Microsoft compilers).

like image 44
hmjd Avatar answered Oct 05 '22 23:10

hmjd