QDomNode::isNull() and QDomNode::hasChildNodes()



  • While refactoring some code, I came across the following if-clause several times:

    const QDomElement foo = mfGetSomeQDomElementFromSomewhere();
    if (!foo.isNull() && foo.hasChildNodes())
    { ... do stuff ...}
    

    I am wondering why the author added the check !foo.isNull()... I am thinking of refactoring this to

    if (foo.hasChildNodes()) {...}
    

    because if foo has child nodes, it implies that it is not null, right?

    Just wanted to check my reasoning and that I haven't overlooked something. Feel free to confirm or point me to mistakes in my reasoning :-)


  • Qt Champions 2016

    Hi
    Not checked with QDomElement but
    such check can also be that
    if foo.isNull() then its not safe to to call foo.hasChildNodes()
    But purely speculating.


  • Qt Champions 2016

    @Bart_Vandewoestyne said in QDomNode::isNull() and QDomNode::hasChildNodes():

    because if foo has child nodes, it implies that it is not null, right?

    Yes, but not having child nodes doesn't mean the node is null. ;)

    Just wanted to check my reasoning and that I haven't overlooked something.

    I don't think you have overlooked anything, it should be safe to remove the first part of the check.



  • We are still using Qt 4.8.7, so I checked the source:

    bool QDomNode::hasChildNodes() const
    {
        if (!impl)
            return false;
        return IMPL->first != 0;
    }
    
    bool QDomNode::isNull() const
    {
        return (impl == 0);
    }
    

    so from this I conclude that my assumption was right and the refactoring was ok :-)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.