リファクタリング


グループ化し、原子的に呼び出すことができるコードスニペットを見つけます.

TL;DR: Group your cohesive sentences together



問題点
  • 読みやすさ
  • 複雑さ
  • コード再利用

  • 関連する匂い






















  • ステップ
  • は、別の新しい方法
  • にコード断片を動かします
  • 古いコードを最近作成されたメソッドの呼び出しに置き換えます.

  • サンプルコード

    以前
    object Ingenuity {
        fun moveFollowingPerseverance() {
            //take Off
            raiseTo(10 feet)
    
            //move forward to perseverance
            while (distanceToPerseverance() < 5 feet){
                 moveForward()             
             }
    
            //land
            raiseTo(0 feet)
        }
    

    アフター
    object Ingenuity {   
        //1. Move the code fragment to a separate new method 
        private fun takeOff() {
            raiseTo(10 feet)
        }
    
        //1. Move the code fragment to a separate new method 
        private fun moveForwardToPerseverance() {
           while (distanceToPerseverance() < 5 feet){
                 moveForward()             
             }
        }
    
        //1. Move the code fragment to a separate new method 
        private fun land() {
            raiseTo(0 feet)
        }
    
        fun moveFollowingPerseverance() {
            takeOff()
            //2. Replace the old code with a call to the recently created method.
            moveForwardToPerseverance()
            //2. Replace the old code with a call to the recently created method.
            land()
            //2. Replace the old code with a call to the recently created method.
        }
    }
    

    種類
    自動化
    多くのIDEがこの安全なリファクタリングをサポートします

    制限
    使用する場合はうまくいきません.

    タグ
  • 複雑さ
  • 読みやすさ

  • 関連リファクタリング
  • 新しいクラス
  • へのメソッドの移動

    クレジット
    HreishoからのPixabayによるイメージ
    この記事はリファクタリングシリーズの一部です.