q****x 发帖数: 7404 | 1 如下代码,检查空指针和清空集合的责任应该是caller还是callee?按garbage in
garbage out的原则,这两个语句都可以省掉,caller自己负责。但这样好吗?
大家倾向哪种风格?理由是?
void GetNames(set* names) {
if (names == nullptr) return;
name.clear();
for (const string& name = stream.get()) {
names.insert(name);
}
return;
} |
w**z 发帖数: 8232 | 2 callee 我是一定会check, 保护你的程序。至于是否清空,看你怎么定义你的接口。
【在 q****x 的大作中提到】 : 如下代码,检查空指针和清空集合的责任应该是caller还是callee?按garbage in : garbage out的原则,这两个语句都可以省掉,caller自己负责。但这样好吗? : 大家倾向哪种风格?理由是? : void GetNames(set* names) { : if (names == nullptr) return; : name.clear(); : for (const string& name = stream.get()) { : names.insert(name); : } : return;
|
p***o 发帖数: 1252 | 3 For performance, use assert. For robustness, use exception.
Ignore it as your code does only if the spec requests that explicitly.
【在 q****x 的大作中提到】 : 如下代码,检查空指针和清空集合的责任应该是caller还是callee?按garbage in : garbage out的原则,这两个语句都可以省掉,caller自己负责。但这样好吗? : 大家倾向哪种风格?理由是? : void GetNames(set* names) { : if (names == nullptr) return; : name.clear(); : for (const string& name = stream.get()) { : names.insert(name); : } : return;
|
q****x 发帖数: 7404 | 4 你说了三种检查策略。但这里的问题是该不该检查。
【在 p***o 的大作中提到】 : For performance, use assert. For robustness, use exception. : Ignore it as your code does only if the spec requests that explicitly.
|
a*****g 发帖数: 19398 | 5 对。
【在 p***o 的大作中提到】 : For performance, use assert. For robustness, use exception. : Ignore it as your code does only if the spec requests that explicitly.
|
d****i 发帖数: 4809 | 6 我认为不用在callee里面检查,C++里面这种传指针进函数的用的太多了,要是每个都
像你这么检查的话overhead太大,code也看着丑陋。应该由caller来检查。
【在 q****x 的大作中提到】 : 如下代码,检查空指针和清空集合的责任应该是caller还是callee?按garbage in : garbage out的原则,这两个语句都可以省掉,caller自己负责。但这样好吗? : 大家倾向哪种风格?理由是? : void GetNames(set* names) { : if (names == nullptr) return; : name.clear(); : for (const string& name = stream.get()) { : names.insert(name); : } : return;
|
g*********e 发帖数: 14401 | 7 debugger build用assert检查,release build不检查 |
d****n 发帖数: 12461 | 8 看是公共接口还是私有接口。私有的一般都不检查,留下debug想象空间。公共的做好
定义内的检查,留下记录。
【在 q****x 的大作中提到】 : 如下代码,检查空指针和清空集合的责任应该是caller还是callee?按garbage in : garbage out的原则,这两个语句都可以省掉,caller自己负责。但这样好吗? : 大家倾向哪种风格?理由是? : void GetNames(set* names) { : if (names == nullptr) return; : name.clear(); : for (const string& name = stream.get()) { : names.insert(name); : } : return;
|
Y**G 发帖数: 1089 | 9 If you write public API, you should not trust caller. Internal api is
different story.
【在 q****x 的大作中提到】 : 如下代码,检查空指针和清空集合的责任应该是caller还是callee?按garbage in : garbage out的原则,这两个语句都可以省掉,caller自己负责。但这样好吗? : 大家倾向哪种风格?理由是? : void GetNames(set* names) { : if (names == nullptr) return; : name.clear(); : for (const string& name = stream.get()) { : names.insert(name); : } : return;
|
p***o 发帖数: 1252 | 10 啥时候都应该啊,谁写code都有睡着的时候,指望所有的caller都负责是不现实的。
【在 q****x 的大作中提到】 : 你说了三种检查策略。但这里的问题是该不该检查。
|
|
|
p***o 发帖数: 1252 | 11 That's why you need to use assert. And now you can even use prefast
for static analysis in VC++.
【在 d****i 的大作中提到】 : 我认为不用在callee里面检查,C++里面这种传指针进函数的用的太多了,要是每个都 : 像你这么检查的话overhead太大,code也看着丑陋。应该由caller来检查。
|
b*******s 发帖数: 5216 | 12 这是个相当好的问题
【在 q****x 的大作中提到】 : 如下代码,检查空指针和清空集合的责任应该是caller还是callee?按garbage in : garbage out的原则,这两个语句都可以省掉,caller自己负责。但这样好吗? : 大家倾向哪种风格?理由是? : void GetNames(set* names) { : if (names == nullptr) return; : name.clear(); : for (const string& name = stream.get()) { : names.insert(name); : } : return;
|
k**********g 发帖数: 989 | 13 Try to write code that will eliminate or avoid some of the bugs in the first
place.
For example, if the argument is required (in this case), don't pass in a
pointer. Use a C++ reference.
void GetNames ( set < string > & names)
{
names.clear();
for (...)
{
names.insert(...);
}
}
If use of pointer argument is necessary, then it is necessary to perform
null pointer check in most cases.
The reason is that, in other languages (Java, C#, anything else), a null
pointer will be caught, and will generate a stack trace. In C++, the
consequences are truly undefined.
http://xkcd.com/292/
Only in the case of a C++ class's private method can the null pointer check
be skipped, and only if someone can methodically prove that the null pointer
check cannot happen.
Most C++ code should have logging. Compared to other languages, the design
of C++ logging must take into account the memory corruption issue. This
means C++ logging should either be (1) flushing more frequently, because
memory corruption could destroy the last moment of logs that lead up to the
crash. (2) or, send the log data over a shared memory map to a second
process, which will persist the data.
Anyway, C++ is different, like a surface-to-air missile. |
g*****y 发帖数: 7271 | 14 I do the check even in release build since most of them are super
cheap.
【在 p***o 的大作中提到】 : For performance, use assert. For robustness, use exception. : Ignore it as your code does only if the spec requests that explicitly.
|
q****x 发帖数: 7404 | 15 nullptr check probably is always necessary, as it's critical and cheap.
container clear, on the other hand, is debatable. in this sample code,
actually i think skipping clear is even a bug.
【在 g*****y 的大作中提到】 : I do the check even in release build since most of them are super : cheap.
|