Programming Tips: Avoid NULL values

One way to make your code simpler and avoid potential bugs is by avoiding NULL values when possible. Usually you won’t have a significant performance overhead doing this.  By not returning null values you eliminate null pointer exceptions and also you simplify your code by eliminating the null checks.

Example 1

Simplify your code by never to returning a null list. If you return null lists your code will look like this:

...
List<Student> getStudentsByName(String name) {
  if (name == null || name.equals("")) {
    return null;
  }

  List<Student> studentList = new ArrayList<Student>();
  ...
  // Code that fills the list.
  ...
  return studentList;
}
...

List<Student> studentList = getStudentsByName("Armin");
// Must check for null list.
if (studentList != null) {
   for (int i = 0; i < studentList.size(); ++i) {
     // Do something
   }
}
...

Without nulls, the code is simpler:

...
List<Student> getStudentsByName(String name) {
  List<Student> studentList = new ArrayList<Student>();
  if (name != null && !name.equals("")) {
    // Code that fills the list.
  }
  return studentList;
}
...

List<Student> studentList = getStudentsByName("Armin");
for (int i = 0; i < studentList.size(); i++) {
  // Do something
}
...

Example 2

If you have a logger, that can be null, you must check for null logger every time you log something (yeachh):

Logger getLogger(BuildType buildType) {
   if (buildType == BuildType.Production) {
     return null;
   } else {
     return new FileLogger("debug.log");
   }
}

...
Logger logger = getLogger(type);
...
if (logger != null) {
   logger.log("A message");
}
...
if (logger != null) {
   logger.log("message");
}
...

It’s better to return a NullLogger instance that will consume all the messages and will do nothing.

Logger getLogger(BuildType buildType) {
   if (buildType == BuildType.Production) {
     return new NullLogger();
   } else {
     return new FileLogger("debug.log");
   }
}

...
Logger logger = getLogger(type);
...
logger.log("A message");
...
logger.log("Another message");

Example 3

If we eliminate the null values and replace them with place-holders or dummy values we can transform this:

...
List<Student> filterStudents(StudentFilter filter) {
   if (filter == null) {
      return null;
   }

   List<Student> filteredStudents = new ArrayList<Student>();
   for (Student student : studentList) {
       if (filter.field!= null ) {
           if (filter.keyword != null &&
student.getField(field) != null) {
               if (student.getField(field).contains(keyword)) {
                   filteredStudents.add(student);
               }
           }
       }
   }
   return filteredStudents;
}
...

into this:

...
List<Student> filterStudents(StudentFilter filter) {
   List<Student> filteredStudents = new ArrayList<Student>();

   for (Student student : studentList) {
       if (student.getField(field).contains(keyword)) {
           filteredStudents.add(student);
       }
   }
   return filteredStudents;
}
...

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s