views:

569

answers:

8

To me, as a developer and as a user, I find the empty string ("") useless and the cause of much confusion, it's like saying string == char[]

Maybe computers need the empty string, so I'd like to learn why.

See also:

+8  A: 

Sometimes you need a string value that isn't null if it isn't populated. Otherwise, for any given string-related task you need to perform, you have to include a null check which merely serves to bloat your code.

And just to have it on record, in C#, it is encouraged to specify string.Empty instead of hardcoding in "".

Edit: And as LBushkin pointed out, there is a difference between have an empty value versus having an unknown value.

Anthony Pegram
I find it interesting that C# has the String.IsNullOrEmpty() function. Makes coding easier but also makes it easier to not think through ramifications of null/empty in a particular case.
Skeets
That function is certainly useful when null or empty can be treated the same. In other situations, as you allude to, one value can be OK and the other can be exceptional.
Anthony Pegram
+28  A: 

Empty string are quite common in real world situations - and they are useful to differentiate from null.

  • Null means that the value is undefined or unknown.

  • An empty string means that it is known to be nothing (empty).

It might seem like splitting hairs, but there is a difference. Empty strings are instances of an object - nulls are not. You can manipulate an empty string using any of the valid methods that can be called on a string object. You cannot manipulate a null. Empty strings are often the result of certain manipulations on other strings, for example:

var source = "Hello";
var result = source.Remove("Hello");  // result is now: ""

Here's a simple example where the above matters: an application with a form that collects a persons's middle name via a textbox. Should the value of the text box be null or empty string? It's much easier to always have the textbox return an empty string as its value (when empty) rather than trying to differentiate between null and "".

Interestingly, some databases DO NOT differentiate between NULL and empty string - the best example of which is Oracle. In Oracle:

INSERT into MYTABLE(name) VALUES('')

actually inserts a NULL into the table. This can in fact create confusing situations when writing code that manipulates data from a database in .NET - you sometimes get DBNull.Value where you expected a String.

LBushkin
Good point on the difference between no value and an unknown value. +1
Anthony Pegram
Null only means "undefined" if you think it means undefined. This is rather like Humpty Dumpty: "words mean what I decide they mean". Null means whatever your program does with null. It is a common convention null means "undefined" but you might be surprised; some program may interpret "null" for a string to mean the value should be interpreted as the number Pi. I wouldn't award style points for such a program, but you'd be amazed at what you find in code.
Ira Baxter
Can you give an example where the empty string represents the known data of something ? eg. hello, my name is ""
Max Toro
no NULL means undefined to everyone but you any other person that writes if x==null then x=somethingnotnull;
fuzzy lollipop
@Ira Baxter: It's true that null is often overloaded to mean different things and it is indeed context sensitive. However, it is a common practice to treat null values as unknowns and non-nulls as defining a specific value. Defying these rules of convention can sometimes be useful, but often just confuses future maintainers of the code.
LBushkin
@Max Toro: My humorous response would be: *The shortest non-null string is ""*, but that aside, empty strings are valid string objects that simply have no text (length). They can be the result of many different manipulations on strings, for example: `"Hello World".Substring(0,0) -> ""`. If you returned null in such a case, then asking for the length of the result would throw an `NullReferenceException`. This is generally viewed as undesirable, so many operations that result in strings of no length return `String.Empty` to simplify development.
LBushkin
@LBushkin: I know how strings work, I don't need a lesson, I'm just trying to find the meaning or purpose of things.
Max Toro
@Max Toro, an example. What did you eat for breakfast? Before the question, the answer is unknown. Null. If you were to answer and audibly say "nothing", then the data representation of that is the empty string. Before the question, the answer is "value is not known." After the question (and answer), the value is emtpy.
Anthony Pegram
@Ira. is is defined to "unknown value" or simply undefined. It is however often interpreted as specific value but that's something very different. The undefined is was (A == null) very often returns null nomatter the value of A. Some languages (incl. c# will however return true but C# can't return null in a boolean expression since bool is not nullable)
Rune FS
+6  A: 

One could argue that the empty string is an instance of the Null Object Pattern. As Anthony pointed out, this is used to avoid special cases and lots of ifs.

Space_C0wb0y
true, though in many cases empty string has a different meaning than null for the business logic that code represents. For example the protocol I am working with designates empty strings as cases when the associated field in database must be cleared whereas null string means leave that field alone. Though I would admit, this is a special exception to the rule you give.
Newtopian
+10  A: 

Why do we need zero? It's really the same question. What is "pajama" without all the a's? "pjm". What is "a" without all the a's? "". Simple as that. The product of some string operations is a string without any characters in it, and that's the empty string. It behaves just like any other string - you can easily determine its length; you can concatenate it onto other strings, you can count the a's in it, etc.

Carl Manaster
Then why not use char[] instead of string ?
Max Toro
Because char[] is a *type*, not a string instance.
Ira Baxter
+2  A: 

It serves the same purpose for strings as the number "zero" serves for integers. If you didn't have zero, you couldn't write down the number of sheep you have when you don't have any sheep. Likewise, if you don't have the empty string, you can't write down the seqeunce of characters you have, when you don't have any characters.

All sorts of analoguous algebraic laws apply: as X+0 is the same as X, Y cat "" is the same as Y, etc.

Lets talk about substrings. If I have the string "abc", then

substring("abc",1,3)  is  "abc"
substring("abc",1,2)  is  "ab"
substring("abc",1,1)  is  "a"
substring("abc",1,0)  is ...?

Another interesting case:

substring("abc",1,3) is "abc"
substring("abc",2,2) is "bc"
substring("abc",3,1) is "c"
substring("abc",4,0) is ...?

A lot of programming languages get this last case wrong, because an implementer didn't think the empty string was interesting.

Ira Baxter
A: 

If an empty string is returned from a function, it means the function carried out it's purpose and the result was an empty string. If the function failed to construct a string, then it could return a null (depending on the function's contract - it may throw an exception). In this case the null and the empty string have different meanings.

The flipside is function parameters - if a string param is optional, then you have to do the null check (this is where it's usually bundled in with the emptyString() check).

ireddick
this is why you have exceptions... for exceptional situations... If the function returns null it is because the contract stipulates that some, normally occurring situations, it should return null. If something bad happened then it should throw an exception. In other words... if the function returns (whatever the value) means the function did what it was supposed to do, otherwise it throws an exception.
Newtopian
I trying to clarify cases where a null and an empty string represent different things. The rest depends on the function's contract and how exceptional behaviour is expressed in the language and/or API you are using.
ireddick
+1  A: 

why do we need xero? or an empty array, an empty set (also called a null set). Why do we care at all about things when they are not there?

Somethings are simply more interesseting when they are not present

Somethings cannot be expressed if we cannot express the value of nothing (which is very differrent from the "unknown value" of null

e.g

string allUpperCaseLetters = "";
for(byte i = 'A';i<'Z';i++)
{
  allUpperCaseLetters += (char)i;
}

if you say unknow plus (char)i the result would still be unknown however "" + 'A' is "A". How big is 1 + unknown? when an addition can wrap around you can't even state that 1+A is larger than A (if A is unknown)

some operations would be very weird if we didn't have "" how about

string letters = allUpperCaseLetters;
for(;i>0;)
{
  int i = letters.Length
  Console.WriteLine(letters[0]);
  letters = letters.Substring(1);
}

why would you want the first line inside the loop to throw a NullReferenceException rather than the loop just ending when there's no more letters?

Rune FS
A: 

I have'nt heard complaining about this one before. While null is special value which can be a value of any object while empty string is a String. Similar analogy can also be drawn between null and empty List or array.

fastcodejava