if-> return if if-> else效率 [英] if->return vs. if->else efficiency

查看:120
本文介绍了if-> return if if-> else效率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能听起来像一个愚蠢的问题,我犹豫要发布它,但仍然:
如果某些东西只需要在某种条件下运行,哪一种更有效:

This may sound like a silly question, and I hesitated to post it, but still: if something needs to run only in a certain condition, which of these is more efficient:

A。

if (condition) {
   // do
   // things...
}

B。

if (!condition) { return; }
// do
// things...


推荐答案

它们同样有效,但 B 通常被认为具有更好的可读性,尤其是在用于消除多个嵌套条件时。

They are equally efficient, but B is usually considered to give better readability, especially when used to eliminate several nested conditions.

这篇关于if-> return if if-> else效率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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