Sign in
Log inSign up
Thai Tran

171 likes

·

7.5K reads

28 comments

raddevus
raddevus
Jun 24, 2022

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());
9
·
·2 replies
Thai Tran
Thai Tran
Author
·Jun 24, 2022

Take my 💰 please!

1
·
Alessandro
Alessandro
Jul 2, 2022

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.

·
Eleftheria Batsou
Eleftheria Batsou
Jun 17, 2022

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. :)

8
·
radu2(●__●)
radu2(●__●)
Jun 17, 2022

Starter here.

5
·
·1 reply
Stanhope Jite
Stanhope Jite
Jun 24, 2022

if you are new to Javascript .. send me a mail on or message me on discord with JyteCeo#2852 let's do pair learning ..and task ourselves

·
Brian Wachira
Brian Wachira
Jun 16, 2022

Realised I am a junior ...lol 😅😅😅

3
·
·1 reply
Thai Tran
Thai Tran
Author
·Jun 17, 2022

Hehe. No judgement, I only say it to motivate people to write better code. 😋

1
·
Zeeshan S
Zeeshan S
Jun 17, 2022

...and a Lead developer would create a data structure.

2
·
Nick Hodges
Nick Hodges
Jun 17, 2022

This is totally on point.

2
·
Han O'Connor
Han O'Connor
Jun 18, 2022

Excellent read, thank you Thai Tran

Time to level up!

2
·
Maxi Contieri⭐⭐⭐
Maxi Contieri⭐⭐⭐
Jun 20, 2022

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

2
·
·3 replies
Thai Tran
Thai Tran
Author
·Jun 20, 2022

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?

1
·
Maxi Contieri⭐⭐⭐
Maxi Contieri⭐⭐⭐
Jun 20, 2022

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

3
·
Thai Tran
Thai Tran
Author
·Jun 21, 2022

Hi Maxi Contieri . Thanks very much for sharing it. This will be very helpful for my next posts.

1
·
Douglas Rogers
Douglas Rogers
Jun 21, 2022

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. 😩🤣

2
·
Tony Tin Nguyen
Tony Tin Nguyen
Jun 24, 2022

The post is very informative, I learn a lot from it. Thank you, Thai Tran.

2
·
Mohamad Cheaito
Mohamad Cheaito
Jun 19, 2022

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..

1
·
Ogbolo Joshua
Ogbolo Joshua
Jun 16, 2022

With the examples given, it's safe to say that I'm still a junior developer 😃

·
·1 reply
Thai Tran
Thai Tran
Author
·Jun 17, 2022

Thanks for reading ❤️

·
Um Zaharradeen
Um Zaharradeen
Jun 17, 2022

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

·
·2 replies
Thai Tran
Thai Tran
Author
·Jun 17, 2022

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.

·
Um Zaharradeen
Um Zaharradeen
Jun 18, 2022

Thai Tran Very true, to avoid repetitions until the condition is valid.

·
Anh
Anh
Jun 24, 2022

Thanks Thai

·
Ibrahim Nergiz
Ibrahim Nergiz
Jun 25, 2022

Wow, nice tricks for senior dev level 👏

·
Humphrey Pietersen
Humphrey Pietersen
Jun 30, 2022

Pretty neat and yes thats true for a senior developer

·
Moiz Alnoor
Moiz Alnoor
Jul 2, 2022

even if i am senior i will go with switch, "easy is best"

·
Developer Avocado
Developer Avocado
Jul 6, 2022

Interesting write up Thai Tran 💪

·