Interesting write up Thai Tran 💪
even if i am senior i will go with switch, "easy is best"
Pretty neat and yes thats true for a senior developer
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());
The post is very informative, I learn a lot from it. Thank you, Thai Tran.
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..
Excellent read, thank you Thai Tran
Time to level up!
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
Realised I am a junior ...lol 😅😅😅
With the examples given, it's safe to say that I'm still a junior developer 😃
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.