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

8

I am implementing an exercise in 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 doubt of implementation may seem very primary, but it really made me thoughtful. Should I put global variables by exchanging a readable code?

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

Note: As the code got a bit large I posted a "sketch"

Example with% 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 inside 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?

  • I put this enum in global scope as in the first example?
  • I do as in the second example declaring a enum only in main, and in the functions that use it do I substitute for its integer values?
  • Do I enter enum in enum and in all functions that use it?
  • I do not use main , and only enforce their respective integers?
  • asked by anonymous 11.02.2015 / 19:52

    1 answer

    8

    Typically the opposite, global variables negatively affect readability. While it may seem the reverse for beginners especially seeing very limited examples.

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

  • Enumerations are not variables, they are declarations of data structures, so they must be global. Unless they're only used locally - which is pretty rare. Note that an enumeration has members and not variables. These members are symbols and do not vary at all. At most they can be called constants.

  • It does not. You can even do it but it does not make sense.

  • Do not insert locally into any function if use is global.

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

  • There is nothing to prevent the use of matrix as a local variable. This 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 wrongly and it will probably teach other things in the wrong way. So get ready to learn wrong.

    C does casting automatically in many situations and does not complain but the correct one would be to compare two types exactly the same. That is, you should not compare an integer with an enumeration (it works because the enumeration falls to an integer automatically) but these var variable should be of type direction . It may seem like it does not make a difference, but legibility requires you to pass on 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);
        return 0;
    }
    

    See the correct example working on ideone .

        
    11.02.2015 / 20:13