-2

Is it a good practice to use the static member methods to check if an object of a class is NULL or not. The object would be sent through the parameters offcourse.

Something like,

#include <iostream>
using namespace std;


class Box {
    public:
      static int checkNull(Box* b) {
         if (b != NULL)
            cout << "present\n";
        else
            cout << "absent\n";
      }

};

int main() {

    Box *b1, *b2;
    Box b;
    b1 = b2 = NULL;
    b1 = &b;
    Box::checkNull(b1);
    Box::checkNull(b2);

    return 0;
}
2
  • 1
    Sharing your research helps everyone. Tell us what you've tried and why it didn’t meet your needs. This demonstrates that you’ve taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer. Also see How to Ask Commented Feb 15, 2019 at 9:27
  • This example looks way too contrived to generate an answer which will fit to yout real code. It makes absolutely no sense to have this kind of checkNull method inside the Box class, since it contains only a fictional action which is in no way related to a Box. Write a better question, get a better answer. Commented Feb 16, 2019 at 5:53

1 Answer 1

2

No, and I'll give you several reasons why this is a bad idea:

  1. You're creating a solution that will work exclusively with Box pointers. Every pointer in C++ can be checked to be NULL, and by creating a method for this express purpose, you're singling out Box pointers. Generally you should find solutions that work in the general case if it is applicable in the general case.
  2. It is arguable whether or not this is more readable than simply writing "b1 == NULL". I am a strong believer in porting code even concise into methods for readability, however this is a bit too extreme in my opinion, and it isn't even something that is particularly difficult to read in its own rite.
  3. Checking pointers is not something specific to Box class. In an OOP paradigm, methods, even static methods, should pertain in some direct way to the class which holds it. This is nothing specific to Box, which is presumably meant to hold dimensions of some kind.

Maybe it is not evident to you now, but with some experience under your belt, you'll probably see why this is generally a code smell. Good luck!

4
  • Hmm.. I understand your point.. Do you think this practice would be justified, if there were some Box class related processing to be done in case the object is NULL. May be like, failing a state machine, or freeing some resources? Commented Feb 15, 2019 at 9:55
  • @Haris No, though certainly having a method dedicated towards freeing up Box (assuming it is more elaborate than simply freeing the pointer) or possibly failing a state machine in the case the pointer passed is NULL may be justified. I just wouldn't have one purely for checking if it is NULL. :) Commented Feb 15, 2019 at 10:01
  • ok.. But in such a case, the general method to check for NULL will have to call this dedicated Box method to do the cleanup (may be not a like a destructor exactly).. Commented Feb 15, 2019 at 10:13
  • @Haris One doesn't have to call the other. If you can write the method in 5 lines or less, and no amount gets reused elsewhere, write it in a single method. And yes also, don't forget about the deconstructor which is probably better for this than a dedicated method. Commented Feb 15, 2019 at 10:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.