Skip to content

Instantly share code, notes, and snippets.

@poga
Created January 7, 2017 19:01
Show Gist options
  • Save poga/e52c5eeec4dba6f91a149b4a71047dea to your computer and use it in GitHub Desktop.
Save poga/e52c5eeec4dba6f91a149b4a71047dea to your computer and use it in GitHub Desktop.
// before
// 通常這四個都有個共通的因素,才會被擺在一起
if (rtn == RESULT_A || rtn == RESULT_B || rtn == RESULT_C || rtn == RESULT_D) {
Do1();
} else {
Do2();
}
// after
// 所以就把那個因素寫成 function name
function someReason(rtn) {
return rtn == RESULT_A || rtn == RESULT_B || rtn == RESULT_C || rtn == RESULT_D
}
// 這樣就可以直接理解因素是什麼,而不用看實做細節
if (someReason(rtn)) {
Do1();
} else {
Do2();
}
@ETBlue
Copy link

ETBlue commented Jan 7, 2017

好妙啊(反覆咀嚼

@Superbil
Copy link

Superbil commented Jan 8, 2017

非常推薦這樣幹,算是增加可讀性(重構)的一個技巧

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment