Do not returns null

If you create a method that return a list and you don’t have data, always return empty list instead of null. This would allow the caller to iterate the list without checking. As an example, consider the 2 blocks of code below, which is better?

var customers = GetCustomers();
foreach(var customer in customers) {
  // do something
}
var customers = GetCustomers();
if (customers != null) {
  foreach(var customer in customers) {
    // do something
  }
}

Having said that, if you write caller side code and received a list. Make sure you don’t get NullReferenceException by using this code.

var customers = GetCustomers();
foreach(var customer in customers ?? Array.Empty<Customer>()) {
  // do something
}

C# Exception Handler

Given the code below, how would you add exception handling in your code?

public void ButtonClicked() {
   DoSomething();
   DoTask2();
}

private void DoSomething() {
  DoSomethingElse();
}

private void DoSomethingElse() {
  // code
}

private void DoTask2() {
  // code
}

Some developer would handle like this

public void ButtonClicked() {
  try {
    DoSomething();
    DoTask2();
  }
  catch(Exception ex) {
    logger.LogError(ex, "Error in buttonclicked");
  }
}

private void DoSomething() {
  DoSomethingElse();
}

private void DoSomethingElse() {
  // code
}

private void DoTask2() {
  // code
}

While other do something like this

public void ButtonClicked() {
   var result = DoSomething();
   if (result.Success) {
     var task2Result = DoTask2();
     if (!task2Result.Success) {
       logger.logError(result.Error);
     }
   }
   else {
     logger.logError(result.Error);
   }
}

private Result DoSomething() {
  try {
    DoSomethingElse();
  }
  catch(Exception ex) {
    return new Result { Success = false, Error = ex.Message }
  }
}

private result DoSomethingElse() {
  try {
    // code
  }
  catch(Exception ex) {
    return new Result { Success = false, Error = ex.Message }
  }
}

private void DoTask2() {
  try {
    // code
  }
  catch(Exception ex) {
    return new Result { Success = false, Error = ex.Message }
  }
}

public class Result 
{
   public bool Success { get; set; }
   public string Error { get; set; }
}

Which style is more readable?

Startup configure method

Did you know that you can inject any object in the Configure method of your startup class? For example if you want to inject ICustomer.

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddScoped<ICustomer,Customer>();
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ICustomer customer)
    {
        // use customer here
    }
}

Date and time arithmetics

One of our developer wrote this code

private DateTimeOffset CombineAppointmentDateTime(string date, string time)
{
    time = time == "NA" ? "00:00" : (time == "9:30" ? "09:30" : time);
    var dateTime = DateTime.ParseExact($"{date} {time}", 
                       Constants.DateTimeFormat, CultureInfo.InvariantCulture);
    return DateTime.SpecifyKind(dateTime, DateTimeKind.Local);
}

var result = CombineAppointmentDateTime(
    DateTime.Now.ToString("yyyy-MM-dd"), "13:00");

He is combining the date and time but in a clumsy way. What he’s doing is composing a string then convert it to DateTime. A lot of junior developers do this way for some reasons.

What a lot of developers tend to forget is that using date and time arithmetics is much more effiencient and readable.

The code below is much better.

private DateTimeOffset CombineAppointmentDateTime(DateTime date, string time)
{
    return TimeSpan.TryParse(time, out TimeSpan val)
           ? date.Date + val
           : date.Date;
}

var result = CombineAppointmentDateTime(DateTime.Now,"13:00");