---
title: "Discussion on \"How to code like a pro in 2022 and avoid If-Else\" | Hashnode"
description: "Discussion on \"How to code like a pro in 2022 and avoid If-Else\". 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 ..."
url: "https://hashnode.com/posts/how-to-code-like-a-pro-in-2022-and-avoid-if-else/62aab3692b2b09182b654c62"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [How to code like a pro in 2022 and avoid If-Else](https://thaitran.dev/how-to-code-like-a-pro-in-2022-and-avoid-if-else?utm_source=hashnode&utm_medium=feed)

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

Jun 16, 2022 · 3 min read · thaitran.dev  
Read the article

-   [#dotnet](https://hashnode.com/tag/dotnet)
-   [#best-practices](https://hashnode.com/tag/best-practices)
-   [#programming](https://hashnode.com/tag/programming)
-   [#if-else](https://hashnode.com/tag/if-else)
-   [#clean-code](https://hashnode.com/tag/clean-code)

## Written by

[Thai Tran](https://hashnode.com/@thaitran)

@thaitran · Melbourne, Australia

  
[View profile](https://hashnode.com/@thaitran)

Software Developer

## Responses 19

Markdown supported · ⌘↵ to post

[Kazys Račkauskas](https://hashnode.com/@EasyTdd)

[Aug 22, 2024](https://hashnode.com/posts/how-to-code-like-a-pro-in-2022-and-avoid-if-else/62aab3692b2b09182b654c62/comment/66c79a9cf92670324fcdf83c)

The solution isn't entirely my style, but I support it. I actually really love polymorphism, which is what attracted me to OOP.

[Developer Avocado](https://hashnode.com/@AvocaDEV)

[Jul 6, 2022](https://hashnode.com/posts/how-to-code-like-a-pro-in-2022-and-avoid-if-else/62aab3692b2b09182b654c62/comment/62c5e2a7c8efe473b8fe4ab1)

Interesting write up [Thai Tran](https://hashnode.com/@thaitran) 💪

[Moiz Alnoor](https://hashnode.com/@moiz-)

[Jul 2, 2022](https://hashnode.com/posts/how-to-code-like-a-pro-in-2022-and-avoid-if-else/62aab3692b2b09182b654c62/comment/62c02d61ccecfb25bbc73061)

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

[Humphrey Pietersen](https://hashnode.com/@humphreyscorner)

[Jun 30, 2022](https://hashnode.com/posts/how-to-code-like-a-pro-in-2022-and-avoid-if-else/62aab3692b2b09182b654c62/comment/62bd48dade0d0a065f4f7851)

Pretty neat and yes thats true for a senior developer

[Ibrahim Nergiz](https://hashnode.com/@nergiz)

[Jun 25, 2022](https://hashnode.com/posts/how-to-code-like-a-pro-in-2022-and-avoid-if-else/62aab3692b2b09182b654c62/comment/62b77bee92447c9aed5b8861)

Wow, nice tricks for senior dev level 👏

[raddevus](https://hashnode.com/@raddevus)

[Jun 24, 2022](https://hashnode.com/posts/how-to-code-like-a-pro-in-2022-and-avoid-if-else/62aab3692b2b09182b654c62/comment/62b601d392447c9aed5b5ddb)

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: [stackblitz.com/edit/typescript-xibe8j](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());
```

[Thai Tran](https://hashnode.com/@thaitran)

[Jun 24, 2022](https://hashnode.com/posts/how-to-code-like-a-pro-in-2022-and-avoid-if-else/62aab3692b2b09182b654c62/comment/62b601d392447c9aed5b5ddb#reply-62b630f892447c9aed5b6259)

Take my 💰 please!

[Alessandro](https://hashnode.com/@anger)

[Jul 2, 2022](https://hashnode.com/posts/how-to-code-like-a-pro-in-2022-and-avoid-if-else/62aab3692b2b09182b654c62/comment/62b601d392447c9aed5b5ddb#reply-62c06ca6ccecfb25bbc73a75)

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.

[Tony Tin Nguyen](https://hashnode.com/@tonytinnguyen)

[Jun 24, 2022](https://hashnode.com/posts/how-to-code-like-a-pro-in-2022-and-avoid-if-else/62aab3692b2b09182b654c62/comment/62b5cdc1b86f939ac81b3543)

The post is very informative, I learn a lot from it. Thank you, [Thai Tran](https://hashnode.com/@thaitran).

[Anh](https://hashnode.com/@jimmyz)

[Jun 24, 2022](https://hashnode.com/posts/how-to-code-like-a-pro-in-2022-and-avoid-if-else/62aab3692b2b09182b654c62/comment/62b5bfdc92447c9aed5b52f8)

Thanks Thai

[Douglas Rogers](https://hashnode.com/@standingdreams)

[Jun 21, 2022](https://hashnode.com/posts/how-to-code-like-a-pro-in-2022-and-avoid-if-else/62aab3692b2b09182b654c62/comment/62b134b2c0f84f6b540d256a)

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

[Maxi Contieri](https://hashnode.com/@mcsee)

[Jun 20, 2022](https://hashnode.com/posts/how-to-code-like-a-pro-in-2022-and-avoid-if-else/62aab3692b2b09182b654c62/comment/62afc2eed8afe74aa51fe397)

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

[Thai Tran](https://hashnode.com/@thaitran)

[Jun 20, 2022](https://hashnode.com/posts/how-to-code-like-a-pro-in-2022-and-avoid-if-else/62aab3692b2b09182b654c62/comment/62afc2eed8afe74aa51fe397#reply-62afc9f40924134a8048af80)

Hi [Maxi Contieri](https://hashnode.com/@mcsee). 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?

[Maxi Contieri](https://hashnode.com/@mcsee)

[Jun 20, 2022](https://hashnode.com/posts/how-to-code-like-a-pro-in-2022-and-avoid-if-else/62aab3692b2b09182b654c62/comment/62afc2eed8afe74aa51fe397#reply-62b06df2c0f84f6b540d0f2d)

Hi [Thai Tran](https://hashnode.com/@thaitran)

Yes of course!

This article brings your senior solution:

[maximilianocontieri.com/code-smell-133-hardcoded-…](https://maximilianocontieri.com/code-smell-133-hardcoded-if-conditions)

This one talks about the problem with IFs

[maximilianocontieri.com/code-smell-36-switchcasee…](https://maximilianocontieri.com/code-smell-36-switchcaseelseifelseif-statements)

And this one shows how to use polymorphism instead of IFs

[maximilianocontieri.com/how-to-get-rid-of-annoyin…](https://maximilianocontieri.com/how-to-get-rid-of-annoying-ifs-forever)

Thanks again for your article. It started a good conversation

[Thai Tran](https://hashnode.com/@thaitran)

[Jun 21, 2022](https://hashnode.com/posts/how-to-code-like-a-pro-in-2022-and-avoid-if-else/62aab3692b2b09182b654c62/comment/62afc2eed8afe74aa51fe397#reply-62b1304cc0f84f6b540d2504)

Hi [Maxi Contieri](https://hashnode.com/@mcsee) . Thanks very much for sharing it. This will be very helpful for my next posts.

[Mohamad Cheaito](https://hashnode.com/@HashSlash)

[Jun 19, 2022](https://hashnode.com/posts/how-to-code-like-a-pro-in-2022-and-avoid-if-else/62aab3692b2b09182b654c62/comment/62aec6f4d8afe74aa51fc360)

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

[Han O'Connor](https://hashnode.com/@hanoconnor)

[Jun 18, 2022](https://hashnode.com/posts/how-to-code-like-a-pro-in-2022-and-avoid-if-else/62aab3692b2b09182b654c62/comment/62ad834dd0fe4244bb693823)

Excellent read, thank you [Thai Tran](https://hashnode.com/@thaitran)

Time to level up!

[radu2(●\_\_●)](https://hashnode.com/@SeidHamid)

[Jun 17, 2022](https://hashnode.com/posts/how-to-code-like-a-pro-in-2022-and-avoid-if-else/62aab3692b2b09182b654c62/comment/62ace6cd42d95b3dafbf214f)

Starter here.

[Stanhope Jite](https://hashnode.com/@JyteDev)

[Jun 24, 2022](https://hashnode.com/posts/how-to-code-like-a-pro-in-2022-and-avoid-if-else/62aab3692b2b09182b654c62/comment/62ace6cd42d95b3dafbf214f#reply-62b63e9b92447c9aed5b6388)

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

[Eleftheria Batsou](https://hashnode.com/@eleftheriabatsou)

[Jun 17, 2022](https://hashnode.com/posts/how-to-code-like-a-pro-in-2022-and-avoid-if-else/62aab3692b2b09182b654c62/comment/62ace5d66528393d8aa52345)

Now I know I am a mid-level! 😅

Thanks [Thai Tran](https://hashnode.com/@thaitran)! 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. :)

[Nick Hodges](https://hashnode.com/@NickHodges0702)

[Jun 17, 2022](https://hashnode.com/posts/how-to-code-like-a-pro-in-2022-and-avoid-if-else/62aab3692b2b09182b654c62/comment/62acdf8be4b2413be7a65f55)

This is totally on point.

[Zeeshan S](https://hashnode.com/@zshn)

[Jun 17, 2022](https://hashnode.com/posts/how-to-code-like-a-pro-in-2022-and-avoid-if-else/62aab3692b2b09182b654c62/comment/62acdd96e4b2413be7a65f23)

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

[Um Zaharradeen](https://hashnode.com/@deeni)

[Jun 17, 2022](https://hashnode.com/posts/how-to-code-like-a-pro-in-2022-and-avoid-if-else/62aab3692b2b09182b654c62/comment/62acd75fa10f123c0cf6e464)

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

[Thai Tran](https://hashnode.com/@thaitran)

[Jun 17, 2022](https://hashnode.com/posts/how-to-code-like-a-pro-in-2022-and-avoid-if-else/62aab3692b2b09182b654c62/comment/62acd75fa10f123c0cf6e464#reply-62ad10933d2cd8400b1bda20)

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](https://hashnode.com/@deeni)

[Jun 18, 2022](https://hashnode.com/posts/how-to-code-like-a-pro-in-2022-and-avoid-if-else/62aab3692b2b09182b654c62/comment/62acd75fa10f123c0cf6e464#reply-62ae067ad8afe74aa51fb3c0)

[Thai Tran](https://hashnode.com/@thaitran) Very true, to avoid repetitions until the condition is valid.

[Brian Wachira](https://hashnode.com/@techbytes)

[Jun 16, 2022](https://hashnode.com/posts/how-to-code-like-a-pro-in-2022-and-avoid-if-else/62aab3692b2b09182b654c62/comment/62ab5a742b2b09182b6567bc)

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

[Thai Tran](https://hashnode.com/@thaitran)

[Jun 17, 2022](https://hashnode.com/posts/how-to-code-like-a-pro-in-2022-and-avoid-if-else/62aab3692b2b09182b654c62/comment/62ab5a742b2b09182b6567bc#reply-62ad118e8ebaba4030907c7f)

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

[Ogbolo Joshua](https://hashnode.com/@webothdo)

[Jun 16, 2022](https://hashnode.com/posts/how-to-code-like-a-pro-in-2022-and-avoid-if-else/62aab3692b2b09182b654c62/comment/62aaf9262b2b09182b65579d)

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

[Thai Tran](https://hashnode.com/@thaitran)

[Jun 17, 2022](https://hashnode.com/posts/how-to-code-like-a-pro-in-2022-and-avoid-if-else/62aab3692b2b09182b654c62/comment/62aaf9262b2b09182b65579d#reply-62ad11bf8ebaba4030907c84)

Thanks for reading ❤️
