Is it good to use global variables for greater readability in the code?

Asked

Viewed 400 times

10

I am implementing an exercise of the Deitel book, How to Program C, 6th edition, the Logo problem in chapter 6. It was an interesting question with legal logical problems etc. The only question of implementation may seem very primary, but it really left me thinking. I must put global variables in exchange for readable code?

The examples below demonstrate the doubt, which still uses another global variable matrix, which unfortunately can not be local. I know it is a beginner doubt, but I really do not know how to proceed in this case. Below exemplify my doubt:

Note: As the code got a little big posted a "sketch"

Example with enum global:

#include <stdio.h> 
#define SIZE 25

/*variável global necessária para que as funções escrevam na matriz */
char matrix[SIZE][SIZE]; 

/* Aqui enum é global podendo ser utilizado nas funções*/
enum direction {DOWN, RIGHT, UP, LEFT};

..."Protótips de funções" ... 

int main(void){

..."Aqui utiliza-se DOWN, RIGHT , UP, LEFT" ...

 return 0;
 }

 void funcaoExemplo(int var){

     if(var == DOWN) ...  

     ... "e utilizam-se também as outras variáveis de enum"
     ... "como RIGTH , UP e LEFT."
 }

The second case is with Enum within the main function: Example with enum local:

#include <stdio.h> 
#define SIZE 25

/*variável global necessária para que as funções escrevam na matriz */
char matrix[SIZE][SIZE]; 

..."Protótips de funções" ... 

int main(void){

/* Aqui enum é local podendo ser utilizado somente em main*/
enum direction {DOWN, RIGHT, UP, LEFT};

..."Aqui utiliza-se DOWN, RIGHT , UP, LEFT" ...

 return 0;
 }

 void funcaoExemplo(int var){

     if(var == 0) ... /* equivalendo a DOWN */ 

     ... "aqui se utilizam os valores correspondentes" 
     ... "como 1 , 2 e 3 no lugar de RIGTH , UP e LEFT respectivamente"
 }

How should I proceed?

  1. I put that enum in global scope as in the first example?
  2. I do as in the second example declaring a enum only in main, and in the functions that use it I substitute for their integer values?
  3. The insert enum in main and in all functions using it?
  4. I don’t use enum, and only enforce their respective integers?

1 answer

11


Generally the opposite, global variables negatively affect readability. Although it may seem otherwise for beginners mainly seeing very limited examples.

Examples of books and tutorials usually do not care much about the readability of the code, because the code is usually very small and in these cases the readability is not so affected. But they breed a bad habit.

  1. Enumerations are not variables, they are statements of data structures, so they should be global even. Unless they are only used locally - which is very rare. Note that an enumeration has members and no variables. These members are symbols and vary nothing. At most they can be called constants.

  2. It doesn’t. It might make sense.

  3. Does not enter locally in any function if the use is global.

  4. The enum will always be recommended in these cases. Do not use magic numbers.

There is nothing to prevent the use of the variable matrix as local. This yes should no longer be global. It should be passed as a parameter between functions.

There is another problem with this approach. If the book teaches like this, it is teaching this wrong and will probably teach other things wrong. So get ready to learn wrong.

O C makes casting automatic in many situations and does not complain but the correct thing would be to compare two exactly equal types. That is, you should not compare an integer with an enumeration (it works because the enumeration drops to an integer automatically) but these variables var should be the direction. It may seem like it makes no difference but readability requires you to pass the right intention.

#include <stdio.h> 

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

void funcaoExemplo(enum direction var) {
    if(var == DOWN) printf("%d", RIGHT); //só para testar
}

int main(void){
    funcaoExemplo(DOWN);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • I really didn’t know I could pass enumerations for functions. I’ll try to take matrix from the global scope and pass it as a parameter to functions, since integer matrices are passed by reference should be easy to implement.

  • You can pass any kind of dice. Deep down you are passing one int. I’m not saying you shouldn’t use ma on C an enumeration has little real advantage. It makes the code more readable but the language doesn’t help to use it in a better way. In C++ and other more modern languages the enumeration is actually typed and the compiler can detect errors.

  • I actually took the enumeration out of the code, because having to treat it as you describe it, I would have to make significant changes, because I was literally accounting for the values of each one, because there is a function that "rotates" such values to change direction. The only problem is that the enumeration gave greater readability to the code, but I can live with that.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.