Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a better way to comment the parameters' direction in code?

I want to improve the readability of the code. So I commented the parameter's direction in the code like this:

#define IN
#define OUT

void Add(IN int Para1, IN int Para2, OUT int& Result);

But I think the compiler will replace every instance of IN and OUT with blank and it could be quite a problem some times.

So is there a better way? Thanks.

(I use C++.)

like image 290
William.Zhou Avatar asked Nov 29 '22 03:11

William.Zhou


2 Answers

You can try to put it in comments. That is much better and readable.

void Add(/*IN*/ int Para1, /*IN*/ int Para2, /*OUT*/ int& Result);
like image 85
Ajit Vaze Avatar answered Dec 05 '22 13:12

Ajit Vaze


Yes: Forget those things and use constness. That will work as long as you don't have a "in" and "out" parameter, which is and should be rarely used.

void foo(int i, const std::string& s, std::vector<char>& out_buf);
// i and s are obviously "in" variables, while out_buf could be both, 
// but you can easily show that by giving the parameter a proper name.

Edit: And const correctness does not mean to make value parameters const! This doesn't give the caller any additional information at all, since you can't change his variables either way.

like image 38
cooky451 Avatar answered Dec 05 '22 12:12

cooky451