Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 693535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:45:50+00:00 2026-05-14T02:45:50+00:00

I have a C program which uses Lua for scripting. In order to keep

  • 0

I have a C program which uses Lua for scripting. In order to keep readability and avoid importing several constants within the individual Lua states, I condense a large amount of functions within a simple call (such as “ObjectSet(id, “ANGLE”, 45)”), by using an “action” string.

To do this I have a large if tree comparing the action string to a list (such as “if(stringcompare(action, “ANGLE”) … else if (stringcompare(action, “X”)… etc”)

This approach works well, and within the program it’s not really slow, and is fairly quick to add a new action. But I kind of feel perfectionist. Is there a better way to do this in C?

And having Lua in heavy use, maybe there is a way to use it for this purpose? (embedded “chunks” making a dictionary?) Although this part is mostly curiosity.

Edit: I should note I am not using C++

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T02:45:50+00:00Added an answer on May 14, 2026 at 2:45 am

    It’s hard to say exactly what might be better, since you’re not very clear about what your constraints are. Other answers show what you could do if you were willing to export more symbols into Lua or delegate more work to Lua. My answer addresses a narrow question: how could you refactor your C code without changing the way you interact with Lua? I propose you make your code table-driven.

    Here’s a design sketch:

    typedef struct action {
        const char *name;
        int (*doit)(lua_State *L, int idx);
    } *Action;
    
    static Action my_actions[] = {
      { "angle", set_angle },
      { "color", set_color },
      ...
      { NULL, NULL }
    };
    
    int i_replace_nest_of_ifs(lua_State *L) {
      const char *action = luaL_checkstring(L, 1);
      for (int i = 0; my_actions[i].name && strcmp(my_actions[i].name, action); i++)
        ;
      if (my_actions[i].name) 
        return my_actions[i].doit(L, 2);
      else
        return luaL_error("Asked for unknown action '%s'", action);
    }
    

    If the linear search through actions gets too expensive, you can sort by name when you open the library, then call bsearch.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 497k
  • Answers 497k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer No, the compiler has it right. :) The parameter l… May 16, 2026 at 11:56 am
  • Editorial Team
    Editorial Team added an answer Most apps solve this problem by adding a button somewhere… May 16, 2026 at 11:56 am
  • Editorial Team
    Editorial Team added an answer You could just pass the ID in the selector for… May 16, 2026 at 11:56 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have a VB6 program which uses comctrl32.ocx to display a progress bar. I've
I have written a program which uses a SQL exception class and then use
I have a desktop program which uses an embedded data base mechanism. For the
Suppose i have a software program which uses an inefficient data structure. Now how
Say, if I have a Lisp program, which uses (eval 'sym) and looks it
I have a program which needs to upgrade any Access (Jet) database it opens
I have a program with which dreamlax worked a lot with me on, which
I have a program in which I want to be able to store certain
A hypothetical question: Is it possible to have a C++ program, which includes preprocessor
I have a program written in Ruby and C. The C portion is a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.