Last active
June 21, 2020 10:38
-
-
Save tajuszk/873eacca2cb51d2e5c40c9e80fcb4351 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 東京在住の1年目エンジニアならログを出す | |
// あまり良くない書き方 | |
// 「ネストが深い」と言われ、複数の条件を意識しながら処理を読み進めていかなければならない | |
function isTokyoKakedashiEnginner (person) { | |
if (person.address === 'Tokyo') { | |
console.log('東京在住です') | |
if (person.job === 'Enginner') { | |
console.log('エンジニアです') | |
if (person.careerYear <= 1) { | |
/* | |
<-----> ← ここの長さをネストという | |
*/ | |
console.log('1年目です') | |
return true | |
} | |
} | |
} | |
return false | |
} | |
// 改善させた書き方 | |
// ブロックごとに考慮するポイントがまとまっていて、条件を満たさないものは早めに処理を抜け、1つ1つの処理を簡潔にしている | |
function isTokyoKakedashiEnginner (person) { | |
// 東京在住じゃなければここで終了 | |
if (person.address !== 'Tokyo') { | |
return false | |
} | |
console.log('東京在住です') | |
// エンジニアじゃなければここで終了 | |
if (person.job !== 'Enginner') { | |
return false | |
} | |
console.log('エンジニアです') | |
// 1年目以下ならログを出す | |
if (person.careerYear > 1) { | |
return false | |
} | |
console.log('1年以下です') | |
// ここまで来たらOK | |
return true | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment