• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Statements on multiple lines

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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



Out of a cannon, into a volcano? I agree!
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for all the answers!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic