Assignment of functions Parameter Names of names ( ++)

In a function declaration, while parameters should not be named, is it preferable to name them? What are the advantages and disadvantages of this?

+4
source share
6 answers

The advantage of naming them is that you can refer to them in the documentation. (This includes your editor / IDE, collecting them and presenting them to you as hints when entering a call to such a function.)

The disadvantage would be that the names can change according to the implementation of the function, except for the fact that the names in the function declaration may differ from the names in the function definition. (IOW: I don't see a flaw.)

+7
source

For documentation reasons, mainly

+3
source

Naming options have a significant advantage when working with IDEs that support automatic completion. When you start typing your function name, a list of suggestions appears in the IDE. findUser(string firstName, string lastName) through findUser(string firstName, string lastName) tells you much more than just findUser(string,string) .

+3
source

It is rather a matter of style. Personally, if I see a function declaration without named parameters, I find it difficult to read. I think the more you type C ++, the better.

+1
source

One of the advantages is that they can convey more information when the types of parameters are the same.

Consider

 CoordSystem CreateCoordinateSystem( const UnitVector& xdirection, const UnitVector& ydirection, const UnitVector& zdirection ) 

over

 CoordSystem CreateCoordinateSystem( const UnitVector& , const UnitVector& , const UnitVector& ) 
+1
source

The declaration does not have any programmatic advantages or disadvantages. However, there is one advantage of the style that I can think of:

If you are not using a parameter in a function, you cannot name this parameter in a declaration AND definition:

 void foo(int); void foo(int) { } 

Naming parameters that you do not use in the definition are not illegal, but this is a warning! The approval of the style that I mentioned will not point to the parameters in the declaration, so anyone who views the header file will know that a particular parameter is not used in the definition. However, this is only if you synchronize the unnamed name between the definition and the declaration.

Here is an example of when you can omit the parameter:

 class Base { virtual void foo(int a) = 0; }; class Child1 { virtual void foo(int a) { std::cout << a + 1 << std::endl; } }; class Child2 { virtual void foo(int) { std::cout << "mwahaha" << std::endl; } }; 

In this case, the parameter is not named, but it must still be specified, since the prototype of the function must match the name of its parent.

0
source

Source: https://habr.com/ru/post/1384968/


All Articles