🛰️

リファクタリング?if文って美味しいの?

に公開

if文のリファクタリングあるある

本記事では、TypeScriptで書きます。

1. if文の中にif文を書いてすぐreturn

before リファクタリング

// その1 値比較
if ( a === 1 ) {
    if ( b === 2 )
        return true
}

// その2 真偽比較
if ( result === "成功" ) {
    if ( this.navigateToProfilePage( this.profile.name ) )
        return
}
after リファクタリング
// その1 値比較
if ( a === 1 && b === 2 ) { return true }

// その2 真偽比較
if ( result === "成功" && this.navigateToProfilePage( this.profile.name ) ) {
    return
}

最後に

まとめるタイミングで本記事にリファクタあるあるを増やして行きます

Discussion