Checking for null parameter

T

Tom Anderson

If some application like, say, MS Word terminated whilst I was using it and
displayed "NullPointerException" and a stack trace, I'd describe it as having
crashed.

I'd be surprised if the general population of MS Word users felt any
different.

I'd give a loud cheer and go to the pub myself.

tom
 
T

Tom Anderson

Someone even called you insane,

That was me, and it was rude and i apologise for it. Sorry, Lew - you're
not insane. We have different ideas on some things, but that doesn't make
either of us crazy.

tom
 
T

Tom Anderson

[ SNIP ]
It would be nice if java could express a requirement for non-nullity in
the type system. Some type systems forbid nulls from normal variables, and
if you want a variable that can be null, you have to call it an
"Option<Foo>" or something. I quite like this idea - most variables
shouldn't be null, so make that the default, but provide a way of having
them be null when that's okay. A simple syntax would be a question mark
after the type - implying it might be one of those, or it might not:

String foo = null ; // compile-time error
String? bar = null ; // that is okay
String baz = bar ; // also a compile-time error - can't assign from ? to
not-?
String qux = (String)bar ; // okay, albeit icky

That's probably in Scala or something.
[ SNIP ]

It's probably in C# is what it's probably in. :) A nullable type in C#
is an instance of System.Nullable.

I was about to say that a type can't be an instance (unless you have
metaclasses), but you mean a template instantiation, aka a specialisation,
i realise.
Essentially these are value types. So you can write (as a shorthand)

double? temperature = null;

See http://msdn.microsoft.com/en-us/library/2cf62fcy(VS.80).aspx

That's pretty cool. I can see that it's essential for effective value
classes. Next, i want to see it extended to all types.
What Scala has is the Option class.

Ah, that's where i got it from.

tom
 
T

Tom Anderson

[...]
Essentially these are value types. So you can write (as a shorthand)

double? temperature = null;

See http://msdn.microsoft.com/en-us/library/2cf62fcy(VS.80).aspx

That's pretty cool. I can see that it's essential for effective value
classes. Next, i want to see it extended to all types.

Meaning what? In C#, anything that's not a value type already is
nullable, inherently. How would you extend it? Or are you just
reiterating your desire that Java include something like this?

Sorry, i didn't make myself clear. I was suggesting that the non-value
types be treated the same way as the value types: by default, they can't
be null, and if you want them to be nullable, you have to declare them
with a ?.

The point is to get rid of inappropriate nulls with the type system, and
thus avoid the whole question of whether we should check for null
parameters etc.

tom
 
P

pek

That's indeed a great idea.
Ah. I see.

Well, while I see the benefit you're trying to achieve, I doubt you'll
ever see that. In Java, maybe it's less clear, but in C# a significant
feature of a reference type is that it's nullable. In other words, it's
not just a side-effect of being a reference type, it's a fundamental
characteristic of the type.

Besides, at this point retrofitting Java or C# to enforce a "non-nullable"
attribute on the type would be completely impractical. You'd have to go
through the entire framework and either get rid of or wrap in exceptions
somehow all of the places where a null value is possible.

Somehow I believe that the minds behind Java will make it possible one
way or another, as they did with many other. Or at least I hope so, so
that I stick for a while more in Java.. ;)
 
L

Lew

Somehow I believe that the minds behind Java will make it possible one
way or another, as they did with many other. Or at least I hope so, so
that I stick for a while more in Java.. ;)

Not going to happen, I will bet. I guess you'll just have to quit
using Java.
 
T

Tom Anderson

Ah. I see.

Well, while I see the benefit you're trying to achieve, I doubt you'll
ever see that. In Java, maybe it's less clear, but in C# a significant
feature of a reference type is that it's nullable. In other words, it's
not just a side-effect of being a reference type, it's a fundamental
characteristic of the type.

Same in java, i think. The crux of my argument is that in the vast
majority of cases, nullability is actually not helpful. Pick any class,
from any library, and look at the parameters to its methods: in how many
cases is it legitimate for them to be null? The chances are it's few or
none. Of course, we need nullable variables and parameters in some cases,
but most of the time, preventing nullness makes our programs more
reliable.
Besides, at this point retrofitting Java or C# to enforce a
"non-nullable" attribute on the type would be completely impractical.
You'd have to go through the entire framework and either get rid of or
wrap in exceptions somehow all of the places where a null value is
possible.

Oh, absolutely. It would be a complete nightmare to retrofit, and we'll
never have it in java.
Maybe you'll get it in the next language. :)

Here's hoping!

tom
 
P

pek

Or at least I hope so, so
I have a hard time believing someone would really switch languages over  
that issue.  Talk is cheap.  :)

Pete

Indeed, a wrong choice of words. I was mentioning to your line:
Maybe you'll get it in the next language. :)

Every language has it's pros and cons, depending on what is a pro for
your, the more a language has, the more likely you'll stick with it.
 
D

Daniel Pitts

Lew said:
Oh, good, you found an alternative to Java. Have fun with it.
Nice is an interesting JVM based language. I've never used it, but have
looked into it. The nice (no pun) thing about it is that it can easily
interact with Java libraries.
 
A

Arne Vajhøj

Daniel said:
Nice is an interesting JVM based language. I've never used it, but have
looked into it. The nice (no pun) thing about it is that it can easily
interact with Java libraries.

I believe that is a common feature among JVM based scripting languages.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top