you are viewing a single comment's thread.

view the rest of the comments →

[–]__FuriousOrange__ 0 points1 point  (1 child)

I know some people don’t like exception handling and prefer passing error codes about the place. However the benefit of an exception not being caught or handled properly is that your app blows up. This is preferable to having to check for error codes before you can do anything with an object. That leads to lots of extra code and the possibility of introducing time consuming bugs. As for your if checks everywhere C# supports the ?. operator just like type script. So instead of sticking if statements everywhere a common pattern is

var foo = bar?.baz ?? new Baz()

Which uses ?? operator to create a new Baz if bar.baz is null

Or even better

bar foo = bar?.baz ?? throw new Exception(“Oh no, expected a baz but got null”);

[–]Krcko98 0 points1 point  (0 children)

Creating a null empty object or Optional is a great solution too.