0

I am new to coding and started off with C. I am trying to recreate the famous Snake game and currently using the betty style of coding. Apparently global variables are not allowed and so far What I have thought of is passing my variables as function parameters but I'm not into it as it seems a rather bad coding practice. How do I go about it?

I declared my variables that are used in diffrent source files as global variables in a header files. so far its working but betty does not allow the use of global variables

Johnny
  • 13
  • 2
  • If your global variables are constants, you could `#define` them. Otherwise, you could have a function like this: `int* globalValue(void) { static int value = 0; return &value; } `, however that's probably just even worse style, but might be accepted by betty. Or you could save them to a file and read/write to it all the time. – Joel Jun 02 '23 at 08:51
  • 1
    Do these global variables represent the game state and nothing else? If that is the case, then you could create a `struct game_state` that consists of all of the variables that represent the game state. You can then create one instance of this `struct` in your function `main` and pass a pointer to that `struct` to all functions that require it. – Andreas Wenzel Jun 02 '23 at 09:24
  • I have actually defined them as struct and it works perfectly. Thank you . – Johnny Jun 02 '23 at 09:33
  • " but I'm not into it as it seems a rather bad coding practice" What makes you think that, out of the blue? It's generally considered good practice. The key however is that each code module shouldn't concern itself with anything but its designated task. – Lundin Jun 02 '23 at 13:52
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community Jun 02 '23 at 16:27
  • I was kind of reluctant passing variables as parameters in a function and wasn't ready to pass 4 variables as function members, it felt restraining. That is why I thought of it as a bad code practice. From your pov, I take back my words. – Johnny Jun 05 '23 at 11:01

1 Answers1

1

Pure functions, that is functions which result only depends on it's arguments (and the same input result in the same output), leads to a program that is easy to reason about and by extension test. This is a good thing to strive towards. It's usually considered good style to keep the number of arguments reasonable. You do that by decomposing large functions into smaller ones that does one thing, and by grouping related variables into struct(s) that you can easily pass around:

enum direction { UP, RIGHT, DOWN, LEFT };

struct position {
   unsigned x;
   unsigned y;
};

struct state {
  enum direction dir;
  struct position pos;
};

void update(struct state *s) {
   if(s->dir == UP)
        s->pos.y--;
   // ...
}

Another option might be to make your global variables static so they can only be accessed in a given file.

If you are the chaotic evil type then what is worse than global variables is external state: files, database or external services in general. The life time of your data now exceeds the program, and your data may be accessed by other programs possible concurrently with your program.

Allan Wind
  • 23,068
  • 5
  • 28
  • 38