What is meta? ×
Meta Stack Exchange is where users like you discuss bugs, features, and support issues that affect the software powering all 134 Stack Exchange communities.

I've been going through and editing some answers and questions recently for readability of code sections. I realize there's a number of ways developers like to format the code blocks, especially when it comes to the use of brackets (i.e. {..}). I've read a number of coding standards throughout my career and it seems that - in general - most prefer to avoid the need to use horizontal scroll bars as it becomes difficult to follow.

Is there a rule of thumb for editing out horizontal scroll bars? I don't want to be stepping on toes if someone feels their answer was "more readable" as they posted it. Here's an example:

var someVariable = SomeMethodWithLotsOfArguments(argumentNumberOne, someObject.AndItsProperty, lastArgument);

vs.

var someVariable = SomeMethodWithLotsOfArguments(
    argumentNumberOne, someObject.AndItsProperty, lastArgument
);

Or the use of property initializers (in C#):

var someNewObject = new SomeObject { PropertyA = someValue, AnotherProperty = anObject.WithProperty };

vs.

var someNewObject = new SomeObject {
    PropertyA = someValue,
    AnotherProperty = anObject.WithProperty
};

In my humble opinion, the latter in each example is more readable and so I would make the edit for clarity.

EDIT: Found Why do people not split long lines when posting code here? after posting, which is similar but doesn't address whether there is a guideline to follow.

share|improve this question
2  
As an aside: the mobile websites wrap lines rather than overflow them with scrolling. No matter how hard one tries: that can look quite horrible then. –  Arjan Aug 26 '11 at 19:07
1  
@Arjan: Thanks for mentioning. Mobile is always going to be its own animal, I think. And most people will probably expect some display differences / issues to a degree. –  Yuck Aug 26 '11 at 19:10
2  
I'm for improving the readability of code samples, however be careful when the language in question is sensitive to indentation. It's easy to accidentally break the code. –  hammar Aug 26 '11 at 19:14
1  
@hammar: Good point. As a disclaimer I should have mentioned that I work primarily with C# and SQL, neither of which are sensitive to white space. –  Yuck Aug 26 '11 at 19:15

1 Answer 1

up vote 15 down vote accepted

If you are familiar with the language, and can split lines without breaking the code,

DO SO!

In fact, if you can edit out nonsense that isn't relevant to the situation...

from this:

<DataGrid Name="ThisNameHasNothingToDoWithTheQuestion" RenderTransform="{StaticResource ThisQuestionIsntAboutTransforms}" Click="ClickWhatThisQuestionIsAboutBinding" ItemsSource="{Binding ThisDoesntWork}">
    <!-- fifteen million more lines of XAML not shown in this meta example -->

to this:

<DataGrid ItemsSource="{Binding ThisDoesntWork}" />

DO THAT AS WELL!

I firmly believe that if your code is so wide or so long that scrollbars appear, your potential audience is likely to lose interest.

The best questions have the minimum amount of code needed to illustrate the problem. They are clean, neat and easy to read.

If I have to scroll through a ton of code to figure out just where the code you're talking about is, I'm probably going to bail on the question. Its just not worth my time to dig through code the OP was too lazy to clean up!

If they don't care, why should I?

By editing their code and presenting it as easily viewed and read chunks of code, you are only helping them get their question answered. And isn't that the point of editing?

share|improve this answer
5  
+1. But pay attention to the first sentence - don't do such editing if you don't know the language in question. –  Paŭlo Ebermann Aug 26 '11 at 20:36
    
By editing their code and presenting it as easily viewed and read chunks of code, you are helping future visitors read and understand the question (FIFY :) ) –  BryanH Feb 5 '13 at 16:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .