Skip to content
Discussion options

You must be logged in to vote

This is a nice demonstration of using function pointers to replace large switch statements or deeply nested if chains. I like how the menu entries are data-driven rather than hardcoded into the control flow.

That said, I think there are a few improvements that could make the implementation more scalable and easier to maintain.

1. Make the menu size dynamic

Instead of hardcoding the number 4 in the loop, compute it automatically. This allows adding new menu entries without modifying the search logic.

struct menu {
    const char *key;
    void (*handler)(void);
};

struct menu entries[] = {
    {"a", array},
    {"b", string},
    {"c", ifStatement},
    {"d", elseIf},
};

const size_t men…

Replies: 1 comment

This comment was marked as low quality.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question Ask and answer questions about GitHub features and usage Programming Help Discussions around programming languages, open source and software development source:ui Discussions created via Community GitHub templates
2 participants