'Stop Using if else if else In Your Code!' | Code Cop #005

preview_player
Показать описание

Hello everybody, I'm Nick, and in this video, I'll show you some really questionable advice around if else statements and ternary statements in C#.

Don't forget to comment, like and subscribe :)

Social Media:

#csharp #dotnet
Рекомендации по теме
Комментарии
Автор

I personally have come to love switch expressions over the past year or so. Nesting ternary operators almost always leads to code that is hard to read.

brianm
Автор

Clean code doesn't equate to short code.

bloggrammer
Автор

Ternaries are nice when you only have the if option and the else option, even for somewhat complex things because multilining a single ternary is still readable. But nested ternaries are just universally bad.

cruzale
Автор

The Art of software development is writing code humans can understand.

bob-xmny
Автор

Best use of ternary is in razor files, or string interpolation. However, once you start nesting, it's much cleaner to create a function

veec
Автор

That measuring tape has me cracking up 🤣

onebike
Автор

Hey Nick, I really appreciate the content you share. Here are my thoughts about this topic:

Opting out of using 'else' or 'else if' in our code is an interesting practice that's quite different from replacing these with ternary operators. While a clean, single ternary operator can sometimes offer a neat solution, I believe that nesting them is universally frowned upon due to the complexity and readability issues it introduces.

In my experience, adhering to the single responsibility principle often eliminates the need for additional conditional branching. This not only simplifies our code but also enhances its readability and maintainability. Good judgment and a clear understanding of the problem at hand can guide us to better coding practices without over-relying on ternary operators or excessive branching.

Thanks for sparking this conversation, Nick. It's through discussions like these that we can share insights and continue to refine our coding techniques.

Looking forward to more of your videos!

ayalamac
Автор

For a single if/else with assignment, I'd probably use the ternary operator. But in this case with else ifs, it's way too bad to read. I completely agree with using the switch expression here. It's the easiest to read of all of those ways and it's much cleaner imo.

AkariTheImmortal
Автор

My headcanon now includes Nick always carrying around a tape measure for code analysis.

jamesterwilliger
Автор

Just want to note that when you use dictionary of strings for mapping purposes then it's nice to specify string comparator for it. E.g. Otherwise you might have issues in some scenarios. People forget this too often

Andrei-cvxt
Автор

I was of this opinion until yesterday while I was migrating a .NET 7 library to .NET Standard 2.0 (which unfortunately doesn't have switch expressions). In this situation, Rider actually did recommend a ternary operator refactor and the key to making it readable is the line breaks. The original example is perfectly fine with correctly placed line breaks:

=
environment is "UAT" ? "UAT" :
environment is "PREPROD" ? "PRP" :
environment is "PROD" ? "PRD" :
string.Empty;

In my opinion, this comes 2nd place in readability to the switch expression. In fact, it is very similar syntactically to the switch expression. If I was working with an older C# language version, I would use this.

DryBones
Автор

Three seconds after seeing the if else if block, I said aloud, “Switch expression….”
That ternary operator stuff just boggles the mind on how someone would prefer that, especially if you need to add more cases to evaluate to it.

stephajn
Автор

Definitely use switch expression when that works. However, when the logic of the set of conditions varies (not just the value being compared) then I have used nested ternaries to good effect. However the rules are, only the else side may contain another ternary and the else ":" is always placed on a new line. Each line then is simply a condition and an outcome, its very readable yet compact. Oh and another rule is that the neither conditions nor the outcome value expressions should do any significant work.

codingbloke
Автор

"it's kinda long" with the measuring tape. I lost it. Beautifully executed.

StianHanger
Автор

I just learned about the switch expression a few days ago, and it's such a good addition to c#.

peterprins
Автор

I'd use the ternary operator in such a way only if it's part of a method call with many parameters, and usually easy to understand. Something like this:
Item item = new Item (a, b, c, d, e, f, g, h, i == true ? 1 : i == false ? 0 : -1, j, k, l, m);
I don't want to use up ~5-10 lines solely for one parameter, especially if it's trivial logic.

brianviktor
Автор

I keep forgetting about the switch expression. It is very nice and concise. Thank you, Nick!

MyUbuntuVids
Автор

Oh wow! I haven't been following C# for a while, so haven't seen that switch expression, but it so nice how much the language design was pushed towards pattern matching for example. I have enjoyed writing C# before, but seems like I would have enjoyed even more now.

LifeLoveAndMonads
Автор

Switch Expression was the first thought when I saw the example. Love the feature.

ilyakurmaz
Автор

Ternary operators seem to be something new programmers latch onto. About 20 years ago, as a senior mentoring a junior, I saw he'd produced a piece of code that had *19* chained ternary calls. We had a conversation very much like this the one in this video. :)

Computers will parse anything you throw at them; write code for humans, not for computers.

Kestrel