171 likes
·
7.5K reads
28 comments
That's a nice well-written article with good points.
Software Dev Team Lead
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
Try the code out and see it run at: https://stackblitz.com/edit/typescript-xibe8j?file=index.ts
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());
Take my 💰 please!
This is good, but you can run into abstraction issues (I am not familiar with Typescript, but on this I am coming from a C++ point of view)... and the article makes such a good way to evolve this if/else with pushing it further to call functions from a map.
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. :)
Starter here.
if you are new to Javascript ..
send me a mail on Jytestanhope@gmail.com or message me on discord with JyteCeo#2852
let's do pair learning ..and task ourselves
Realised I am a junior ...lol 😅😅😅
Hehe. No judgement, I only say it to motivate people to write better code. 😋
...and a Lead developer would create a data structure.
This is totally on point.
Excellent read, thank you Thai Tran
Time to level up!
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
Hi Maxi Contieri. Thank you for reading and commenting on my blog post. As always, I'm interested in hearing about SOLID principles. Would you please mind sharing with us more on that so that we could all learn from you?
Hi Thai Tran
Yes of course!
This article brings your senior solution:
This one talks about the problem with IFs
And this one shows how to use polymorphism instead of IFs
Thanks again for your article. It started a good conversation
Hi Maxi Contieri . Thanks very much for sharing it. This will be very helpful for my next posts.
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. 😩🤣
The post is very informative, I learn a lot from it. Thank you, Thai Tran.
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..
With the examples given, it's safe to say that I'm still a junior developer 😃
Thanks for reading ❤️
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
In my opinion, it is ok to use If-Else for some simple instances but in a more complicated use case, you should not avoid using it. Especially if you are not the one who writes the code, it will take longer for you to evaluate all conditions to assert the result.
Thai Tran Very true, to avoid repetitions until the condition is valid.
Thanks Thai
Wow, nice tricks for senior dev level 👏
Pretty neat and yes thats true for a senior developer
even if i am senior i will go with switch, "easy is best"
Interesting write up Thai Tran 💪