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

后端 未结 16 2226
梦谈多话
梦谈多话 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: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.

提交回复
热议问题