コード嗅覚16 - リップル効果


小さな変化は予期しない問題をもたらす

TL;DR: If small changes have big impact, you need to decouple your system.


問題


のカップリング



  • 解決策

  • デカッププル.
  • テストで
  • カバー.
  • リファクターとは何が変化して孤立します.
  • はインターフェイスに依存します.


  • 遺産システム
  • サンプルコード


    間違い


    class Time {
       constructor(hour, minute, seconds) {
         this.hour = hour;    
         this.minute = minute;  
         this.seconds = seconds;  
      }
        now(){
          //call operating system
        }  
    }
    
    //Adding a TimeZone will have a big Ripple Effect
    //Changing now() to consider timezine will also bring the effect
    


    class Time {
       constructor(hour, minute, seconds, timezone) {
         this.hour = hour;    
         this.minute = minute;  
         this.seconds = seconds;  
         this.timezone = timezone;  
      }  
      //Removed now() since is invalid without context
    }
    
    class RelativeClock {
       constructor(timezone){
         this.timezone = timezone;
       }
       now(timezone){
         var localSystemTime = this.localSystemTime();
         var localSystemTimezone = this.localSystemTimezone();
         //Do some math translating timezones
         //
         return new Time(..., timezone);     
       }  
    }
    

    検出

  • それが起こる前に問題を見つけるのは簡単ではありません.Mutation Testingsingle points of failuresの根本原因分析は助けるかもしれません.
  • タグ

  • 遺産
  • 結論


    レガシーで結合されたシステムに対処する複数の戦略があります.我々の目の下で爆発する前に我々はこの問題に対処すべきだ.

    関係


  • 詳しい情報


  • クレジット


    Jack TindallUnsplashによる写真

    Architecture is the tension between coupling and cohesion.


    ネールフォード


    この記事はCodesmellシリーズの一部です.


    最終更新日: 2021/06/24