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

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

问题描述

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

答.

if (条件) {//做//事物...}

B.

if (!condition) { return;}//做//事物...

解决方案

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

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...

解决方案

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

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

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