Confused Programmers Question Code... (C# 2.0 and the nullable syntax)

It is odd when a programming language feature uses such a powerful natural language construct. The question mark (?) denotes so much meaning in terms of understanding, insight, and curiosity. To use such a symbol to mean something quite different, in the case of nullable types in C#, is very odd to me. Take the following code:

int? foo = null;

The above seems almost incomplete, as if the programmer didn't know exactly what they were doing, and not that the type is actually a larger construct that allows for the absence of value. I have to admit, it is a great shortcut, but readers of the language will have to be curious about why the programmer is questioning his use of variable type. Even worse, they are assigning null to an integral type, how confusing.

Even more confusing is the allowance of a nullable reference type, however, the compiler does give due warning for this. However, to note there could be cases where the lack of a value has different meaning than a null value. The lack of a value might mean unset or untried, a kind of quantum concept for not having investigated the possibilities of a variable. A null value on the other hand would clearly represent a lack of data after inspection. Two states at once and the possibility of two different types of return values. Note: This is NOT possible since there is no way to set the value to null and have the HasValue return true. It would have just been nice to have in some strange world far, far away

Now, to think about the construct in terms of a question, “Is the value present or not?”, would be appropriate. At least they didn't use an exclamation point (!), because the last thing we need is to get spuriously excited by writing our code. I already have a twitch from the coffee, don't compound that with short bouts of value type excitement.

Now, taking that in stride, would the exclamation point provide a meaningful construct? Well, an exclamation implies excitement, something more, something greater, enhancement. We often use it to enhance what we are saying. The difference between “That's great Bob” and “That's great Bob!” is unobvious at times in written form, but the spoke differences are extreme. After all, a nullable value type is really something more, but only slightly more, than the original value type. So in this case an exclamation point would be just as valid a choice to denote nullable types.

Well, you probably find it strange the reading code may evoke some sort of natural language emotional reponse. However, I know a number of programmers that took the length of a comment, for instance, to mean something more than what was implied. Short sentences tend to be imperative when read making them seem absolute. When documenting code you often write few words to explain what you were doing. You aren't setting anything in stone, but the appearance upon reading is there.

Well, while you get angry at short comments, hopefully that gets balanced out by a nagging, questioning feeling coming from all of the extra question marks.

Published Tuesday, July 13, 2004 11:53 AM by Justin Rogers
Filed under:

Comments

Tuesday, July 13, 2004 3:04 PM by Frans Bouma

# re: Confused Programmers Question Code... (C# 2.0 and the nullable syntax)

Add this to the confusion:

int? foo;

// datareader dr is initialized and looped through
foo = dr["Foo"].Value; // can be NULL (DbNull.Value)

this doesn't work at the moment. However this is (IMHO) one of the most useful areas for nullable types.
Tuesday, July 13, 2004 3:20 PM by nospamplease75@yahoo.com (Haacked)

# RE: Confused Programmers Question Code... (C# 2.0 and the nullable syntax)

It actually sorta makes more sense if you spend a lot of time reading regular expressions for fun.

? = 0 or 1.

Tuesday, July 13, 2004 4:59 PM by Per Hultqvist

# re: Confused Programmers Question Code... (C# 2.0 and the nullable syntax)

Since the letter u is used for unsigned, my proposal is to use the letter n as an prefix (n for nullable), i.e. :

nint = nullable int
nlong = nullable long
...

I just realized when typing that we would then need the following too:

nuint = nullable unsigned int
nulong = nullable unsigned long
...

Now I'm not so sure if it is a good idea anymore...oh well...time to sleep anyway.../Per
Tuesday, July 13, 2004 11:10 PM by Justin Rogers

# re: Confused Programmers Question Code... (C# 2.0 and the nullable syntax)

To Haacked: Yeah, use something from regular expressions because that isn't already ranked as the most confusing programming construct in history ;-) j/k, I think it is a good point.

To Per: The problem with prefixes is that they'll work for some types, but not others. For instance, every integral type is a reserved word, as is every unsigned integral type. It would introduce many more reserved words to add the n to these already reserved keywords.

You also couldn't differentiate between two value types, Perhaps struct Between {} and struct nBetween {}, trying to be a bit too clever there, but it demonstrates the point.

I would say having a nullable keyword is nice, however, that is nearly as verbose as the Nullable<> format already, and so wouldn't work very well.

My guess is that these nullable types are considered to be so extremely important that they warrant a shortcut in the form of the type post-fix nullable operator. So far, I've made good use of them in my code, but still not enough to warrant a new language prefix. I'm not sure making it easy is the right thing here, since many users are inclined to use any features that are easy, even if they aren't the right feature for the job.

IMHO, using the question mark in this circumstance is going to be perfect, since most programmers will be asking themselves "Do I even need this damn thing?", "What in the hell am I doing here?", and possible "Why would C# make it so easy if it were wrong?".
Tuesday, July 13, 2004 11:25 PM by nospamplease75@yahoo.com (haacked)

# RE: Confused Programmers Question Code... (C# 2.0 and the nullable syntax)

Oh C'mon. It's no more confusing than PERL syntax. oh wait, that is confusing. ;)
Wednesday, July 14, 2004 4:34 AM by Pattern Guru

# Programmers aren't confused between syntax and semantics, are they...?

So for me, using a question mark (ASCII 63) in code is something different than writing a question mark (that wriggly thing with the dot under it) on a piece of paper. Or typing it into a Word document or web log comment box, for that matter.

If a programmer isn't confortable with abstractions and semantical stuff like this, how much of programmer are you really...?
Wednesday, July 14, 2004 4:36 AM by Pattern Guru

# Programmers *are* confused about natural language... ;-)

Ehh... "comfortable" and "of a programmer", of course...
Wednesday, July 14, 2004 5:32 AM by Justin Rogers

# re: Confused Programmers Question Code... (C# 2.0 and the nullable syntax)

I'm not really speaking of the comfortability with the abstractions, I'm more pointing out the very real scope of human behavior with respect to symbols.

Hearing running water and wanting to pee doesn't make you less of a man, just because a man should be able to hold his liquids.

Nor does having a pre-programmed feeling towards a symbol appearning in code have any bearing on how good of a programmer you are.

For instance, if you saw a ;) as a code construct or a ;-) even, you might for an instance smile, even if you don't want to. The same point is in using things like question marks, which will cause most people to double take, at least the first few times.
Tuesday, July 03, 2007 7:46 AM by Rexiology@MSDN

# C#: What does it mean about statement "int? varA = 3;" ?

Ok, I have to admit that I didn't really go through the whole C# language reference and today when I

Tuesday, July 03, 2007 7:48 AM by Rexiology::Work

# C#: What does it mean about statement "int? varA = 3;" ?

crosspost from http://blogs.msdn.com/rextang Ok, I have to admit that I didn&#39;t really go through

Tuesday, July 03, 2007 8:18 AM by Noticias externas

# C#: What does it mean about statement "int? varA = 3;" ?

Ok, I have to admit that I didn&#39;t really go through the whole C# language reference and today when

Wednesday, March 24, 2010 3:13 PM by Reed

# re: Confused Programmers Question Code... (C# 2.0 and the nullable syntax)

Hi all. Lack of money is no obstacle. Lack of an idea is an obstacle. Help me! I find sites on the topic: Precious moments baby bedding. I found only this - <a href="baby-bedding.net/">green baby bedding</a>. Bedding, although they have immensely used each solvent in centuries, their internet is reasonable. Bedding, i realized that the picture is the most new relaxation party suffering back with any dismissal weekday research. With respect :rolleyes:, Reed from Bolivia.

Thursday, July 14, 2011 9:27 PM by chi flat iron

# re: Confused Programmers Question Code... (C# 2.0 and the nullable syntax)

we should be careful in selecting and using reference books

http://www.chiflat-iron.com/

Thursday, July 14, 2011 9:31 PM by discount handbags

# re: Confused Programmers Question Code... (C# 2.0 and the nullable syntax)

Nowadays more and more lectures are held on college campuses.

Thursday, July 14, 2011 9:34 PM by wholesale designer handbags

# re: Confused Programmers Question Code... (C# 2.0 and the nullable syntax)

Such a popular practice indicates that parents are attaching greater importance to their children's education.

Sunday, September 04, 2011 10:10 PM by donghanjin

# re: Confused Programmers Question Code... (C# 2.0 and the nullable syntax)

Since the mid-1990s, the fashion industry movers and shakers of the few brands,

Leave a Comment

(required) 
(required) 
(optional)
(required)