Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plane: Change a return statement that is not executed #26698

Conversation

muramura
Copy link
Contributor

@muramura muramura commented Apr 5, 2024

When AP_SCRIPTING_ENABLED is enabled, there are RETURN statements that are not executed.
I would make sure to select the RETURN statement

Copy link
Contributor

@magicrub magicrub left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lgtm

@peterbarker
Copy link
Contributor

I do not like the change introduced here. For the same reason we omit else on if statements which always return, we should omit the #else here.

Here's the early return pattern we use:

int foo() {
    if (bar()) {
        return 17;
    }
    if (baz()) {
       return 42;
    }
    return 3;
}

This is what it looks like in preprocessor:

int foo() {
#if FOO
    return BOB;
#endif

#if BAR
     return BAX;
#endif

    return XYZZY; 

See how that is symmetric?

We've rejected a lot of "pointless else" patches, we should not be adding them, they just make things more difficult to understand at a glance.

@magicrub
Copy link
Contributor

@peterbarker I don't understand your point. The # else looks fine and is appropriate here.

@peterbarker
Copy link
Contributor

peterbarker commented Sep 27, 2024

@peterbarker I don't understand your point. The # else looks fine and is appropriate here.

Would you think this is correct? I certainly don't.

bool myfunction()
{
#if AP_OPTION_A
    if (option_a_is_looking_bad()) {
        GCS_SEND_TEXT(MAV_SEVERITY_ERROR, "Everything is looking fine");
        return false;
    }
#endif

    GCS_SEND_TEXT(MAV_SEVERITY_WARNING, "Continuing world domination");
    if (frob_the_widget()) {
        GCS_SEND_TEXT(MAV_SEVERITY_WARNING, "The master plan is underway");
        return true;
    }

#if AP_OPTION_B
    if (coax_the_sylvanberry()) {
        GCS_SEND_TEXT(MAV_SEVERITY_CRITICAL, "Sylvanberry engaged");
        return true;
    }
#else
    return false;
#endif
}

.... because its natural conclusion is this:

bool myfunction()
{
#if AP_OPTION_A
    if (option_a_is_looking_bad()) {
        GCS_SEND_TEXT(MAV_SEVERITY_ERROR, "Everything is looking fine");
        return false;
    }
#else
    GCS_SEND_TEXT(MAV_SEVERITY_WARNING, "Continuing world domination");
    if (frob_the_widget()) {
        GCS_SEND_TEXT(MAV_SEVERITY_WARNING, "The master plan is underway");
        return true;
    }
#if AP_OPTION_B
    if (coax_the_sylvanberry()) {
        GCS_SEND_TEXT(MAV_SEVERITY_CRITICAL, "Sylvanberry engaged");
        return true;
    }
    return false;
#endif
    return false;
#endif
}

@peterbarker peterbarker self-assigned this Sep 27, 2024
@peterbarker
Copy link
Contributor

Closing as contrary to our preferred early-return pattern.

@peterbarker peterbarker closed this Nov 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants