Forums Register Login

Statements on multiple lines

+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

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



Out of a cannon, into a volcano? I agree!
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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.)
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
Thanks for all the answers!
Weeds: because mother nature refuses to be your personal bitch. But this tiny ad is willing:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 2347 times.
Similar Threads
How can I create an object based on input
Refactoring a static method for use in a clustered environment(multiple threads)
Context.xml: how to reference parameter inside the same file
Design implementation Doubt Please tell me the best approach
getting null value from getParameter() in Applet
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 01:51:16.