CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2022

    Question Is it ok to use null checks?

    What are your thoughts about explicitly checking for null in Java or other language that allows nulls? There are some alternatives but I don't know what think of it. I read an article recently(linked below), that suggests to throw exceptions, using Optional and other ...
    Is there any case that would justify the usage of null checks? I feel like checking for null is bad design but if so, why Java allows it in first place? Please let me know your thoughts.
    This article I read says that doing null checks is bad design and I kind of agree with it, I see where is coming from. But so many other devs just say that is BS.
    I did work in a company where they had methods with almost 10 parameters and full of null checks, I think that was definitely bad design butt everybody was so obsessed with checking for nulls that I didn't know how to argue against them.
    Please let me know your thoughts, I personally like some of the alternatives mentioned in that text.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012

    Re: Is it ok to use null checks?

    Well C++ allows null pointer (nullptr - 0). If a function returns a pointer then checking for nullptr is valid - and probably needed. The question probably isn't about checking a pointer for nullptr, but rather should a pointer be returned in the first place. Also, there's the situation in passing an optional argument to a function. If an argument is passed via a pointer then it's valid to check that pointer for nullptr to see if an argument has actually been passed or not - which is different to having a default value if an parameter is not specified.

    For returning an optional value (or an error value) then returning a pointer in C++ used to be the way - with the caller checking for nullptr. However there's now C++ std:ptional for returning an optional value and exceptions for indicating an error has occurred (such as memory error when trying to allocate).
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Feb 2017

    Re: Is it ok to use null checks?

    Quote Originally Posted by n1m_n1 View Post
    I feel like checking for null is bad design but if so, why Java allows it in first place? Please let me know your thoughts.
    The inventor of the null reference, Tony Hoare, has publicly apologized for it. He calls it his billion-dollar mistake. See this Wikipedia entry (the first paragraph in the Apologies and retractions section)

    It beats me why Java introduced it, and even more so, why they haven't done anything about it? Even the NullPointerException-message itself is a misnomer because there are no pointers in Java.

    I don't see how you can avoid every possibility of a NullPointerException sneaking in, not even with the most careful of designs. Not as long as there are people who do the programming. Experienced programmers tend to be defensive. I think they will keep adding safeguards to avoid embarrassing runtime errors in their code. Defensive checking is even considered a good Java idiom by many. Mistakes happen, also in well-designed programs.
    Last edited by wolle; February 21st, 2022 at 01:12 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Click Here to Expand Forum to Full Width