My tips for .NET code-review

My tips for .NET code-review

Code-review is important in our daily development. Let us check out some probable issues in .NET applications. Here are some tips from my recent code-review and I hope it would be helpful for you. Image for post. NullReferenceException series.

Code-review is important in our daily development. Some developers may spend lots of time on learning new features of the languages, DDD, distributed system or some fancy stuff but the first thing we should keep in mind is we need to write the robust, maintainable code. Here are some tips from my recent code-review and I hope it would be helpful for you.

Image for post

NullReferenceException series

The NullReferenceException is really annoying. The best way to avoid it is to check if the variable is null before you use it. Here are some potential issues. I also include other exceptions such as ArgumentNullException here.

Always initialize the collection when you declare it

One common error is sometimes we declared a collection but did not initialize it before using it. Like this:

List<Person> persons;
// Some logics
// If you use persons directly you may got a null object

So the better way is to always initialize the collection when we declare it:

List<Person> persons = new List<Persons>();
// Then you can do something as usual

Especially if we have a property which has its own private field:

// It is good to initialize the private field when we declare it.
private ObservableCollection<Person> _personList = new ObservableCollection<Person>();
public ObservableCollection<Person> PersonList
{
     get => _personList;
     set => SetProperty(ref _personList, value);
}

DO NOT return null values from methods returning collections

Another tip is if we have a method that returns a collection, do not return null. Instead, if there are no satisfied elements, we should return an empty collection.

public List<Person> GetPersons()
{
    // Some logic to retrieve the objects. If not found, then return an empty collection. DO NOT return null.
    return new List<Person>();
}

FirstOrDefault() or First()?

LINQ has some similar methods like First() and FirstOrDefault(). The key difference is the behavior for the empty collection. If the collection is empty or there is no element that satisfies the condition, then First() will throw InvalidOperationException. For this case, FirstOrDefault() is safe because it can return a default value for an empty collection or no element satisfies the condition. But we should be truly clear if the default value is null - that is another reason which would cause NullReferenceException - The default value for reference and nullable types is **`null**`. So it would be better if we do like this:

var firstPerson = persons.FirstOrDefault(x => x.Age > 18);
// Check if the variable is null
if (firstPerson != null)
{
    // Do something to the firstPerson
}

code-review dotnet dot-net-framework dotnet-core

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

jQuery Ajax CRUD in ASP.NET Core MVC with Modal Popup

In this article, we’ll discuss how to use jQuery Ajax for ASP.NET Core MVC CRUD Operations using Bootstrap Modal. With jQuery Ajax, we can make HTTP request to controller action methods without reloading the entire page, like a single page application.

Announcing Entity Framework Core (EF Core) 5 RC2

Entity Framework Core EF Core 5.0 gets closer to a final release. RC2 is now available with bug fixes and a go-live license.

Delete role in asp net core identity framework

LIKE | COMMENT | SHARE | SUBSCRIBE In this video, I will show you How to delete role name by using identity framework. Source code link : https://gitlab.com/...

MVC User Registration & Login with ASP.NET Core Identity

In this article, we will discuss the quickest way to use ASP.NET Core Identity for User Login and Registration in a new or existing MVC application.

Entity Framework Core in ASP.NET Core 3.1

We will learn about entity framework core in details i.e. what, how & why and see how to use it in ASP.NET Core Web API to implement CRUD operations. Though here we will be using entity framework in ASP.NET Core this can be implemented even in .NET Framework for your data persistence.