如何统计是否迟到java,更好的Java方法语法?早退还是迟到?

Often times you might have a method that checks numerous conditions and returns a status (lets say boolean for now). Is it better to define a flag, set it during the method, and return it at the end :

boolean validate(DomainObject o) {

boolean valid = false;

if (o.property == x) {

valid = true;

} else if (o.property2 == y) {

valid = true;

} ...

return valid;

}

or is it better/more correct to simply return once you know the method's outcome?

boolean validate(DomainObject o) {

if (o.property == x) {

return true;

} else if (o.property2 == y) {

return true;

} ...

return false;

}

Now obviously there could be try/catch blocks and all other kinds of conditions, but I think the concept is clear. Opinions?

解决方案

If it's a method you'll be calling thousands of times, then early return is better to achieve a [slightly] increased performance.

If not, then I'd prefer late return, since it improves readability.

Remember programmers usually spend more time reading than writing code, so anything you can do to improve readability will be certainly welcome.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值