Search code examples
language-agnosticrefactoringconventions

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


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

e.g.

private String var1, var2, var3

instead of:

private String var1;
private String var2;
private String var3;

Solution

  • I think that there are various reasons, but they all boil down to that the first is just less readable and more prone to failure because a single line is doing more than one thing.

    And all that for no real gain, and don't you tell me you find two lines of saved space is a real gain.

    It's a similar thing to what happens when you have

    if ((foo = some_function()) == 0) {
        //do something
    }
    

    Of course this example is much worse than yours.