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.

Many times I enter code into SO questions and answers (obviously!) that is only a little bit longer than the threshold that will activate the right-side scroll bar.

This makes it rather annoying that you can't see all the code at a glance despite the fact that it fits inside the browser window just fine.

To get around this, I have been putting   lines in the middle of the code to ensure they're treated as independent code blocks but:

  • it inserts an unnatural looking break in the code.
  • it's a pain trying to find a good "break" point, especially if it's just one function that's two lines too long.

Is there a possibility that we could get some leeway on the scrollbar kicking in? Say, if it's only three lines too long, don't put up the scrollbar.

I'm not sure as to what calculations are done to activate this scrollbar so there may be a solution that's already available to me but unknown, in which case feel free to let me know.

share|improve this question
    
As an aside: that workaround is bad, I feel. (If only as posts are not only used on the original sites, and are not only shown in regular web browsers.) –  Arjan Nov 4 '11 at 13:13

2 Answers 2

up vote 4 down vote accepted

  Method

Would this answer be an example of this technique? An excerpt from this answer is:


    // Some more code above
    return ptr;
}

 

int main (void) {
    // Some more code below

the source for which is:

        return ptr;
    }
 

    int main (void) {

There are two lines between the blocks, which is visually distracting.

HTML Comment Method

If you're set on breaking up your code blocks to avoid scrollbars, and don't want text between the blocks, I suggest using an HTML comment. This method puts the blocks much closer together (there's less than one line of blank white space between them):


    // Some more code above
    return ptr;
}
int main (void) {
    // Some more code below

the source for which is:

        return ptr;
    }
<!-- An HTML comment -->

    int main (void) {

I've also used this before to break up quoted comment exchanges. Note that it's probably not a defined behavior for Markdown, I discovered it accidentally. Of course, breaking up code blocks based on finding &nbsp; between them is probably also not defined; they don't break on other whitespace and that is rendered as a whitespace character.

The best method:

The scrollbars are there for a reason. Long code blocks without explanatory text are problematic. Put some explanatory text in between the blocks, like this:


    // Some more code above
    return ptr;
}

This function returns a pointer to an array containing the desired numbers. Here's an example of how to call it. Note that you have to call free on the returned array if you successfully allocate the memory! While it looks like this is allocating memory on the stack at a glance, the memory is actually on the heap.

int main (void) {
    // Some more code below

the source for the above should be pretty obvious.

share|improve this answer
    
Actually, the intervening text method makes far more sense since it inlines the explanations with the code - this both stops the scrollbars (mostly, unless a single code block being explained is still too long, but I can live with that) and makes the answer easier to read. I think I'll be doing that from now on. –  paxdiablo Nov 9 '11 at 1:50

This makes no sense to me. Assume for example that the maximum height is 25 lines. You're suggesting a tolerance of three lines, meaning you don't get scrollbars before 28 lines -- that's like just increasing the maximum to 28 in the first place. Or possibly -- depending on how precisely you suggest to do this -- making a 29-line codeblock (with scrollbars) smaller than a 28-line codeblock (without scrollbars).

share|improve this answer

You must log in to answer this question.

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