1

I have this following codes, to help me explain what I have trouble with the decoupling concept.

for example;

async getSomething(id: number): Promise<boolean> {
 if(id === "111") {
  return true;
 }
 else {
  return false;
 }
}

someFunc(): void {
 let id="123";
 if (getSomething(id)) {
  console.log("do somthing special");
 }
 else {
  console.log("sleep");
 }
}

from this answer on other decoupling question

We use interface to decouple two methods, but I have trouble picturing the interaction between the two methods are independent of each other.

For example;

export namespace Special {
 export interface MagicDay {
  isMagic: boolean;
 }
}
...

async getSomething(id: number): Promise<Special.MagicDay> {
 if(id === "111") {
   // here what I think is different from before
   // we create an object which we store our boolean
   // so in some future we can the way we implement this function
   // it is ok because we still have the **isMagic** that **someFunc** needs
  return {isMagic: true};
 }
 else {
  return {isMagic: false};
 }
}

someFunc(): void {
 let id="123";
 let someDay = getSomething(id);
   // different is now
   // we receive an object 
   // so what happens in the backend we don't need to worry about it
   // as long as we have the **isMagic** returns here
   // but what makes this decoupling which we still need the value from 
   // get something
 if (someDay.isMagic) {
  console.log("do somthing special");
 }
 else {
  console.log("sleep");
 }
}

I make comments on what I think inside the code* above where I think I have most trouble.

I read this article about why we should use decoupling and I do understand why, but when it comes to implementation I am troubled by the idea because I keep come to think how do decoupling work in making one independent of the others but we still need the input to make it work?

thanks in advance.

roger
  • 1,225
  • 2
  • 17
  • 33

1 Answers1

0

The points made in this answer don't necessarily apply to TypeScript, because TS uses structural typing. database: Database doesn't have to be an instance of Database to comply to Database type. They aren't tightly coupled already. As long as Database and IDatabase types are same, IDatabase isn't needed.

MagicDay interface doesn't decouple this piece of code. boolean type was just replaced with { isMagic: boolean } type. It doesn't make sense to decouple one method from another one if they belong to same class. It would be possible to decouple unrelated functions:

type TGetSomething = (id: number) => Promise<boolean>;

const getSomething: TGetSomething = async (id: number) => {
 if(id === "111") {
  return true;
 }
 else {
  return false;
 }
}

function async someFunc(callback: TGetSomething): Promise<void> {
 let id="123";
 if (await callback(id)) {
  console.log("do somthing special");
 }
 else {
  console.log("sleep");
 }
}

someFunc(getSomething);

If these functions are actually methods, decoupling should likely be performed at class level. Decoupling is primarily referred as OOP principle in mentioned sources.

Estus Flask
  • 206,104
  • 70
  • 425
  • 565