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.