是一个好的设计,通过参数返回值? [英] Is it a good design to return value by parameter?
本文介绍了是一个好的设计,通过参数返回值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
bool is_something_ok(int param,SomeStruct* p)
{
bool is_ok = false;
// check if is_ok
if(is_ok)
// set p to some valid value
else
// set p to NULL
return is_ok;
}
此函数返回true,并将p设置为有效值
否则返回false并将p设置为NULL
this function return true and set p to a valid value if "something is ok" otherwise return false and set p to NULL
这是一个好的还是坏的设计?用它。
如果没有文档和评论,我真的不知道如何使用它。
Is that a good or bad design? personally, i feel uncomfortable when i use it. If there is no document and comment, i really don know how to use it.
BTW:有没有一些关于API设计的权威书/文章? / p>
BTW:Is there some authoritative book/article about API design?
推荐答案
由于您已将问题标记为C ++而不是C,我建议您:
Since you have tagged the question as C++ and not C, I would suggest you to:
- 直接返回值
- 如果您有多个值,请使用输出参数
- 使用非const引用作为输出参数(而不是指针),并使用const引用作为输入参数。
- 如果出现错误,引发异常而不是返回false或-1。
- return the value directly
- if you have more than one value, use output parameters
- use non-const references as output parameter where possible (instead of pointers), and use const-references for the input parameters.
- if something went wrong, raise a exception instead of returning false or -1.
但这只是一些一般的提示。最好的方法总是取决于具体的问题...
But that are just some general hints. The best way to go always depends on the specific problem...
这篇关于是一个好的设计,通过参数返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文