It's not a secret anymore!*
The moose likes Beginning Java and the fly likes Statements on multiple lines Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Statements on multiple lines" Watch "Statements on multiple lines" New topic
Author

Statements on multiple lines

Janis Mittelstaedt
Greenhorn

Joined: Dec 01, 2010
Posts: 12
So a java convention states that a line shouldn't be over 80 characters long...
I already know that when calling a method with many parameters, one can start a new line with each new parameter, like this:


I hope I got it right using a tab.

But what happens when declaring a method that takes many parameters and the 80 characters arent long enough, will it go like this:


or like this?


I reckon both will work, but what's the right way?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

I can't remember the last time I worked in a shop that followed the 80 char standard. Most monitors are wide enough now for 120 or more. I think mine is currently at 120 or 128 or 132. Pick a width that you're comfortable with, or one that your team can agree on.

As for the indenting, there is no one right way. If you can't find it spelled out in the Java coding conventions, just pick one that's reasonable that you like, as above. Most IDEs let you select one of a handful of common conventions.

Make sure you're using space characters, not tab. You can hit the tab key but have it generated spaces. That too is an IDE or editor configuration.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61095
    
  66

Janis Mittelstaedt wrote:So a java convention states that a line shouldn't be over 80 characters long...

Said who? News to me.

In any case, what I do is whatever makes the intent most clear. Mindlessly following rules should not trump the goal of clarity.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61095
    
  66

Jeff Verdegan wrote:Make sure you're using space characters, not tab.

Quoted for emphasis. Putting tabs into code is a firing offense to me.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Bear Bibeault wrote:Putting tabs into code is a firing offense


Out of a cannon, into a volcano? I agree!
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7711
    
  20

Janis Mittelstaedt wrote:So a java convention states that a line shouldn't be over 80 characters long...

It's not just a Java convention, it's a good one for any language; and, unlike my esteemed colleague, I think it's a good one to follow.

And who cares whether it came from punched cards or not? Would you want to read a novel that crammed as many words into a line as it could? I don't think the standards for those have changed much in the last hundred years; and I wouldn't be at all surprised if their limit is around 80 characters (possibly a bit more).

The fact is that our eyes are designed for scanning, so if you give them too much to take in at once, your brain finds it more difficult to process the information; and that leads to mistakes - for you and the reader - and I hope you plan on your programs being read.

Now whether the optimum is 70, 80 90 or 120, I wouldn't know but, old fart that I am, I'd say 120 is on the high side and 70 maybe a bit short; but I'd much rather be reading code that errs on the short side than the long.

And what does it cost you? An extra hit of the 'return' key.

DO IT.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Winston Gutkowski wrote:
The fact is that our eyes are designed for scanning, so if you give them too much to take in at once, your brain finds it more difficult to process the information; and that leads to mistakes - for you and the reader - and I hope you plan on your programs being read.

Now whether the optimum is 70, 80 90 or 120, I wouldn't know but, old fart that I am, I'd say 120 is on the high side and 70 maybe a bit short; but I'd much rather be reading code that errs on the short side than the long.

And what does it cost you? An extra hit of the 'return' key.


My reason for the wider screen has nothing to do with the strain on my pinkie to hit return a few extra times. My IDE will adjust that stuff for me if I set it to do so. It's because I find it harder to read code that's limited to 80 chars per line.

Obviously there's a lot of personal preference involved here, but I find code easier to read when the lines are long than when a given statement or method call wraps. Unlike reading a novel, when I read code I don't usually read the whole thing top-to-bottom, left-to-right, beginning-to-end. I tend to read it in chunks--methods, blocks, loops, etc. I'd rather have a single method call with it's multiple or long-named parameters take up a single wide line than wrap to 2 or 3, especially when there are multiple of those in a row. Of course, there's a limit to that, and for me, that limit lands comfortably in the 120-140 character range.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61095
    
  66

Again, I don't count. I just do what seems to make the most sense and let the chips, er columns, fall where they may.

Looking through some of my code, I don't see any really really long lines -- though there are a few that are likely longer than 80 characters -- so I'm not saying that I routinely code in long lines, I just don't pay attention to column count.

(Even though I am an old coot that used to carry boxes of punch cards around way back when.)
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7711
    
  20

Jeff Verdegan wrote:Obviously there's a lot of personal preference involved here, but I find code easier to read when the lines are long than when a given statement or method call wraps.

Really? I have more problems with statements that have to wrap (OP's method example being a case in point).

Unlike reading a novel, when I read code I don't usually read the whole thing top-to-bottom, left-to-right, beginning-to-end. I tend to read it in chunks--methods, blocks, loops, etc. I'd rather have a single method call with it's multiple or long-named parameters take up a single wide line than wrap to 2 or 3, especially when there are multiple of those in a row.

See above - but given that it might occur (and unfortunately, with things like generics, it often does these days) I don't have any problem with multiple lines (in fact, my Eclipse line limit is 75). Each to his/her own, I guess.

Of course, there's a limit to that, and for me, that limit lands comfortably in the 120-140 character range.

It used to be for me too (132); but not these days (and not here either, if you want to use code tags).

And I say again: do you want your programs to be read?

Winston
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Winston Gutkowski wrote:Each to his/her own, I guess


I think that is the main thing the OP should take from this thread.

And I say again: do you want your programs to be read?


And I say again: Some people find them easier to read without wrapping.
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1012
    
    5

I don't think there is a true right way here. If you are coding on your own, find a style that suits you. What is most important is when working in a team when a standard formatter and styling should be agreed and adopted by all members. Otherwise, you are going to have some frustrating conflicts when committing code to your SCM system.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7711
    
  20

Bear Bibeault wrote:(Even though I am an old coot that used to carry boxes of punch cards around way back when.)

Ya see? In the end it all comes back to those damn punched cards...

Winston
Janis Mittelstaedt
Greenhorn

Joined: Dec 01, 2010
Posts: 12
Thanks for all the answers!
 
 
subject: Statements on multiple lines