我一直在写if语句,只要我在大学就读我的第一个软件类,最近我的同事已经引入了一种略有不同的方法来处理与出口而不是如果声明包装。我不确定哪种方法更好,希望听到一些意见。嵌套if语句vs函数退出
如何我写的东西(方案1):
function showLikesPopup($th) {
if ($th.find(".liked-times").text() !== "0") {
var $li = $th.parents(".comment-like");
if (!$li.find(".liked").length) {
var comment_id = $th.parents(".ci").data("comment-id");
var application_id = $th.parents(".ci").data("application-id");
var get_likes_url = url_to_current_unique + "/comments_module/get_comment_likes";
var aParams = {comment_id:comment_id,application_id:application_id};
if(!get_likes_request_started) {
get_likes_request_started = true;
ajaxRequest(
get_likes_url,
aParams,
function (response) {
$li.append(response.data.html);
$li.find(".liked").show();
get_likes_request_started = false;
}
);
}
}
$li.find(".liked").show();
}
}
如何我的团队建议这被写入(选项2):
function showLikesPopup($th) {
if ($th.find(".liked-times").text() === "0") {
return;
}
var $li = $th.parents(".comment-like");
if ($li.find(".liked").length) {
$li.find(".liked").show();
return;
}
if (get_likes_request_started) {
return;
}
var comment_id = $th.parents(".ci").data("comment-id");
var application_id = $th.parents(".ci").data("application-id");
var get_likes_url = url_to_current_unique + "/comments_module/get_comment_likes";
var aParams = {comment_id:comment_id,application_id:application_id};
get_likes_request_started = true;
ajaxRequest(
get_likes_url,
aParams,
function (response) {
$li.append(response.data.html);
$li.find(".liked").show();
get_likes_request_started = false;
}
);
}
请投选项1或2?谢谢你们,我知道这两种方式都可以工作,但在我承诺开始使用这种新风格之前,我想确保它是长期的良好实践。
http://softwareengineering.stackexchange.com/questions/18454/should-i-return-from-a-function-early-or-use-an-if-statement – Bergi