Using If-Else is one of the programming fundamentals that we learned in college and it is helpful to get us into our first jobs. Interestingly, many senior developers are convinced that If-Else is evil and you should avoid using If-Else in your code ...

Markdown supported · ⌘↵ to post
That's a nice well-written article with good points.
Here's a Software Dev Team Lead's way of doing this: No need to even determine the type. Instead the type implements the correct method for itself & there is never a need to even know the base type. Strategy pattern. 😎🤓
Try the code out and see it run at: stackblitz.com/edit/typescript-xibe8j
interface Animal{
Speak();
}
class Cat implements Animal{
Speak(){
alert("meow");
}
}
class Dog implements Animal{
Speak(){
alert("woof");
}
}
class Lion implements Animal{
Speak(){
alert("roar!");
}
}
let a : Animal = new Dog();
a.Speak(); // woof
let allAnimals = [];
allAnimals.push(new Dog());
allAnimals.push(new Lion());
allAnimals.push(new Cat());
allAnimals.push(new Lion());
allAnimals.forEach( a => a.Speak());
I was reading this article thinking "Man! I've NEVER encountered ANY of these methods in all my years of writing code! I'm mid-level at best and I need to go back to the drawing board and start from scratch."
THEN I realized you're writing .NET.........not JavaScript/TypeScript. 😩🤣
I am a senior programmer and I avoid IFs althougheter by using polymorphism
Your "senior" example violates Open Closed Principle
So, IMHO article is nice but incomplete
Nice article Thai thanks a lot for introducing those different implementations, but it doesn't stand only for if-else statements, what values a developer is his/her ability to communicate and lead in addition to writing a well designed code etc..
Now I know I am a mid-level! 😅
Thanks Thai Tran! It was an interesting read.
p.s. I think you're doing a great job and people would love to follow your work, if you're using any social media (LinkedIn, Twitter) consider adding them to your Hashnode profile. :)
I think if & else makes the job more easier and efficient. The other way around is great though, but looks kind of odd to understand
Kazys Račkauskas
Tdd
The solution isn't entirely my style, but I support it. I actually really love polymorphism, which is what attracted me to OOP.