函数应该使用: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.