函数应该使用:return null;吗? [英] Should a function use: return null;?

查看:182
本文介绍了函数应该使用:return null;吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

函数应该返回null吗?

例如

function test()
{
    return null; // vs return;
}

后者是不好的做法吗?

PS

不好的做法不应该是主观的恕我直言.

Whether it is bad practice shouldn't be subjective IMHO.

推荐答案

如果不返回任何内容,只需使用return;或在函数末尾完全忽略它即可.
如果您的函数通常返回但由于某种原因没有返回,则使用return null;是可行的.

If you don't return anything, just use return; or omit it at all at the end of the function.
If your function is usually returns something but doesn't for some reason, return null; is the way to go.

这类似于您的操作方式,例如在C中:如果您的函数未返回内容,则为void,否则它通常返回有效指针或NULL.

That's similar to how you do it e.g. in C: If your function doesn't return things, it's void, otherwise it often return either a valid pointer or NULL.

这篇关于函数应该使用:return null;吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆