Minimize Impact on Other Components of a System

103 阅读1分钟

When taking over a system which was developed by other developers, there is some needs to modify the system or add new functionalities to the system, we can hold the Minimize Impact principle to avoid introducing potential bugs to the working system.
This is an experience that has worked effectively with my daily development. But one day, I forgot this principle and add a method named multi to a class that inherited the Redis class, then errors occured. Here is the code snippet,

class MyRedis extends Redis {
      public function multi() {
           try {
               $ret = parent::multi();
           } catch(RedisException $e) {
               log::Error(...);
               return false;
           }
           return true;
}           
         

After adding the method named multi to class MyRedis, something error occured, why?
Because other components of the system has called method named multi before adding the new method,

$redis = new MyRedis();
$pipe = $redis->multi(Redis::PIPELINE);
$pipe...

$redis->multi() in fact calls the multi method of Redis class, it returns a redis instance!
So to fix this, there are two solutions I can think of,

  1. rename the newly added multi to multi2 to avoid overwrite the parent's method
public function multi2() {
...
}
  1. return type of the newly added multi method holds the same with that of the parent's method.

No matter which solution, the ultimate principle is Minimize the impact on existed components of the System.