This question already has an answer here:

Lately, I've seen lots of homework questions or of "first postings" for more professional help with really poor editing quality.

This is understandable and usually you can let it go and rework it yourself, or still make sense of the code. But some take it to a level of rudeness that's quite puzzling and would require lots of effort on the editors' part.

I was wondering if the following approach could be interesting:

When a question is posted, detect the first "language tag" (c, java, c#... ) in the list. If one is found, assume this indentation for the code snippets.

This could be applied to the whole thread with decent results, but to be conservative maybe this could only apply to the question only, as it's very likely to be the intended effect. Alternatively, it could be done rather protectively and only done for the most active tags: c, java, javascript, ecmascript, ruby, etc... and leave out esoteric languages.

While not perfect, this looks like a minimal effort thing for posters, and a good default behavior for a vast majority of cases.

I feel this must have come up in some form or another before, and I see some question suggestions that relate to this but not exact duplicates. Feel free to correct me and point me to them if you find them.

Other alternatives:

  • Offering to indent with a checkbox (we detecting you seem to use language "x", do you want us to indent that for you or proactively we indented your code as it seemed to "x". Uncheck this if you don't want us to do that.).
  • Offering a short-hand syntax like in many wikis and bug trackers to specify the language (a sort of pragma-thing, like #lang java). That doesn't struck me as a great thing for questions as posters wouldn't be aware of that. However, it could be nice for answers.

And make sure that the backend actually rewrites the code, so it gets formatted nicely even in the saved question/answer source text.

marked as duplicate by Nicol Bolas, Lucifer, hims056, animuson, Lance Roberts Jun 10 '13 at 6:05

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 4
    And your parser for finding and auto-indenting code is....? – AndrewC Jun 9 '13 at 20:51
  • @hayden: thanks, indeed related. I think I'd be in favor of a solution that's not intrusive. But maybe we should federate these meta questions and answers. – haylem Jun 9 '13 at 20:52
  • @AndrewC: who said I wanted to auto-detect it? I suggest to use the first known language tag for finding the language type. For the rest, you have code blocks to find them in the page. Of course, if the poster doesn't even use that, you can't do much (well you could, but then that would be a more complicated solution, like what you allude to). – haylem Jun 9 '13 at 20:53
  • @AndrewC: Also, there are many automated code highlighting and beautifuing frameworks, both for server- and client-side. In fact, SO already automatically highlights code (more or less correctly), and it could add indentation as well, though it's a more destructive change. It's not like there aren't any options. – haylem Jun 9 '13 at 20:55
  • 5
    I guess one of the problems is, by the nature of SO, that often code isn't valid. – hayd Jun 9 '13 at 20:57
  • Sorry I misunderstood you. You mean impose some style guidelines for indentation of code when it's already got four spaces in front of it, whereas I thought you meant automatically add the four spaces. – AndrewC Jun 9 '13 at 20:58
  • 3
    SO-imposed indentation style? Surely we'd just get a holy war over the placement of { and }. – AndrewC Jun 9 '13 at 20:59
  • 4
    That's never going to work properly for Python. – user201929 Jun 9 '13 at 21:00
  • Yes, you'd have to automatically turn it off at the sight of a language flag for any whitespace-sensitive language, where this would break code, or indeed automagically corecting an error. And presumably you'd need to keep a copy of the original on the server to cope when there's been mistagging. – AndrewC Jun 9 '13 at 21:01
  • 5
    There's no shortage of humans willing to do this instead. Add a flag for poor indentation and a review queue for it and you'll have 100 2k rep users begging for a chance to get it edited before anyone else. ;) – AndrewC Jun 9 '13 at 21:05
  • @hayden: that's the point that worries me more than the detection, yes :) We could try an empirical test on a subset of questions already asked to see how that works out. – haylem Jun 9 '13 at 21:13
  • @AndrewC: Surely we'd just get a holy war over the placement of { and } Note that it's why I refrained from offering "automated formatting" (though I then made a mention of the gofmt stuff). I think simple indentation could already be decent enough, and less polarizing. I see people complain about posters' crappily indented snippets in questions, not so much being petty about their styles. (maybe I should have refrained from mentioning that in my question, indeed). – haylem Jun 9 '13 at 21:15
  • @AndrewC, @ZeroPiraeus: Yes, you'd have to automatically turn it off at the sight of a language flag for any whitespace-sensitive language. Note that in the case of Python (or other space-based languages), that wouldn't make it better or worse than it already is: you can never know if what the OP sends is a result of copy-paste, code that's been mistyped in the form, or bad indentation correction. So that's already an issue anyways. – haylem Jun 9 '13 at 21:17
  • 1
    Re whitespace-sensitive languages: No, it would be worse than it already is. At the moment there's a discrepancy between how (a) SO (b) the OP's editor (c) the compiler (d) the OP and (e) experienced programmers see the indentation of code. We don't need (f) through (j) interpretations of the auto-indenter's code! We'd have two layers of debugging! – AndrewC Jun 9 '13 at 21:26
  • 1
    @AndrewC: thanks for helping me refine that question, by the way. I edited it a bit based on your comments. – haylem Jun 9 '13 at 21:45

Personally, I can see where you're coming from, but this is (potentially) a bit of red herring. Why?:

  • SO-flavoured markdown is actually pretty good as markdown(s) go.
  • SO can't possibly detect the varied, creative and sometimes odd use of code blocks/fragments without a lot of effort.
  • Code is often posted with differing levels of indentation (varied spacing, tabbing).
  • Unless declared in an obvious way, code isn't that trivial to classify.

With experience, usage, willingness (and rep), users fast improve their usage of markdown/code-blocks.

We all see newer users (usually under the guise of userXXXXXXX) who hastily post code and miss either single lines or entire portions of code through inadequate use of indentation or back-ticks - Where this is the case, it's usually pretty obvious that they haven't bothered to click let alone read the Markdown Editing Help link.

Thankfully, there are many users (of all rep levels, including myself) who are more than willing to help improve code display within questions and answers (anyone just needs to peruse the queues to see them!) although, granted, sometimes the edits can be less than ideal in some cases.

Perhaps the most important aspect of my viewpoint for me, is that removing effort from posting will negatively effect the quality of posts too. Remove the effort and the signal to noise ratio will change and may get worse - something I'm sure the community would like to avoid.

  • 1
    I did think of that... if we facilitate posting too much, and posters do not receive criticism anymore for their crap posting, we don't train them to behave correctly. Too much technology may encourage laziness... :) – haylem Jun 10 '13 at 1:14
  • @haylem Too much technology may encourage laziness - Well put and my viewpoint exactly + it provides incentives for lower-rep users to gain rep. Good discussion post either way! – nickhar Jun 10 '13 at 1:18
  • 1
    you know, it's a very common dilemma I face when teaching. You feel like packing as much value in the timeframe you have to get a class of students to do get the best learning experience. So you make their lives as easy as possible to remove all the "menial" tasks that would get in the way of them actually learning the material. Exept that will often 1) get the lazy ones to slack off more 2) prevent the ones who don't know yet they could be great at that to discover it by working. Only the ones already good and hard-working benefit from it. Hard to balance, really. – haylem Jun 10 '13 at 1:24
  • And the larger the group, and more disparate the levels of the students, the harder it is! Which brings on the other sides of the dilemma: a) do you want to touch as many students as possible (even if that means having less impact) b) can you really get anyone on board, and is anyone really meant to (or be able to) learn that stuff (no matter what the stuff is)? Teaching is awesome in so many ways. :) – haylem Jun 10 '13 at 1:24
  • Accepting your answer, as I suppose this was as close an answer I could get. Thanks. – haylem Feb 26 '14 at 12:02
  • @haylem It's a tough call. I know where you're coming from and I know many others do too. – nickhar Mar 5 '14 at 0:57

I would like to suggest a much more modest alternative.

I've noticed* most of the code indentation problems fall under two kinds:

- Most common issue:

      users starting code here


It should be here.

- Second most common issue:

first line is indented
           but everything from the second line on
           has the same offset
               and usually indentation can be inferred from the rest of the code

Both these cases particularly easy enough to fix, especially the first one. I don't see any use case for starting a code block with too much indentation.

This would probably solve the large volume of such indentation problems without being too much work to implement.

The first one in particular would require no programming language understanding at all.

*That's just my anecdotal evidence, I wonder if actual data can be gathered here

  • That's an interesting addition, though I don't think that's the issue that bugs me the most. That's fairly easily blocked (and maybe i'm used to that "defect" as JIRA suffers from a similar bug when pasting XML code in a code section: it never indents the first line for XML, for some reason...). No, the main problem for me are snippets where the indenting is a total illogical mess (which always brings people to complain about it and to yell at the OP to learn to not burn our eyes), or where almost everything is at the 0 column, making it hard to distinguish any structure almost just as badly. – haylem Jun 9 '13 at 22:49

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