PHP - 如果出现这种情况,则不执行任何操作
“如果出现这种情况,就什么也不做”这样的说法是否正确?
if ( ($hostNameInfo == $hostNameInput) && ($hostAddressInfo == $hostAddressInput) )
{
return;
}
更新: 我不在函数内部。 :( 所以返回只是废话。
这是更多代码:
//if the input fields are equal to database values, no need to update and waste resources,hence, do nothing:
if ( ($hostNameInfo == $hostNameInput) && ($hostAddressInfo == $hostAddressInput) )
{
//do nothing
}
//If, however, (they are NOT equal and) input fields are not empty:
elseif (!empty($hostNameInput) && (!empty($hostAddressInput)))
{
//do something.
}
提前致谢, MEM
Is this a proper way to say: if something is the case, do nothing?
if ( ($hostNameInfo == $hostNameInput) && ($hostAddressInfo == $hostAddressInput) )
{
return;
}
Update:
I'm not inside a function. :(
So the return is just nonsense.
Here is more code:
//if the input fields are equal to database values, no need to update and waste resources,hence, do nothing:
if ( ($hostNameInfo == $hostNameInput) && ($hostAddressInfo == $hostAddressInput) )
{
//do nothing
}
//If, however, (they are NOT equal and) input fields are not empty:
elseif (!empty($hostNameInput) && (!empty($hostAddressInput)))
{
//do something.
}
Thanks in advance,
MEM
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果不执行任何操作,您只需输入:
For do nothing you simply can type:
也许你应该做相反的事情,如果你的情况没有得到验证,就做一些事情
Maybe you should do the opposite, do something if your condition is not verified
我假设您位于一个函数内部,在这种情况下,它会按照您的预期执行操作,尽管函数内的多个(显然我错了。)return
语句可能导致混乱和缺乏可读性.相反,我更喜欢让所有条件块(我对
if
的{...} 块)包含相关代码,即,在需要额外处理(子流程)时,以总条件计算为
true
的方式编写条件检查:此外,您可以提取条件语句,以提高控制流的可读性和简单性:
更新(基于更新的问题)。考虑一下这个:
ERGO
通过编写您想要满足的条件(而不是您想要忽略的所有异常(主观))来最小化分支率并避免空块。
I assume you're inside a function in which case it does what you expect, although multiple(Apparently I was wrong.)return
statements within a function can lead to confusion and a lack of readability.Instead, I prefer to let all conditional blocks (my description for the code between in the
if
's{...}
block) contain the relevant code, i.e., write the conditional check in such a way that the total condition evaluates totrue
when additional processing (sub-flow) is needed:Furthermore, you can extract the conditional statement in order to improve both readability and simplicity of control flow:
UPDATE (based on updated question). Consider this instead:
ERGO
Minimize branch rate and avoid empty blocks by writing conditions you want to be met, not all the exceptions you want to ignore (subjective).
您在这里谈论的是最佳实践..
最佳实践之一是例程应具有 单一退出点,尽管它广泛 已讨论,由开发人员/风格决定。
更新:
新答案,因为问题已更改:
如果代码只应在某些情况下运行,则没有任何理由添加额外的检查。为了使代码更具可读性,您应该坚持您认为易于维护的任何内容,如下所示(或类似的内容):
You're talking about best practices here..
One of best practice things is that routine shall have single exit point, though it is widely discussed and is up to developer/style.
UPDATE:
New answer, since the question was changed:
Don't see any reason to add additional checks if the code should run only under some circustances. To make the code more readable, you should stuck to whatever you accept as easy-maintainable, like this (or something similar):
有时,本机 do_nothing() 函数会非常好且可读。
为了避免来自语法检查器和语法检查器的压力警报linter,当你有一个空的
if
块时会变得疯狂,我使用:A native do_nothing() function would be very nice and readable sometimes.
To avoid stressing alerts from syntax checkers & linters, that go crazy when you have an empty
if
block, I use:另一种可能性是抛出一个新的异常,您稍后可以在应用程序中捕获该异常。
更新:不在函数内部,这可能是一个坏主意。
The other possibility is to throw a new exception, which you can later catch in your application.
UPDATE: not inside the function this is probably a bad idea.