-1

How can I check for null without using if statement, in C#?

public class Bar {
 public void foo() {

  var discount = _discountService.GetDiscountById(discountId);
  //this is anti pattern . is there any other way ?
  if (discount == null)
      throw new ArgumentException("Discount could not be loaded");

  //main logic of methods continues here 
 }
}
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Arash
  • 3,458
  • 7
  • 32
  • 50
  • 2
    Who says conditional operations are _"code smell[s]"_? The only time it's a problem is for GPU shaders and then it's for _performance_ reasons not _cosmetic_ –  Oct 09 '18 at 05:29
  • it is code smell because you have to write test for each condition , so managing test would be hard . – Arash Oct 09 '18 at 05:35
  • 2
    @Arash Anytime you want to do something different depending on a condition (in this case, whether something is null), you _will_ have branches. If you want to eliminate any "conditionals" then your code must do "one thing". – Sweeper Oct 09 '18 at 05:53
  • After reading [this post](https://stackoverflow.com/q/1554180/585968) again, I'm not sure whether checking for null constitutes a code smell. You aren't really performing a branch based on the `type` but rather that an object has _valid state_. You can't very well proceed if inputs aren't valid. What's probably more interesting is why `GetDiscountById` can return `null` in the first place? Simply return `decimal 0` –  Oct 09 '18 at 06:44
  • and what is that negative vote for ? – Arash Oct 09 '18 at 07:56
  • @Arash wasnt me. Who's down-voting all the answers? –  Oct 09 '18 at 08:07
  • I'm voting to close this question as off-topic because this question belongs on Software Engineering. – NetMage Oct 09 '18 at 22:04

3 Answers3

1

Branching is not always a code smell. It usually considered a code smell when you are branching over types, which could be solved with polymorphism.

Here you could consider what the behavior should be of GetDiscountById when the discount is not found. If you have that method throw instead at null, you only have to test the id not found case in the discount service class and not everywhere where you call it, because you can always expect it to return succesfully or throw.

stephanV
  • 183
  • 6
-1

What about ?? Operator ? Yes, behind the scenes it's look similar, but in fact there is no any "if" statements.

Example of using:

var discount = _discountService.GetDiscountById(discountId) ?? throw new ArgumentException("Discount could not be loaded");
Stanislav Balia
  • 385
  • 1
  • 12
-1

You can do it by using ??(null-coalescing operator ) operator. Replace below code :

if (discount == null)
   throw new ArgumentException("Discount could not be loaded");

With

discount = discount??throw new ArgumentException("Discount could not be loaded");

Or you can use try catch block.

try
{
//main logic of methods continues here 
}    
catch(ArgumentNullException ex)
{
// handle exception
}
catch(Exception ex)
{
// handle exception
}
ksdev
  • 56
  • 7