is it good or bad to reuse the variables?

后端 未结 9 1988
灰色年华
灰色年华 2020-12-17 15:03

I wonder if it is good or bad (or does not matter) if I reuse the variable names as much as possible? for example

int main(void){
  //...
  int x=0;

  //..
         


        
相关标签:
9条回答
  • 2020-12-17 15:17

    In general it's very poor practice to reuse variable names for different purposes - if someone else needs to maintain your code later on the person will have to find these "context switches" in your code where x now suddenly means something other than what it meant before that line of code.

    You may get some memory savings but that's so small compared to the problems it introduces that it's advised against. (Read the edit below, too.)

    Typically it's also recommended not to use 1-character variable names for other than loop counters. One could argue that x could also be an X coordinate but I'd use some prefix or a longer name in that case. Single-letter variable names are too short to give meaningful hints about the purpose of a variable.

    Edit: as several comments (and some of the other answers) pointed out, the potential memory savings (if any) depend on how good the compiler is. Well-written optimizing compilers may realize that two variables have no overlapping lifetimes so they only allocate one variable slot anyway. The end result would be no run-time gain and still less maintainable source code. This just reinforces the argument: don't reuse variables.

    0 讨论(0)
  • 2020-12-17 15:18

    It is better to reuse variables in terms of memory. But be careful you don't need the value in a variable before reusing it. Other than that, you shouldn't use always the variable. It is important to keep a clean and readable code. So I advice you to choose different variable with names depending on the context so that your code don't become confusing.

    You should also take a look at dynamic memory allocation in C, which is very useful to manage memory and variables.

    https://en.wikipedia.org/wiki/C_dynamic_memory_allocation

    0 讨论(0)
  • 2020-12-17 15:19

    You can re-use it but i don't think it will bring a any significant benefit to your programm and it will make your code less readable.

    0 讨论(0)
  • 2020-12-17 15:24
      int x=0;
    
      //..
      x = atoi(char_var);
    
      //..
      int x = 0;
    

    You cannot redeclare x in the same scope. If you are not redeclaring it but using it for different purposes, you are free to do this. But it's a bad practice and should be avoided as it decreases code readability. Also you should find meaningful names for your variables for the same reasons.

    0 讨论(0)
  • 2020-12-17 15:24

    In general for any language, if you reuse variable names, and then you decide to refactor part of your code into another method, you end up having to add or edit declarations.

    int i;
    for(i = 0; i < 10; ++i) {
        printf("%d\t%d\n", i , i * i);
    }
    for(i = 0; i < 10; ++i) {
        printf("%d\t%d\n", i , i * i * i);
    }
    

    Suppose you take the second loop and move it to a print_cubes method. You will not be able to just cut and paste the for loop, as i will have no declaration there. A good IDE might be able to insert the declaration, but it might worry about the side-effects on i in the code you've typed.

    In general, compilers can consolidate used variables by what are called graph-coloring algorithms. Consider this variant:

    for(int i = 0; i < 10; ++i) {  // BLOCK 1
        printf("%d\t%d\n", i , i * i);
    } // END BLOCK 1
    for(int j = 0; j < 10; ++j) { // BLOCK 2
        printf("%d\t%d\n", j , j * j * j);
    } // END BLOCK 2
    

    The compiler lists the used variables: i, j. It lists the blocks being used: BLOCK 1, BLOCK 2. The parent function is also a block, but i and j are visible only in BLOCK 1 and BLOCK 2. So, it makes a graph of the variables, and connects them only if they are visible in the same block. It then tries to compute the minimum number of colors needed to color each vertex without giving two adjacent vertices the same color, similar to the Haken-Appel Four Color Theorem. Here; only one color is needed.

    0 讨论(0)
  • 2020-12-17 15:26

    Bad. For simple types like ints, passed by value, the compiler will be able to figure out when they are unneeded and reuse the space.

    For example, I compiled the following C++ code in Visual Studio 2010 using 32-bit Release mode:

    for (int i = 0; i < 4; ++i)
    {
        printf("%d\n", i);
    }
    
    for (int j = 0; j < 4; ++j)
    {
        printf("%d\n", j);
    }
    

    and got the following assembler output:

    ; 5    :    for (int i = 0; i < 4; ++i)
    
        mov edi, DWORD PTR __imp__printf
        xor esi, esi
        npad    6
    $LL6@main:
    
    ; 6    :    {
    ; 7    :        printf("%d\n", i);
    
        push    esi
        push    OFFSET ??_C@_03PMGGPEJJ@?$CFd?6?$AA@
        call    edi
        inc esi
        add esp, 8
        cmp esi, 4
        jl  SHORT $LL6@main
    
    ; 8    :    }
    ; 9    : 
    ; 10   :    for (int j = 0; j < 4; ++j)
    
        xor esi, esi
    $LL3@main:
    
    ; 11   :    {
    ; 12   :        printf("%d\n", j);
    
        push    esi
        push    OFFSET ??_C@_03PMGGPEJJ@?$CFd?6?$AA@
        call    edi
        inc esi
        add esp, 8
        cmp esi, 4
        jl  SHORT $LL3@main
    
    ; 13   :    }
    

    You can see that the compiler is using the esi register for both i and j.

    0 讨论(0)
提交回复
热议问题