The subtle art of refactoring #1
Tags: codequality, csharp, dotnet, refactorit
Published: 2019-12-27 10:10:35

Originally posted in [Medium](https://medium.com/@hharan618/the-subtle-art-of-refactoring-1-78e73a6b238e)

Every developer would have come through the below scenario frequently while writing business logic. The examples here are written is from C, but it applies to any language that you use.

Let’s imagine you query something from the database and you need to find the studentId of the first one or maybe filter the student using a name

    var students = _studentRepository.GetAllStudents();

    var studentId = students.FirstOrDefault().StudentId;

or

    var studentName = students.Select(student=> student.Name.Equals(“John”,StringComparison.OrdinalIgnoreCase);

Do you notice any issue in the above code? No? Even I was not aware that there is a `PosibleNullReference` here

![Warning](https://cdn-images-1.medium.com/max/2000/1*BykPkBM7N0QXChT7t7E2hw.png)

***FirstOrDefault** returns the default value of a type if no item matches the predicate. For reference types that is **null**. That is the reason for this exception.*

![Photo by [Max Nelson](https://unsplash.com/@maxcodes?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText) on [Unsplash](https://unsplash.com/s/photos/c%23?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText)](https://cdn-images-1.medium.com/max/11396/1*g8-DazB3PPaM6fCCFAfJgg.jpeg)*Photo by [Max Nelson](https://unsplash.com/@maxcodes?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText) on [Unsplash](https://unsplash.com/s/photos/c%23?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText)*

## How do we resolve this?

Instead of directly finding the name using FirstOrDefault(), you can change it like this and the **NullReference** warning is gone.

![resolbed](https://cdn-images-1.medium.com/max/2000/1*cl3H7phCQmz9V4rcxZ7oWA.png)

### Pro Tips for readable code

Imagine you find something from the database or get something from an API, you need to check if its **Null / NotNull / Empty / EmptyOrNotNull** etc..

We were doing this earlier but most often we forget to check this before proceeding with the business logic and the ton of `if(something!=null)` loops get piled up.

### Resolution

We wrote an extension method like this

    public static bool IsNotNull(this object obj){

    return obj != null;
    }

So whenever we do an api call or retrieve something from the database, all we need to do is

    var students = _studentRepository.GetAllStudents();

    if(students.IsNotNull()){

    //do something

    }

It is more readable as well as it will be unforgettable in the future since our minds will automatically remember the small things that are readable.

The power of the extension method is it should be generic and not tied to one particular project or area. Write it in a common area so that it is shareable across projects and benefits you.

### Conclusion

Write more extension methods for handling small business logic like checking for `empty/null/` trimming the string etc. It makes the code base more readable and maintainable.

Thanks for reading. Stay tuned for more blogs.