[JAVA] Easy Null Check-I want to give you a chance to write good code. 6 [C # refactoring sample]

Additional validity of easy Null check

Consider the code written when a NullReferenceException occurs in the process of assigning a value to a spread cell. I added the Null check without thinking deeply, but there are a lot of things to think about.

Sample code (before exception occurs)

A code that displays the value obtained from the DB in the spread, and the value may or may not be null. Spreads are third-party products, so we're replacing them with our own class. One day, a NullReferenceException occurred at the point where a value was assigned to a cell.

        public void Main(SpreadCell cell)
        {
            int? dbValue = null;
            //The process of getting the value from the DB is omitted.

            //NullReferenceException occurs.
            cell.Value = dbValue.Value;
            
        }

        /**Spread cell object**/
        private class SpreadCell
        {
            public Object Value { get; set; }
        }
java
    public void main(SpreadCell cell) {
        Integer dbValue = null;
        //The process of getting the value from the DB is omitted.

        //NullPointErexception occurs.
        cell.setValue(dbValue.intValue());
    }

Sample code (code corresponding after an exception occurs)

I'm adding Null checks without thinking, and there's no else. Notice that you don't need a null check above all else.

        public void Main(SpreadCell cell)
        {
            int? dbValue = null;
            //The process of getting the value from the DB is omitted.

            //Assign a value only if it is not Null.
            if (dbValue != null)
            {
                cell.Value = dbValue.Value;
            }          
        }
java
    public void main(SpreadCell cell) {
        Integer dbValue = null;
        //The process of getting the value from the DB is omitted.

        //Assign a value only if it is not Null.
        if (dbValue != null) {
            cell.setValue(dbValue.intValue());
        }
    }

Code after refactoring

There is no problem if you substitute the nullable type as it is. If you take a hundred steps and do a null check, put in the cell initialization code.

        public void Main(SpreadCell cell)
        {
            int? dbValue = null;
            //The process of getting the value from the DB is omitted.

            //You can substitute it as it is.
            cell.Value = dbValue;

            //If you use an if statement, please initialize it.
            cell.Value = null;
            if (dbValue != null)
            {
                cell.Value = dbValue.Value;
            }
        }

java
    public void main(SpreadCell cell) {
        Integer dbValue = null;
        //The process of getting the value from the DB is omitted.

        //You can substitute it as it is.
        cell.setValue(dbValue);

        //If you use an if statement, please initialize it.
        cell.setValue(null);
        if (dbValue != null) {
            cell.setValue(dbValue.intValue());
        }
    }

Summary

You shouldn't add a null check without thinking just because a NullReferenceException has occurred. If you need a null check, don't forget the initialization code.

For those who deal with these obstacles, I'm worried that other codes have been fixed properly.

Previous article (useless processing for collection)

[Next article (Handling of Bool type)] (http://qiita.com/csharpisthebest/items/b0c44764948e1334e94a)

Table of Contents

Recommended Posts

Easy Null Check-I want to give you a chance to write good code. 6 [C # refactoring sample]
A little worrisome code collection-I want to give you a chance to write good code. 2 [C # refactoring sample]
Complicated conditional branching-I want to give you a chance to write good code. 1 [C # refactoring sample]
Bool type handling-I want to give you a chance to write good code. 7 [C # refactoring sample]
Too many function arguments-I want to give you a chance to write good code. 8 [C # refactoring sample]
Wasteful processing of collections-I want to give you a chance to write good code. 5 [C # refactoring sample]
A flowing interface? -I want to give you an opportunity to write good code. 3 [C # refactoring sample]
Is it possible to separate function calls and conditionals? --I want to give you a chance to write good code. 9 [C # refactoring sample]
How to write good code
Let's write a code that is easy to maintain (Part 2) Name
I want to write a nice build.gradle
I want to write a unit test!
I want to simply write a repeating string
Write code that is easy to maintain (Part 4)
Write code that is easy to maintain (Part 3)
7 things I want you to keep so that it doesn't become a fucking code