Is it looked down upon or bad to write multiple variable declarations on the same line? As in:

    boolean playMoreGames = true;           int length;
    boolean win;                            int bodyparts;
    boolean contains;                       char blank = '-';
    String word;                            Scanner fileIn;
    String guess;                           Scanner keyboard = new Scanner(System.in);

vs (standard):

    boolean playMoreGames = true;           
    boolean win;                        
    boolean contains;   
    int length;
    int bodyparts;
    char blank = '-';       
    String word;                            
    String guess;                           
    StringBuilder sb_word;
    Scanner fileIn;
    Scanner keyboard = new Scanner(System.in);
| improve this question | | | | |
  • I haven't seen any auto formatter that will do this though – ratchet freak Jun 8 '13 at 15:44
  • It is quite a relieving habit. If you have OCD I mean... – Piovezan Jun 8 '13 at 16:31

I really like the readability of that style, and might use it in a publication, but a good programming style should also be easily editable. It would be very annoying to insert or delete a declaration in the middle of the list, especially if you're accustomed to being able to do so using your editor's linewise shortcuts, like vim's dd.

The other reason people generally stick to one statement per line is that compilers report errors by line number and version control diffs and merges by line. While it's not that hard to deal with, it makes interacting with your tools that much more convenient.

| improve this answer | | | | |
  • I thought of the debugging issue, too, but, at least in this case, there isn't anything there that can actually fail, so...I would probably only use this style for variable declarations and (mostly) primitive type initializations. I didn't really consider the code maintenance side of the issue. This style probably wouldn't fly in industry because of that, but it's so much nicer to look at... – Steve P. Jun 8 '13 at 16:32
  • There's always the possibility of a typo, or forgetting an import or something. – Karl Bielefeldt Jun 8 '13 at 16:37

Style is always different everywhere. I've never seen this style that I can recall, but that doesn't matter.

I think it might be seen as pretty strange by many but again, it's subjective and really doesn't matter so long as your style doesn't get in your (or anyone elses) way.

Though I would think this style might get in your way, but if it doesn't, and you like it, have a ball.

Bad design is looked down upon, style is looked at mostly with curiosity. Though if you're in a team, style that isn't uniform with the others is (rightly) looked down upon.

The only other thing to note about style is that while it's subjective and everyone's may be different outside of collaborative works, there are often common subsets of style among language communities. While there's no necessity to follow any of these common styles, there can be benefit in that your code will be easier to digest to more people. As many people are used to seeing style X, if you code in style X, more developers will be able to understand and work with your code if you ever share it.

| improve this answer | | | | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.