Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is “If” condition better than ?? and casting

I have following two approaches for same functionality - one with "if” condition and one with "?? and casting". Which approach is better? Why?

Code:

  Int16? reportID2 = null;
  //Other code

  //Approach 1
  if (reportID2 == null)
  {
       command.Parameters.AddWithValue("@report_type_code", DBNull.Value);
  }
  else
  {
     command.Parameters.AddWithValue("@report_type_code", reportID2);
  }

  //Approach 2
  command.Parameters.AddWithValue("@report_type_code", ((object) reportID2) ?? DBNull.Value);

UPDATE

Based on the answers, following are the benefits of ??

  1. Increased readability
  2. Decreased branching deapth of program flow (reduced cyclomatic complexity)

Note: Cost of casting as object is negligible.

REFERENCE

  1. Null-Coallescing Operator - Why Casting?
like image 293
LCJ Avatar asked Dec 04 '12 08:12

LCJ


People also ask

Is it better to use switch or if-else?

A switch statement is significantly faster than an if-else ladder if there are many nested if-else's involved. This is due to the creation of a jump table for switch during compilation. As a result, instead of checking which case is satisfied throughout execution, it just decides which case must be completed.

Which is faster if or if-else?

In general, "else if" style can be faster because in the series of ifs, every condition is checked one after the other; in an "else if" chain, once one condition is matched, the rest are bypassed.

Is switch statement more efficient than if?

A switch statement is usually more efficient than a set of nested ifs. Deciding whether to use if-then-else statements or a switch statement is based on readability and the expression that the statement is testing.

What are the differences between IF condition and switch?

Here, each case will be executed one after the other. In if-else, the values are based on conditions. In the switch case, the values are based on user preference. In case, the situation gets false in the if statement, it will automatically execute the else statement.


2 Answers

The null coalescing operator (??) is a better approach because it does the same thing as your initial block but in a single, easy to read line. This makes the code more readable and more maintainable.

This is one of many examples of syntactic sugar, that is to say code statements which are "shortcuts" for representing a commonly-used idea.i++ is another example of this, as it replaces i = i + 1. It is cleaner and simpler, just like ??.

like image 132
Levi Botelho Avatar answered Oct 02 '22 18:10

Levi Botelho


I always use null-coalescing operator in such cases:

command.Parameters.AddWithValue("@name", value ?? DBNull.Value);

command.ExecuteScalar() as int? ?? -1;

etc.

It increases code readability, decreases branching depth. Also was created especially for database-related scenarios such as ADO.NET.

like image 33
abatishchev Avatar answered Oct 02 '22 20:10

abatishchev