473,395 Members | 1,742 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

isNull() method a good idea?

I am going through some legacy code that has an "isNull()" method
defined on certain classes. I can see that this can be a good way to
eliminate certain types of crashes, by making this the first call in a
method (and bailing out immediately if it is true). However, if this
is such a good idea, why is it not common industry practice?

Mohan

/////////////// Sample source code //////////////
#include <iostream>

using namespace std;

class SampleClass
{
public:
SampleClass(int i):attrVal(i) {}
~SampleClass(){}

void printIt()
{
if(!isNull())
cout << "My attribute value is " << attrVal << endl;
else
cout << "I am NULL" << endl;
}

bool isNull() {return this == 0;}

protected:
int attrVal;
};

main()
{
SampleClass *ptr = NULL;

ptr->printIt();
}

Aug 1 '05 #1
12 15478
Generic Usenet Account wrote:
I am going through some legacy code that has an "isNull()" method
defined on certain classes. I can see that this can be a good way to
eliminate certain types of crashes, by making this the first call in a
method (and bailing out immediately if it is true). However, if this
is such a good idea, why is it not common industry practice?
If you are talking about the code below, it is illegal in C++.

/////////////// Sample source code //////////////
#include <iostream>

using namespace std;
If this is a header file, don't do that. I don't want your using
directive if I include your file.
class SampleClass
{
public:
SampleClass(int i):attrVal(i) {}
~SampleClass(){}

void printIt()
{
if(!isNull())
cout << "My attribute value is " << attrVal << endl;
else
cout << "I am NULL" << endl;
}

bool isNull() {return this == 0;}
This will always return false in a conforming C++ program. You cannot
call a member function on a null poiner.

protected:
int attrVal;
};

main()
main() returns an int. Always.

int main() {
SampleClass *ptr = NULL;

ptr->printIt();
Undefined behavior. Illegal. Bad for your health. Don't do that.
}

Jonathan

Aug 1 '05 #2
Generic Usenet Account wrote:
I am going through some legacy code that has an "isNull()" method
defined on certain classes. I can see that this can be a good way to
eliminate certain types of crashes,
Eliminating certain types of crashes is a "good way" to hide other bugs,
which actually should be eliminated themselves instead.
by making this the first call in a
method (and bailing out immediately if it is true). However, if this
is such a good idea, why is it not common industry practice?
No, it's not a good idea. I actually think it's A BAD IDEA(tm). Never
should a member function be called through a pointer that is null. It
causes undefined behaviour according to the language Standard.
[..]


V
Aug 1 '05 #3
Ian
Generic Usenet Account wrote:
I am going through some legacy code that has an "isNull()" method
defined on certain classes. I can see that this can be a good way to
eliminate certain types of crashes, by making this the first call in a
method (and bailing out immediately if it is true). However, if this
is such a good idea, why is it not common industry practice?

Worse than that, it's a stupid and dangerous idea.

It an object is NULL, how could this method ever be called?

Ian
Aug 1 '05 #4
Ian wrote:

It an object is NULL, how could this method ever be called?

Ian


I was myself very surprised to see the code executing (g++ and native
Solaris compiler)!

Mohan

Aug 1 '05 #5
Generic Usenet Account wrote:
Ian wrote:
It an object is NULL, how could this method ever be called?

Ian


I was myself very surprised to see the code executing (g++ and native
Solaris compiler)!

Mohan


It won't work as written in your post. There must be more to it -
IsNull() is a static member perhaps, or IsNull() used by a class
to check whther or not one of the class member variables is
NULL, etc...

Provide the actual code (class def's and usage) that you've seen
working.

Larry
Aug 1 '05 #6
Larry I Smith wrote:
Generic Usenet Account wrote:
Ian wrote:

It an object is NULL, how could this method ever be called?

Ian
I was myself very surprised to see the code executing (g++ and native
Solaris compiler)!

Mohan

It won't work as written in your post.


Yes, it does "work". It is undefined behaviour nonetheless. So, whatever
it does, it does it by accident, AFA C++ is concerned. It is expected on
some operating systems, however. It used to be the rule among novice C++
programmers on MS-DOS, for example, to check if 'this' was 'NULL'...
There must be more to it -
IsNull() is a static member perhaps, or IsNull() used by a class
to check whther or not one of the class member variables is
NULL, etc...
No, it isn't.
Provide the actual code (class def's and usage) that you've seen
working.


I just compiled it and saw it "working" on one of our Sun Enterprise 4000
machines with the source code "GUA" posted in the original post. Do not
guess, find a Sun machine and check it. What the hell, check it on your
Linux box. How long would it take you to copy-paste it and compile and
run the a.out?

V
Aug 1 '05 #7
Ian
Generic Usenet Account wrote:
Ian wrote:

It an object is NULL, how could this method ever be called?

Ian

I was myself very surprised to see the code executing (g++ and native
Solaris compiler)!

Oh it will execute, provided isNull() is a non-virtual member. All you
end up with is a function like

mangled_name_isNull( SampleClass* this )

being called with this == NULL.

But in anywhere other than a trivial example, you are asking for trouble.

If you are going to check for a null object, do it when the object is
created, or first encountered.

Ian
Aug 1 '05 #8
Larry I Smith wrote:
Generic Usenet Account wrote:
Ian wrote:
It an object is NULL, how could this method ever be called?

Ian
I was myself very surprised to see the code executing (g++ and native
Solaris compiler)!

Mohan


It won't work as written in your post.


I think that an actual compilation of this is highly likely to work.
There must be more to it -
IsNull() is a static member perhaps,
It can't be. It refers to 'this'.
or IsNull() used by a class
to check whther or not one of the class member variables is
NULL, etc...
No, it detects whether 'this' is null, i.e., whether the caller is being
naughty.
This is what the MFC does in VC++ 6.0 (compiler vendor's privilege, I
suppose)
void CObject::AssertValid() const
{
ASSERT(this != NULL);
}
Provide the actual code (class def's and usage) that you've seen
working.


The OP has already done that.

The idea behind this is torpedoed by the language standard, as others have
pointed out. However, would a member function compiled by a real compiler
ever need to execute code dereferencing a null 'this' unless it is used to
access a member variable or call a virtual function? Probably not, and if
not, then the code would probably work, even though the standard says
nothing of the sort.

I think it's a bad idea, too, BTW, in case I've given the opposite
impression.

DW
Aug 1 '05 #9
Victor Bazarov wrote:
I just compiled it and saw it "working" on one of our Sun Enterprise 4000
machines with the source code "GUA" posted in the original post. Do not
guess, find a Sun machine and check it. What the hell, check it on your
Linux box. How long would it take you to copy-paste it and compile and
run the a.out?


This very thing came up a few weeks ago and it also works fine on SGI
systems. (for better or worse, you decide)

For "the usual" implementations it will work provided no virtual
functions are called nor any instance data accessed. Not sure if
multiple inheritance would hose this up or not and I'm too tired to
think about it just now ;-)

IMHO isNull() would only be useful a) during debug and b) if it dumped
source info to say cerr or a log file, then aborted the program. NULL
pointer errors are best caught and dealt with immediately during
development, never, ever handled quietly... (with the possible exception
of life or death code in a production/use environment where
failure/dumping is not an option)
Aug 2 '05 #10
> I am going through some legacy code that has an "isNull()" method
defined on certain classes. I can see that this can be a good way to
eliminate certain types of crashes, by making this the first call in a
method (and bailing out immediately if it is true). However, if this
is such a good idea, why is it not common industry practice?


It is a bad idea, unless used in assert.

Unfortunately, as of common industry practice, MFC is full of such
checks.... (Perhaps that should tell you something of quality of its
design :)

Mirek
Aug 2 '05 #11
Ram
> I am going through some legacy code that has an "isNull()" method
defined on certain classes. I can see that this can be a good way to
eliminate certain types of crashes, by making this the first call in a
method (and bailing out immediately if it is true). However, if this
is such a good idea, why is it not common industry practice?
I think this is a bad idea b'coz it doesn't make a sense to call a
member function on a non-existent object.
/////////////// Sample source code //////////////
#include <iostream>

using namespace std;

class SampleClass
{
public:
SampleClass(int i):attrVal(i) {}
~SampleClass(){}

void printIt()
{
if(!isNull())
cout << "My attribute value is " << attrVal << endl;
else
cout << "I am NULL" << endl;
}

bool isNull() {return this == 0;}

protected:
int attrVal;
};

main()
{
SampleClass *ptr = NULL;

ptr->printIt();
}


A better way would be

SampleClass *ptr = NULL;
// ...

if(ptr)
ptr->printIt();
//...

Aug 2 '05 #12
After all, what's the problem of making the comparison explicit?

if (!ptr_to_manager)
{
throw ...
}
else
{
ptr_to_manager->pay_my_salary();
// ...
}
Aug 3 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

31
by: Chris S. | last post by:
Is there a purpose for using trailing and leading double underscores for built-in method names? My impression was that underscores are supposed to imply some sort of pseudo-privatization, but would...
2
by: Bob Cottis | last post by:
I am getting wierd behaviour with IsNull in ASP. I am passing a string (which may be null) to a function. When the string is null, IsNull seems to return false the first time it is called, then...
2
by: Trev | last post by:
I have two tables, tblMTO and tblIMPORT_MTO. If I import an entire MTO into the import table I want to create a delta from it (i.e. leave only the changed items). I have a view (simplified) ...
4
by: Paul Spratley | last post by:
Hi all Firstly this my first time posting to technical groups - so any mistakes I apologise for in advance. I am trying to count records in several secondary tables for the same run in a...
2
by: Raoul Watson | last post by:
I have used isNull statement for as long as I have used VB.. Recently I am devugging a program and it is very clear that the "IsNull" function sometimes would return a true even when the value is...
16
by: madeleine | last post by:
Please can someone help me, I think I may go mad with this one: Do While Not IsNull(CDate(FormatDateTime(rst!F1.Value, vbShortDate))) If IsNull(CDate(FormatDateTime(rst!F1.Value, vbShortDate)))...
19
by: zzw8206262001 | last post by:
Hi,I find a way to make javescript more like c++ or pyhon There is the sample code: function Father(self) //every contructor may have "self" argument { self=self?self:this; ...
22
by: PW | last post by:
Hi All, I go into debug mode with the code below and varReturnVal is Null, but the code still is run. Any idea why? Dim varReturnVal As Variant Dim intDogID As Integer
8
by: DBlearner | last post by:
Good afternoon folks, I'm a total novice at this game. I'm a student at an University in DC and is taking a basic course in Access and database. I need some help writing a nesting expression...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.