views:

153

answers:

6

This is a subjective question as I want to gauge if it's worth me moaning at my co-workers for doing something which I find utterly detestable.

The issue is that a bunch of my co-workers will truncate method calls to fit a width. We all use widescreen laptops that can handle large resolutions (mine is 1920x1200) and when it comes to debugging and reading code I find it much easier to read one line method calls as opposed to multiple line calls.

Here's an example of a method (how I would like it):

IReallyLongInterfaceName instanceOfInterfaceName = OurContainer.retrieveClass(IReallyLongInterfaceName.class, param1, param2, param3);

(I do hate really long interface/class names as well :)

It seems that this doesn't render well on StackOverflow, but I think most of you know what I mean. Anyway, some of the other devs do the following.

IReallyLongInterfaceName instanceOfInterfaceName = OurContainer.retrieveClass(IReallyLongInterfaceName.class, 
                                                                              param1, 
                                                                              param2, 
                                                                              param3);

Which is the easier to read at the end of the day for you and would I be unreasonable in asking them to use the first of the two (as it is part of our standard)?

+2  A: 

Maybe you should have as part of your standard build process some sort of checkstyle plugin which checks for exactly that kind of thing? If you've agreed the standard with your co-workers it seems reasonable to ask them to keep to it.

I personally find the second of the two options the more readable, but that's just because I don't have a widescreen monitor ;)

Phill Sacre
+2  A: 

If its exlicitly stated in the companies coding standard that method one is the correct method then by all means moan at them, after all they are not adhering to the company standards. If its not exlicitly stated then I guess now would be a good time to get it into the standard. One thing to be aware of though, if you are using an IDE with autoformatting is that it may take it upon itself to reformat the methods to style 2 when its run. So even if everyone is writing to style 1, it may not end up looking like that when they are finished with it.

and like Phil, I find method 2 much more readable, since you can see everything you need to see without having to scroll your eyes sideways :)

+3  A: 

I find the first example more readable in general, though if it is longer than some predefined limit (120 characters, for me), I would break the lines:

IReallyLongInterfaceName instanceOfInterfaceName =
        OurContainer.retrieveClass(IReallyLongInterfaceName.class,
                                   param1, param2, param3);
Avi
This gets my vote, pretty much spot on. The real question then becomes, at what point to you force the line break. At the 80 char limit, at the operator, and the function opening paren, etc. Out of all those I use the operator break the most, like this answer.
Josh
+2  A: 

I prefer the second example. Even though you may have widescreen laptops, you might not always have windows full screen, or in your IDE you may have a lot of other panels around the main coding area that reduce the available width for displaying code.

If the line can't fit without scrolling, then vertical scrolling is preferable to horizontal scrolling. Since we read left-to-right, horizontal scrolling would mean moving backwards and forwards all the time.

I prefer one parameter per line to Avi's suggestion, which is arbitrary to me. If you spread the parameters over multiple lines but have several on each line, it makes it more difficult to find particular parameters when reading the code.

Dan Dyer
You probably mean left-to-right? At least for European languages, and code.
Douglas Leeder
Doh! Fixed, thanks.
Dan Dyer
Wide lines are a #@$# in most diff'ing tools as well, which can make code reviews a pain.
Frank Schwieterman
A: 

I prefer option #2, as well. The issue isn't just how it looks on screen (and if I had 1920 horizontal pixels, I'd have a lot more docked windows), it's how it looks if I need to print it and read it. Long lines will print terribly out of most IDEs, whereas lines broken by an author with the intent to improve legibility will print well.

Another point is general legibility. There's a reason magazines and newspapers are printed in columns -- generally, readability of text (particularly text on-screen) is improved by shorter lines and better layout/formatting.

I think 80 might be overly arbitrary, but I'm using 10pt Consolas, and I seem to be able to get about 100 characters per line on a standard 8.5" printed page.

Now at the end of the day, this is a holy war. Maybe not as bad as where to put your curly braces, but it's up there. I've given you my preference, but the real question goes back to you: What's your company's standard? It sounds to me like they've standardized on option #2, which means for the sake of the team, you should probably adapt to them.

John Rudy
A: 

I prefer option 2, but optionally with comments for parameters where the variable name is not obvious. When you have a function call that is asking for a bunch of parameters, it can be pretty hard for reviewers to tell what the code is doing.

So, I generally code like this if there are more than 3 parameters to a given function:

applyEncryptionParameters(key,
                          certificate,
                          0, // strength - set to 0 to accept default for platform
                          algorithm);
Kevin Day