Why do you not declare several variables of the same type on the same line?

后端 未结 16 2207
梦谈多话
梦谈多话 2020-12-06 05:41

Why is it bad practice to declare variables on one line?

e.g.

private String var1, var2, var3

instead of:

private          


        
相关标签:
16条回答
  • 2020-12-06 05:44

    Here's my reasons:

    • Readability, easier to spot if you know there's only one on each line
    • Version control, less intra-line changes, more single-line additions, changes, or deletions, easier to merge from one branch to another
    0 讨论(0)
  • 2020-12-06 05:45

    In my opinion, the main goal of having each variable on a separate line would be to facilitate the job of Version Control tools.

    If several variables are on the same line you risk having conflicts for unrelated modifications by different developers.

    0 讨论(0)
  • 2020-12-06 05:45

    while attempting this question https://www.interviewbit.com/problems/remove-element-from-array/

    Method 1 gives Memory Limit exceeded for this code:

    Type 1:

    int i,j;
    

    Type 2:

    int i;
    int j;
    

    type 1: Gives Memory Limit Exceeded

    int removeElement  (int* A, int n1, int B) 
    {
        int k=0, i;
        for(i=0;i<n1;i++)
            if(A[i]!=B)
            {
                A[k]=A[i];
                k++;
            }    
        return k;
    }
    

    Whereas type 2 works perfectly fine

    int removeElement  (int* A, int n1, int B) 
    {
        int k=0;
        int i;
        for(i=0;i<n1;i++)
            if(A[i]!=B)
            {
                A[k]=A[i];
                k++;
            }    
        return k;
    }
    
    0 讨论(0)
  • 2020-12-06 05:46

    What about the case such as:

    public static final int NORTH = 0,
                            EAST = 1,
                            SOUTH = 2,
                            WEST = 3;
    

    Is that considered bad practice as well? I would consider that okay as it counters some of the points previously made:

    • they would all definitely be the same type (in my statically typed Java-world)
    • comments can be added for each
    • if you have to change the type for one, you probably have to do it for all, and all four can be done in one change

    So in an (albeit smelly code) example, is there reasons you wouldn't do that?

    0 讨论(0)
  • 2020-12-06 05:49

    In C++ :

    int * i, j;
    

    i is of type int *, j is of type int. The distinction is too easily missed.

    Besides having them on one line each makes it easier to add some comments later

    0 讨论(0)
  • 2020-12-06 05:51

    In C/C++, you also have the problem that the * used to indicate a pointer type only applies to the directly following identifier. So a rather common mistake of inexperienced developers is to write

    int* var1, var2, var3;
    

    and expecting all three variables to be of type 'int pointer', whereas for the compiler this reads as

    int* var1;
    int var2;
    int var3;
    

    making only var1 a pointer.

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