tags:

views:

388

answers:

13

Some may feel this question is subjective. But, I feel this is among the most important things to be told to a programmer.

Is this a good function name to check for null values.

1. checkNull()
2. notNull()
3. isNull()

What if I write

checkIfNull()

I do not know how many people share the same feeling as I do, I have spent more time in thinking good names for my functions than writing one.

How do people think of good names? Can the naming be consistent across languages (mainly C++ and Java)

Update: As I go by the number of updates till now, Most people prefer isNull(). How do you decide upon this that isNull() is the perfect name.

checkNotNull() // throw exception if Null

Is this a good name? Does everyone depend upon their intuition for deciding a name?

The question is about choosing a perfect name!!!

+2  A: 

Nowadays it is highly recommended to use the javaBeans convention:

isNull() //if the return type is a primitive
getNull() //if the return type is an object (Like Boolean in java)

For non boolean types access members, you should use get.
For static variable members use the camel case style: "myVar".
For class name use camel case style with capitalized first letter: "MyClass".
And for constant members use uppercase letter with underscore as separator: "MY_CONSTANT".

marcos
+1  A: 

I prefer IsNull.

To learn good naming style, study the standard libraries (except in PHP). You should follow the style used by the standard libraries in each language.

For C#, study the Framework Design Guidelines.

SLaks
"You should follow the style used by the standard libraries in each language". Hmm, I don't do this at all in C++. I like my classes to have names with initial capitals, even though the standard library classes don't. So I guess, "follow the style of the libraries, unless the libraries have their own special style"...
Steve Jessop
this is about styles, but the question is about choosing meaning ful names.
Devil Jin
Learning the environment's naming style will help you produce meaningful names.
SLaks
+1  A: 

personally, I would use

   IsNull()
Eclipsed4utoo
+4  A: 

isNull might be a bad example, because:

Object foo = null;
if (foo.isNull()) { // Causes a NullPointerException in Java. }

Otherwise, you've got:

Object foo = null; 
if (UtilityClass.isNull(foo) { }

Which seems harder and less clear than just doing:

Object foo = null;
if (foo == null) { }
Dean J
A: 

It can vary depending on the language you are using - and you tagged a couple to this question. It is important to stay consistent with the standards of the language/library you are coding against. Yes, naming conventions are very important! [There's even a wikipedia entry on it: http://en.wikipedia.org/wiki/Naming_conventions_%28programming%29]

For .Net I found this "cheat sheet" on naming conventions:

http://www.irritatedvowel.com/Programming/Standards.aspx

For your example in C# I'd reccommend : IsNull()

brendan
A: 

If your company does not specify naming conventions in its coding standards I suggest it's time you add them.

Our company's Java coding standards are basedon the official Java Coding Standards which, I believe, specify names like isNull().

From your example, the notNull() is bad, because you may end up with statements like if(!notNull()) or the like.

iWerner
Then again, it also allows if(notNull()) instead of if(!isNull()), which is somewhat clearer. Double negatives are a separate issue. After all, nothing stops a bad programmer from writing if (!!!isNull()).
Steven Sudit
I disagree. You seem to be advocating doubling all predicates, to create both positive and negative versions, and therefore not using the negation operator: IsNull and IsNotNull instead of IsNull and !IsNull. IMHO This is an unnecessary proliferation of methods, and could lead to someone ignoring the IsNull and using !IsNotNull instead--surely an aberration. Are there any existing frameworks that implement this doubling of predicates? The one I am familiar with (.Net) does not, and that is good enough precedence for me.
ShellShock
If you read carefully, you'll note that I'm distinguishing between double negatives, which we all agree are bad, and properties whose names contain negations. I said nothing about having both positive and negative versions of each property. You are, unfortunately, arguing against something I did not endorse.
Steven Sudit
To make my point clearer, I've noticed that some properties seem to be used more with a ! than otherwise. For example, I see a lot of `!String.IsNullOrEmpty(s)` but not many `String.IsNullOrEmpty(s)`. I suggest this is an indication that the wrong direction may have been chosen as the positive. In the case where neither direction has any real claim to the default, then we're left with something of a dilemma.
Steven Sudit
A: 

I would use IsNull(); there is a precedence in .Net which has a static IsNullOrEmpty() method for the String type. "Is" is my preferred prefix for methods that return a bool. I would not have a negative method "notNull", because this too easily results in double negatives. Instead use the negation operation on a positive method, e.g., !IsNull().

However, a method that only checks for a null value may be overly complicating things; what is wrong with

x == null

Which I think is more readable than

IsNull(x)

Most developers seeing IsNull(x) would wonder if there is some fancy null checking in the IsNull method; if there isn't then "x == null" is probably better.

ShellShock
As I suggested to iWerner, I don't think double negatives are a genuine problem. Nothing stops any number of negations, regardless. We just have to use good sense.
Steven Sudit
+2  A: 

IsNull() is a good choice, But additionally it should return a bool.

So that you can check its value in if statment without getting any NullReference exception.

Amit
+2  A: 

Like the others, I prefer isNull() (or IsNull(), depending on your language/coding conventions).

Why? Beside it is a widely accepted convention, it sounds nice when you read the code:

if (isNull())
// or
if (foo.isInitialized())

and so on. Almost natural English... :-) Compare to the alternatives!
Like iWerner, I would avoid negative form for making identifiers (variables, methods) names.
Another common convention is to start method/function names with a verb. Now, Sun did not follow this convention in the early days of Java (hence the length() and size() methods, for example) but it even deprecates some of these old names in favor of the verb rule.

PhiLho
+3  A: 

If the function throws an exception if it's null, it should be called ThrowIfNull to make it clear that it will throw for you.

SLaks
+1  A: 

If you're doing a lot of null checking in your code, I think having a pair of methods, i.e.:

IsNull()

IsNotNull()

will lead to the most readable code in the long run.

I know !IsNull() is a standard idiom in curly brace languages, but I think it's much less clear than IsNotNull.

It's too easy to overlook that single "!" character, especially if it's buried in a more complex expression.

Tom Bushell
Not use "!"! How far do we go with this--do we avoid all single character operators? Or have you just got it in for the "!".
ShellShock
I use "!" occasionally. But I find it reduces readability if overused, and can lead to logic errors if you're not careful.
Tom Bushell
You shouldn't use "negative" names (ref. Code Complete), because it makes it hard for the brain to grasp if you have to negate the value.This isn't automatically intuitive: !IsNotNull()
erikric
That's why I suggest having two methods - you would use IsNull instead of !IsNotNull.
Tom Bushell
+1  A: 

I found this article. Felt like sharing with you guys!

Devil Jin
+2  A: 

The answer depends on what your method returns.
If it returns a bool indicating whether the object is null, I would name it IsNull(Thing thing), because it is the least ambiguous formulation - what the method does and what it returns is immediately obvious.
If the method is void but throws if the object is null, I would call it GuardAgainstNull(), or something along these lines.
IMO, CheckNull() is somewhat ambiguous - you don't know by looking at the method if it should return a bool or throw, or what the bool indicates exactly.

Mathias