Non-void function potential lack of return

Str-Gen

I wrote this piece of code to check if a node is a left or right child in a tree. However from a cleanliness perspective, I don't think it is properly implemented.

My issue is that returning a default like false if the parent pointer is null, is dangerous because false would mean it's a right child. However this function needs a return if I want it to compile without warnings. The empty throw is definitely ugly as a solution.

I have seen people use assert(parent) instead of if, but I don't think that's good either because the assert a debug feature.

In short, what are better ideas to implement this function?

bool isLeftChild() const {
    if(parent){
        if(this == (parent->left).get()){
            return true;
        }
        else if(this == (parent->right).get()){
            return false;
        }
    }
    else throw;
}
Nicol Bolas

Cleanliness is ultimately a matter of opinion. What you're having trouble with is the question of what this function's contract is. That is, do you want it to define the behavior of this code if it is called on a node with a NULL parent?

If the answer is yes, then throwing is perfectly valid. Much like vector::at throws if the index is out of bounds.

bool isLeftChild() const
{
    if(!parent) throw ...;

    if(this == (parent->left).get()){
        return true;
    }
    else if(this == (parent->right).get()){
        return false;
    }
}

If the answer is no, then you shouldn't throw. This is how vector::operator[] works with an out-of-bounds index. In C++20, you would express this as a contract:

bool isLeftChild() const
    [[expects: parent != nullptr]]
{
    if(this == (parent->left).get()){
        return true;
    }
    else if(this == (parent->right).get()){
        return false;
    }
}

But this is conceptually an assert; you are saying that undefined behavior results if you call this function on a node with a NULL parent.

Which is "cleaner" is up to you and how you want your interface to behave. "Wide contracts" (where you throw on failure rather than invoke UB) are often seen as the "safer" alternative. This is primarily true in that you get an exception thrown from the place where things went wrong, rather than from other locations. But in this particular case, if parent is NULL, you'll immediately find out when you try to dereference it.

But even so, wide contracts are generally considered poor form in modern C++. Indeed, with contracts in C++20, the committee is looking into slowly removing the places in the standard library that throw logic_errors, turning them instead into contract violations (ie: UB).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

unexpected non void return value in void function

Is the return command in a non-void function necessary?

Type No return, in function returning non-void

Unexpected non-void return value in void function

Unexpected Non-Void Return Value In Void Function (Swift 2.0)

Unexpected non-void return value in void function (Swift 3)

Swift NumberOfRowsInSection Unexpected non-void return value in void function

'Unexpected non-void return value in void function' in swiftui

Empty return in non-void function, is undefined behaviour?

How to return nil object in non void function - swift 2.1?

C++: Is It Possible to End a Non-Void Function Without a Return?

swift Non-void function should return a value in a guard let

C Warning: No return statement in function returning non-void

warning: (343) implicit return at end of non-void function

Casting lambda with non-void return type to function pointer

In a non-void function I want to return nothing

why is this warning ( no return , in function returning non-void)?

typescript function declared a non-void type but has no return expression

What is the return value if we dont return anything from a non void return typed function in c++?[Experimental]

why in this code would I be getting "Unexpected non-void return value in void function"

Retrieve product localised price - Unexpected non-void return value in void function

unexpected non-void return value in void function in new Swift class

Unexpected non-void return value in void function - Swift 4 (Firebase, Firestore)

Unexpected non-void return value in void function swift 4 using json serialisation

Unexpected non-void return value in void function Swift3

Why does Unexpected non-void return value in void function happen?

Unexpected non-void return value in void function(swift3)

Class understanding: Unexpected non-void return value in void function (swift 3)

Return from void function

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive