Function prototype in header files

I wanted to know the difference between the following two declarations in the header files. I came across both of these styles.

void some_method(int); 

and the second style:

 void some_method(int a); 

Does one style have an edge over another? For me, one style simply does not have a variable name and the variable name must be implemented in the cpp accompanying file.

+6
source share
7 answers

There is no difference in functionality, but I often use parameter names as a form of documentation, for example:

 void verbTheThing(int verbId, int thingId); 

When I have nothing to add, I just do not add it:

 int max(int, int); 
+9
source

The main problem is consistency . Choose one and stick to it. I prefer the latter personally, because parameter names can describe functionality and code that are self-documenting better than an alternative.

+4
source

The main difference is readability. The second version is longer, but having an argument name can be a real help. A small example:

 int move(int, int, int, int); int move (int fromX, int fromY, int toX, int toY); 
+3
source

Some development environments (Visual Studio, Borland C ++ Builder, for example, eclipse should also have this function) automatically list the arguments of the function when entering a bracket ( .

list parameters

For these IDEs, it is generally recommended that you verify that the function argument names (inside the headers) are as detailed as possible, as they serve as documentation.

Even if the programmer does not have an IDE with this function, the forward declarations of the function (in the headers) should always be as descriptive as possible. For the same reason, it is easier to open the header and read which functions should have values, rather than looking for the body of the function within the set of *.cpp files.

Not to mention the fact that in some functions you cannot determine what parameters should mean without parameter names.

For instance,

  Image::blitRect(int, int, int, int); 

may be

  Image::blitRect(int x1, int x2, int y1, int y2); Image::blitRect(int x1, int y1, int x2, int y2); Image::blitRect(int x, int y, int width, int height); 

Of course, a C ++ programmer would add classes for Point and Size in this situation, but that's a different story.

+2
source

Both options have nothing to do with compilation. But the second is better, because assigning a name to a parameter increases the readability of the code and helps people better understand it.

+1
source

Both options are valid. But specifying the name of the parameter in the header can help someone by reading the signature of the function, quickly determining what it actually does and what data the parameters represent.

+1
source

Actually, there is no difference between them. It comes down to the fact that you declare your methods ... For example, if you declare your function before the "main ()" func function. you probably don't need to care about the parameter name. However, before the method, you must enter your parameter name in defintion. It's just a Code Style, allowing you and everyone to read the code.

0
source

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


All Articles