3

Consider for example in the database:

CREATE TABLE users (
  id INTEGER IDENTITY PRIMARY KEY,
  last_name VARCHAR (50) DEFAULT '' NOT NULL,
  first_name VARCHAR (50) DEFAULT '' NOT NULL,
  email VARCHAR(50) DEFAULT '' NOT NULL)

And consider a program that interacts with this database using Hibernate as ORM.

In my opinion this would mean:

  • avoiding NullPointerExceptions
  • avoiding the need to check for null values

Is this good practice? Please give examples of the pros and cons.

1
  • 4
    It could be a good idea in some cases, but it depends. Note that if the database is Oracle, empty strings will automatically be transformed to NULL. Commented Oct 11, 2011 at 8:36

5 Answers 5

8

As PeterMmm writes, "Null" has a special meaning, and is generally supported by special operators in your database engine.

"Good practice" is one of those phrases that means whatever you want it to mean, but unpacking the arguments a little:

  • Maintainability: Bad idea. Most developers are used to the way NULL is implemented in database systems, and will need special training in your design.

  • Fault tolerance: Bad idea. If a column is not allowed to be null, that usually means something in your application design. If a user record MUST have an email address to be valid, you can now no longer validate this at the database level - so your database could gradually fill up with bogus data, and you'd only find out when you try to send an email to ''. "Null pointer" exceptions are feature, not a bug!

  • Performance: according to http://www.sql-server-performance.com/2007/datatypes/, NULL is slower.

Sign up to request clarification or add additional context in comments.

1 Comment

On performance, it seems null is more efficient on postgresql. See here. And here.
5

That depends on your use case. null is a special value and has a special semantic. You may default all to empty string but it could be very diferent to have last_name an empty string or a null (last name not known or not applicable)(bad example, i know).

Comments

4

Well, the con would be in that you wouldn't be able to differentiate between a "I didn't enter anything for this field" and the empty string. It would be nice in that you'd never have to worry about null strings. Unless your driver introduces them. (I think some older ones just turn empty strings into a java null.)

Comments

4

In my experience it is not a good practice. assign the null where it needs

Comments

-1

Quoting Django ORM docs:

Avoid using null on string-based fields such as CharField and TextField because empty string values will always be stored as empty strings, not as NULL. If a string-based field has null=True, that means it has two possible values for “no data”: NULL, and the empty string. In most cases, it’s redundant to have two possible values for “no data;” the Django convention is to use the empty string, not NULL.

So, yes, the most widely used Python web framework considers the design you proposed best practice and provides this behaviour by default. Java is also moving away from nulls nowadays with Java 8 Optional, perhaps it is time to reconsider our DB designs in this light.

1 Comment

Old topic, but pointing out that this advice is not well supported. The provided link is broken. I gather it would link here in the latest: docs.djangoproject.com/en/5.1/ref/databases/… where it says "Django generally prefers to use the empty string ('') rather than NULL" but it is misleading. This is not the case of "the most widely used Python web framework" declaring a best practice that the industry now should adhere to. Django is a great framework, but often enough the decisions appear naive and confounding to people using it in practice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.