Skip to main content

Command Palette

Search for a command to run...

Code Smell 16 - Ripple Effect

Updated
2 min read
Code Smell 16 - Ripple Effect
M

I’m a senior software engineer loving clean code, and declarative designs. S.O.L.I.D. and agile methodologies fan.

Small changes yield unexpected problems.

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

Problems

  • Coupling

Solutions

  1. Decouple.
  2. Cover with tests.
  3. Refactor and isolate what is changing.
  4. Depend on interfaces.

Examples

  • Legacy Systems

Sample Code

Wrong

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);     
   }  
}

Detection

Tags

  • Legacy

Conclusion

There are multiple strategies to deal with Legacy and coupled systems. We should deal with this problem before it explodes under our eyes.

Relations

More info

Credits

Photo by Jack Tindall on Unsplash


Architecture is the tension between coupling and cohesion.

Neal Ford


This article is part of the CodeSmell Series.

Last update: 2021/06/24

D

Decoupling a clock isn't the best example of it, I suppose, because I didn't quite get it 😁

When I work with database for bot, as example, I plant queries in methods of accompanying Models. Does it somehow introduce a Ripple Effect?

M

which would be the source of time?

How would you test it?

Remember tests must be in full environment control and have no globals involved and 'now()' is a global out of control.

if you couple your model to your database you will indeed have lots of coupling.

Databases are accidental and, thus, unrelated to business models.

Create your bot. Create your chats, create your conversations and test them.

The database comes later on.

1

Code Smells

Part 1 of 50

In this series, we will see several symptoms and situations that make us doubt the quality of our developments. We will present possible solutions. Most are just clues. They are no hard rules.