262

Code formatting is one of the major contributor to review queue.

I've seen "Tidy Up" buttons in .NET Fiddle and JSFiddle

Button example 1

and

Button example 2

Current Situation

I currently have to go to JSFiddle, paste my code, format it there and then copy it again and come to Stack Overflow, and then paste that in my post.

Possible Solution

Code formatting at most times is language agnostic, as it deals with braces, semi colons, etc..

So can we have a "Tidy Up" button, which can be clicked after selecting the code to be formatted?

I think if this is implemented, we will have awesome looking code!

Even if the original poster's don't use it (though they should be instructed to use), reviewers will have an easy task ahead as all they want to do, can be done in a click of a button.

Though, it might be difficult, it will be a valuable addition to a programming related site and in my opinion, will decrease the edit reason "code formatting" which in turn might reduce the review queue.

So, if anyone has a suggestion, they could post here, which might be implemented by the Stack Exchange team if there is a major consent among us.

I'm not talking about Ctrl + K or {} formatting. I'm talking about the indentation inside code blocks.

I don't intend to say this:

(function foo(){ return "bar"; }) ();

should become to this:

(function foo(){
return "bar";
})
(); // ugly code

but rather, I want the above to become like this:

(function foo() {
    return "bar";
})(); // awesome code
36
  • 75
    alternative: just downvote anyone who uses bad code formatting :-D or have another close reason: "this question was closed because the code was not properly indented."
    – Michael
    Commented Jun 22, 2014 at 10:41
  • 87
    But the button must be automatically disabled when tags indicate the code is Python.
    – Jongware
    Commented Jun 22, 2014 at 11:40
  • 3
    Asked before as well: meta.stackexchange.com/questions/146236/… Commented Jun 22, 2014 at 12:17
  • 45
    Great suggestion, but err... "Syntax formatting at most times is language agnostic"? I guess you could write a formatter that does well for many languages, but to call syntax language-agnostic...
    – Jeroen
    Commented Jun 22, 2014 at 13:16
  • 20
    Language is syntax (well, and semantics. We aren't talking about those right now). Doing this language agnostic is pretty much impossible. Not all languages look like C.
    – Cubic
    Commented Jun 22, 2014 at 13:29
  • 48
    I think a "convert tabs to spaces"-function would already be a huge help.
    – maja
    Commented Jun 22, 2014 at 13:32
  • 1
    @Cubic and Jeroen most languages even for php, jsfiddle's formatting works pretty well, which made me believe what I said.
    – Amit Joki
    Commented Jun 22, 2014 at 13:54
  • 6
    @AmitJoki PHP is also a C like language. With funky html tags everywhere.
    – Cubic
    Commented Jun 22, 2014 at 13:58
  • 10
    Often times it's the original formatting that leads to the problem though. When people fix code in questions, it often removes the problem (or makes it glaringly obvious), making for some very strange questions. Commented Jun 22, 2014 at 14:14
  • 14
    Instead of converting tabs to spaces (which really can lose information), how about the frighteningly easy method of setting tab-size (and -moz-tab-size) to 4?
    – Ry- Mod
    Commented Jun 22, 2014 at 16:25
  • 2
    @false, I disagree with that. Maybe set tab-size to 35 and make it glaringly obvious that there are tabs. Lots of newbie Haskell bugs are caused by people failing to realize that using tabs is a Bad Idea.
    – dfeuer
    Commented Jun 22, 2014 at 16:50
  • 19
    If this happens, I predict years of flame wars about where curly braces should go in C code. Commented Jun 23, 2014 at 13:29
  • 8
    Maybe we need a "This needs to be tidied up" button. Actually, I guess that's the downvote button.
    – austin
    Commented Jun 23, 2014 at 13:49
  • 4
    @KeithThompson: It's blindingly obvious. Opening curly braces should go on their own line, just as the closing braces does, and the two should be horizontally aligned. Doesn't everyone do it this way?! (PS. :) Commented Jun 24, 2014 at 8:35
  • 2
    Function to increase/decrease indentation on one line or set of lines would be great. Just imagine keyboard shortcut for adding/removing four spaces. Commented Mar 17, 2015 at 18:21

18 Answers 18

126

YES!

As someone who regularly copies code from questions into Notepad++ and hits "Reindent C++ code", this would save me a lot of time while making edits.

Two necessary rules:

  1. Disable the TidyUp button unless the question is tagged with a language supported by the formatter.

  2. To avoid exciting our legions of low-rep edit hunters, I would only show this button at 2,000 rep (with the exception of the OP, of course). 2k users can already format code to their heart's content without edit review. This just makes it easier.

5
  • 5
    Hm, maybe make a "suggested edits" badge required.
    – Cerbrus
    Commented Jun 23, 2014 at 8:47
  • Low-rep edit hunters? Is that a thing? Commented Jun 24, 2014 at 13:16
  • 3
    I disagree with the "edit hunters" rule. If users are willing to fix posts for +2 rep, why make it harder for them? It's not like the suggested edits queue is too long or something. However, to prevent <2k-rep editors from suggesting edits that only make minor fixes to formatting, some code could be added that warns the editor if all they did was pushing the "fix code" button a few times. And perhaps the button should be hidden if too many suggested edits were rejected. However, there is already a mechanism in place to deal with bad suggested edits so I don't think that's necessary. Commented Jun 24, 2014 at 13:42
  • 3
    @user2428118 "If users are willing to fix posts for +2 rep" - some users are only willing to spam the edit queue with nonsense in order to get +2 rep: unneccessary tag edits, needless code tags, removing a "thanks" without fixing anything else in the question, minor edits to titles (e.g. add/remove a tag from the tile), ... the list goes on for quite a while. As sadly too many edit reviewers fail to reject this nonsense, far too less people doing this are actually edit-banned. I guess these users would spam the "format" button on (differently) formatted code in order to earn rep as well...
    – l4mpi
    Commented Jun 24, 2014 at 14:09
  • I just realized rule #1 has a big hole: there are already many questions tagged with e.g. python and java, or with multiple languages in general (see this one for example - most of these questions are already kinda bad, but we shouldn't make them worse). There would need to be checks that no tags are present that are not supported, not only a check for a supported tag. It should also be disabled whenever at least two tags are present that are supported but should not be formatted in the same way.
    – l4mpi
    Commented Jul 21, 2014 at 9:08
114

No.

When you see excessively poorly-formatted code in a question, comment about it to the OP. You're not doing them any favours by quietly fixing it for them: teach them that they must do it.

A big button to make it easier to avoid teaching the OP this critical skill is counter-productive and lazy.

25
  • 49
    makes them lazy? We must be more interested in SO's cause rather than be more interested in OP's cause. What it is doing is, it is making reviewers work more. How many "edit" reasons contain "code formatting" is out of my knowledge but a hell a lot, I can tell.
    – Amit Joki
    Commented Jun 22, 2014 at 15:08
  • 11
    @AmitJoki: You think this button will change that? It won't. It'll just make the edits slightly easier to make, so there will be more of them, so more OPs will not bother to format their code. They can know that the SO edit brigade will just do it for them. This is not a good message to send. Commented Jun 22, 2014 at 15:13
  • 26
    BTW, I disagree with "You're not doing them any favours by quietly fixing it for them" We are doing them a favor, by getting them good answers. If "teach them that they must do it" is your motto, then why does "Edit" mechanism exists in SO in first place. Everyone can be taught that only they can edit. Why need community's help at all?
    – Amit Joki
    Commented Jun 22, 2014 at 15:18
  • 13
    @AmitJoki: The edit mechanism is oft abused. People jump in two minutes after a question has been posted and fix all its formatting/content issues, just to get their +2 rep before anyone can convince the OP that they should not be writing such awfully ugly code. TEACH A MAN TO FISH Commented Jun 22, 2014 at 15:27
  • 4
    @Amit: Just shut down the site Commented Jun 22, 2014 at 15:33
  • 5
    this is a nice principle, but in practice this isn't how SO works at the moment... ppl aren't patient enough to wait for the OP, they simply fix the indentation themselves. because of this, what you think that button teaches is irrelevant. Commented Jun 22, 2014 at 16:58
  • 2
    @KarolyHorvath: There's a bunch of impatient people doing the wrong thing, so let's make it easier for them to do the wrong thing? Interesting logic. Commented Jun 22, 2014 at 20:50
  • 3
    @LightnessRacesinOrbit: it's interesting to you, because you have a bunch of presumptions which aren't necessary true. if we told newcomers we expect well intended code and it's their responsibility to fix before posting I would say you are right. But we don't. Just as we don't tell a lot of other things. There might be some faq entry for this, but as long as it's hidden the way it is (the UI for posting question is just horrible), nobody is going to read it. There's a reason I used bold letters for practice...... So though it may seems twisted logic to you, I'm actually trying to help. Commented Jun 22, 2014 at 21:08
  • 2
    It's also a bit unrealistic to expect good code formatting from a beginner. And what better way is there to teach them, than showing how they code should have looked like? Commented Jun 22, 2014 at 21:14
  • 8
    @KarolyHorvath: They don't pay attention when other people edit their code. They pay attention when you point out that they should do it. You don't have to be an "advanced programmer" to see that your code is unreadable and has inconsistent formatting; you just need to be alive. Commented Jun 22, 2014 at 23:09
  • 5
    There's a false premise here that it only exists (or would only be used) by editors. Why is it wrong to give the OP a tool which they can use (or be told to use)?
    – AD7six
    Commented Jun 23, 2014 at 15:43
  • 2
    Most of the code editors offer code-reformatting feature and they do so for a reason - it just saves time (and you can always ignore it and use whatever convention you want).
    – Isantipov
    Commented Jun 23, 2014 at 21:58
  • 3
    Um .. how does this square with the social mantra repeated over and over and over and over and over and over and over and over and over and over and over and over and over and over ad nauseam that SO is "all about having encyclopaedic worthwhile information on the net in the long run!!"
    – Fattie
    Commented Jun 24, 2014 at 11:43
  • 2
    Also, this sounds like something my grandmother would say. Do you also want them to, say, clean their faces properly (with both sides of the towel), and eat more meat and less carbohydrates? I invented the phrase "teach a man to fish.." but it just doesn't apply here. This is just social engineering in the "it takes a village!" category. SO exists to make money. If SO believe that "tidy code samples" (or, pink code samples, or, more questions about Giraffes, or ... whatever) .. if SO believes "tidy code samples" will make them more money, then SO should make it so.
    – Fattie
    Commented Jun 24, 2014 at 11:46
  • 2
    Except when I'm writing Python or posting on Stack Overflow, I haven't formatted my code by hand since 2002. It's one of the things modern IDEs are for. Commented Jun 24, 2014 at 22:08
45

No

The worst kind of snippets that get posted to questions are not badly formatted ones. The true scourge are the kind that are not copy/pasted from original code or a Short, Self Contained, Correct (Compilable), Example (SSCCE). They are rarely compilable, often completely fail to demonstrate the problem, or give drastically wrong hints by introducing bugs that are not related to the question.

Anything that would encourage the OP to post fake code instead of real code (the kind that's properly formatted by his programming editor) should be very strongly avoided.

19
  • 7
    So any easily readable code is false code?
    – Braiam
    Commented Jun 22, 2014 at 14:51
  • 10
    If you can't fit your code in the 80 columns provided then you are doing it wrong. True for your text editor, true for the SO box. Commented Jun 22, 2014 at 14:53
  • 14
    "Anything that would encourage the OP to post fake code instead of real code [...] should be very strongly avoided." - and how is this relevant here? are you suggesting the tidyup button encourages posting fake code? seriously? Commented Jun 22, 2014 at 16:55
  • 9
    @KarolyHorvath, why would the OP tidy the code here at SO but not when actually programming it at his/her machine?¿ It's like only brushing one's teeth moments before sitting at the dentist chair, not after every meal, no, only moments before the dentist...
    – brasofilo
    Commented Jun 22, 2014 at 17:05
  • @brasofilo: check my other comment. I expect SO users to push the button, not the OP. whoever posts wrongly formatted code won't likely push that button. Commented Jun 22, 2014 at 17:07
  • 2
    @KarolyHorvath, oh, I see, then it shouldn't be available to the OP, so we know what kind of code the person is writing. I think it's quite a different thing if the tool is for editors (agree with the feature) or for posters (disagree).
    – brasofilo
    Commented Jun 22, 2014 at 17:11
  • 6
    to summarize what I meant: this "answer" is a complaint about the lack of proper SSCCE posts. I complete agree with it, but I don't see how this answers the question or even relevant here. Commented Jun 22, 2014 at 17:11
  • 1
    "... this is the reason I dislike some edits to my posts so much. They commonly do nothing but remove extra spacing. [...]. Real copy/pasted code is indented." With this, Hans actually is given an argument in favor of a TidyUp (or Indent) button. He is copying real code which already is properly indented in the source, but obviously he is failing to remove unnecessary extra spacing at the front when pasting.
    – user128300
    Commented Jun 23, 2014 at 5:13
  • 2
    -1 Since when is nicely formatted code "fake code"? I'm quite capable of producing an SSCCE with correct identation. Anybody who can't is inept at using an editor or just plain lazy at posting their question. Having a button that auto-idents code will not ruin the quality of code we receive (which is poor anyway). But it will at least let us see what's happening in it. Commented Jun 23, 2014 at 8:32
  • 1
    Also, NO: Sometimes, the exact reason a user asks a question is his bad formatting. Case in point. Properly indenting that code may make it unclear why exactly that question is a question in the first place.
    – Cerbrus
    Commented Jun 23, 2014 at 8:42
  • 2
    -1: The problem of SO is that it doesn't recognize tabulations as 4 spaces. I can't directly copy-paste a code snippet from my IDE without adjusting it. So you are completely ignoring another category of SO users that would find this solution useful. Commented Jun 23, 2014 at 9:13
  • Hmm, no, it definitely expands a tab to 4 spaces. Programmers that use tabs in their code deserve a reminder though. Commented Jun 23, 2014 at 9:37
  • 3
    @RamyAlZuhouri "The problem of SO is that it doesn't recognize tabulations as 4 spaces" - Because tabs are NOT 4 spaces, maybe? You are completely ignoring everyone who grew up with 8-space tabs or likes their tabs in a different size for some other reason...
    – l4mpi
    Commented Jun 23, 2014 at 9:37
  • If you use tabulations instead of spaces, then you set them to 4 spaces, not 8. Commented Jun 23, 2014 at 10:09
  • 1
    @Cody best of luck with that mission :)
    – AD7six
    Commented Jun 25, 2014 at 11:56
35

Code formatting is absurdly far from being language agnostic. Yes, C, C++, C#, Java, and maybe a few other languages have sufficiently similar syntax that you might be able to get away with one formatter for all of them. But then you get to Haskell and Python, which are sensitive to indentation and look nothing like each other, and Standard ML, which looks somewhat similar to Haskell but whose syntax is actually very different, and line-oriented languages like BASIC, and LISP-family languages that have virtually no syntax and really need to be indented semi-manually, and various assembly languages with their block-based indentation, and you've got yourself a nice mess.

3
  • Even among C++, C#, and Java, the code formatting conventions are different enough that it would make this feature fairly complicated.
    – JLRishe
    Commented Jun 23, 2014 at 5:17
  • 2
    Good points, but one mistake IMHO: the Lisp (not LISP) family are much probably the easiest to auto-indent. Unlike other languages, the whole syntax-tree is defined by the programmer. There is no room to ambiguity. And there are no special cases, like operator precedence rules (C and related languages), semicolon inference (JavaScript, Scala) and so forth. In fact, many Lisp-aware editors do auto-indentation (like Emacs, DrRacket, LispWorks and Light Table, just to name a few). Also, the Lisp community strongly discourages "opinionated" indentation: there are specific rules for doing it.
    – rsenna
    Commented Jun 24, 2014 at 14:42
  • 1
    @rsenna, I was playing a bit fast and loose there. You're right about indentation, in a sense; the indentation is determined once the lines are broken. But there are no rules for breaking lines and failing to break them well can make the code very hard to read.
    – dfeuer
    Commented Jun 24, 2014 at 15:23
19

The proposal is appealing, but dangerous. Reformatting code — especially the egregiously bad code we're all thinking of here — can remove or introduce bugs, and therefore needs to be done with a human eye in the loop. A "fix it for me" button would be a little too easy to smack without thinking.

However, there is a case I know of where code that wasn't misformatted to begin with becomes misformatted when pasted into the SO edit box (and then possibly has the {} button applied to it): if it was originally formatted with hard tabs. I'm not 100% sure I'm remembering this right, but I think the effect is that in the rendered question, everything gets shifted left one tab stop, so it looks like the poster doesn't know to indent function bodies but does know to indent blocks within them. When you open up the edit window, the code appears to be correctly formatted.

This strikes me as a flat-out bug in the site, which could be fixed with relatively small tweaks to the {} button and/or the Markdown parser. One obvious approach would be to have the {} button convert tabs to spaces within the region (assuming 8-space tabs, as that is the default and the most common thing to encounter, and there's no room for a tab-width selector) before shifting everything right four spaces.

16
  • 7
    Why not 4-space tabs?
    – Scimonster
    Commented Jun 22, 2014 at 13:57
  • 2
    That's strange, I have never had a problem with using tabs within a code block on SO... unless there is a mixture of tabs and spaces.
    – cimmanon
    Commented Jun 22, 2014 at 14:01
  • @cimmanon I may be misremembering exactly what is wrong. It may be that the problem is that the {} button can create a mixture of tabs and spaces where there wasn't one already.
    – zwol
    Commented Jun 22, 2014 at 14:05
  • 1
    @Scimonster 8-space is the only workable choice. Please just trust me on this, there isn't room in this comment box for that rant and it's off-topic anyway.
    – zwol
    Commented Jun 22, 2014 at 14:07
  • 4
    I usually downvote postings that "were fine in the poster's editor". Everyone should care enough to proofread their own posts at least once and check that it looks like it should.
    – Michael
    Commented Jun 22, 2014 at 14:58
  • 8
    @Zack: Are you on drugs? (hint: the answer is yes) "Please just trust me on this" LOL how about .. um .. no! Commented Jun 22, 2014 at 15:06
  • @Zack: It's really sad. Sounds like you've have some extraordinarily bad luck which is far from the norm. :( Commented Jun 22, 2014 at 20:53
  • 1
    @LightnessRacesinOrbit People tell me I'm weird for never having worked professionally on anything that wasn't millions of lines of C-family with at least a decade of history, but from where I'm sitting, there are only such codebases, as far as the eye can see...
    – zwol
    Commented Jun 23, 2014 at 0:14
  • 6
    "that is the default and the most common thing to encounter" No, I'd say that is false. Where are you seeing this huge abundance of 8-space tabs?
    – JLRishe
    Commented Jun 23, 2014 at 4:40
  • @Zack: Then, once again, your experience is heavily skewed into the world of the bizarre. Commented Jun 23, 2014 at 9:20
  • 2
    @Zack Maybe if you use 4-space tabs then your argument will fit into this box :)
    – sehe
    Commented Jun 23, 2014 at 11:25
  • 1
    @JLRishe I can't think of a good way to canvas publicly available codebases for their tab width, and I'll believe you if you say your experience has been all 4-space tabs, but ... 8-space is the default in every terminal and terminal emulator there has ever been. It's the default in every incarnation of vi, it's the default in Emacs, it's the default in Xcode, I haven't tried Sublime Text but (based on looking over people's shoulders) it sure looks like it's the default there too, and it's the default in <textarea>.
    – zwol
    Commented Jun 23, 2014 at 13:31
  • 1
    @Zack Eclipse and Visual Studio (and less importantly, Notepad++, jsfiddle, and Geany) use four spaces as their default tab size and default indentation size. And I can tell you that there has been a lot of code written in those first two.
    – JLRishe
    Commented Jun 23, 2014 at 13:47
  • 1
    OK, so, a Windows/not-Windows and/or enterprise/other dividing line? I can believe that. Maybe we do need to find space in the UI for some sort of tab-width control, then. (This would account for why badly-formatted code in the C tag appears to correlate with Windows use, since our textareas interpret hard tabs as 8 spaces -- by, yep, default.)
    – zwol
    Commented Jun 23, 2014 at 13:58
  • 2
    @Zack: You're not crazy. I used to think tabs were far better than spaces. I still believe that in principle tabs are better than spaces. But that mythical, ideal world where principles win out is confined to dreams, fantasies, and perhaps a handful of ivory towers. In the real world, with real programmers and real code, it's a horrendous mess, and the promise of tabs is woefully unfulfilled.
    – John Y
    Commented Jun 23, 2014 at 21:58
13

YES

I can't downvote the existing no answers enough, so I made a yes answer to try to explain why they're wrong, because otherwise I was just going to have to rant about it to someone.

First up, my premises:

  • This suggestion can be implemented in such a way as to make direct negative impacts on questions negligible.

My reasoning behind this is, you could make the feature something more obscure than a button - thus, only users familiar with the site would even know it existed. Or, you could enable it only for questions that we are reasonably sure that it would not affect (ones tagged with the right language for example).

We could also, as one commenter pointed out, have a button to show the original text:

This code was automatically formatted. Click here to see the original text

This would solve the problem for languages like Python where it is common for refomatting to introduce bugs, because the same people who know to look out for those questions know to click that button.

  • This suggestion would provide a benefit to users.

This seems so self-explanatory, but just to make it clear: this will save time in at least some (I would argue many) cases, whether for people editing or for people pasting code in. I'm going to leave it there, because that really can't be argued with.

These two are what I'm building my yes case around, but there are some objections that I want to cover which are actually...

Terrible objections

  • For language X it would not work

Fine, let's not have it for language X then. As many have pointed out, it can be limited in scope as I outlined above, and in many other ways I am sure.

Next, a couple of variants of the same objection:

  • The OP should clean up their own code
  • This promotes lazy answers

These are just the worst. First up, *just because this button is there doesn't mean you can't let the OP know their code sucks.

"Oh, but it'll seem like we're just being nit-picky because it's so easy for us to just click those three buttons needed to tidy it up ourselves"

Why yes, yes it will.

As for promoting lazy answers, it sure doesn't. If the OP was going to be lazy in the current system they just wouldn't format their code. Now, they can be lazy and come out with a high-quality question/answer (potentially)

If it's still too low-quality, then actions can be taken to fix it, but otherwise hey, we've just made someone who with the existing system would not contribute effectively into someone who does contribute effectively. Gosh, what a terrible result.

Just to rub it in a bit, consider my paraphrased version of that same objection:

  • It's good for the soul to do pointless work turning tabs into sets of four spaces instead of just pressing a button to do it for us.

Yeah, no.

"Oh, you are so right Jeff, we shall all bow to you because you are so right and because you have the same name as our illustrious founder"

Hold your horses. There are some legitimate objections to be raised. Funny that no one has raised them yet though, that kind of sucks. So:

Legitimate objections

  • The visual clutter will reduce usability more than the new feature will help

Ok, sure, I'll accept that that's possible, though I don't know for sure. You'd have to ask a UX designer. If that is relevent, then make it hidden, like a key-combination (ctrl-k-d anyone) that only people who know how to use the site know exists, but still makes life easier for the most valuable members of the community.

"Hang on, if we make it hard to spot then..."

  • The gain for this feature while making it so obscure does not outweigh the cost to implement it

Again, I can't speak to this. we'd have to get someone who knows what it would involve to weigh in, but it's certainly not something I would use as an objection right off the bat...

1
  • 3
    +1 this is so good, the making people lazy objection is just ridiculous. Thank you for this brilliance. Commented Sep 18, 2015 at 17:12
12

I think if this is implemented, we will have awesome looking code!

I agree with you.

The TidyUp button will be very useful to those who read posts which contain code. I'm looking forward to the introduction of this button as some unreadable code might become readable and readable code will become more easier and faster to read.

3
  • "l to those who read the posts containing code." every one does ;)
    – Amit Joki
    Commented Jun 22, 2014 at 11:36
  • @Amit ,Edited it...I meant to say the posts which contain code because there are some questions which do not contain code..
    – Spikatrix
    Commented Jun 22, 2014 at 11:47
  • 11
    Why stop at indent options? You could have buttons to choose between K&R brace style, GNU style or One True Brace Style and have 9 tidy up bottoms Commented Jun 22, 2014 at 13:30
11

Yes

The most annoying edits to perform (especially to my own posts!) are indentation related. A user has to into every line and remove just the right amount of spaces so it looks nice.

And I actually do this all the time, to my own posts (and occasionally to others). I don't think it makes it look fake, it makes it look professional. It is certainly more readable.

Of course, it would be great if this button weren't needed and everyone wrote great posts to start with. But then, if that is the expectation why have the "Edit" button to begin with? The community expectation seems to lean much more towards the community improving the site's content.

One more request for the feature: "Remove unnecessary whitespace/newlines". Way too often I see code like this:

//Some code






//Some more code

The extra lines often cause the code to need scrolling, and are really annoying in general. A quick way to get rid of them would be great!

Perhaps gold-badge users in a tag could allow/disallow this button if it would really hurt certain tags (like python).

9

I agree with DavidGelhar's mentality, namely that while I think this would be a very useful button for new users of formatting-similar languages, I do not think it should have multiple other buttons to select formatting style - there should only be one. Having multiple buttons or a process to tidy up would create too much clutter and perhaps even confusion for newer users.

If someone wants to have an indentation or coding style other than what is provided by the Tidy Up button they still have the ability to format the code themselves. The button would primarily serve newer users since they generally have the non-formatted code and many of them don't have a coding style or proper indentation anyway.

2
  • 4
    If this is ever done, it will be necessary to make it language specific. Formatting Python or Haskell code according to some C or Java style will produce results that won't even compile. Formatting SML or Scheme code that way will likely make them completely unreadable.
    – dfeuer
    Commented Jun 22, 2014 at 16:54
  • My God - people wouldn't be able to understand 10 buttons, labelled with programming languages :) (But yes, of course, obviously, the machine-brain should be able to figure out what the language is, etc.)
    – Fattie
    Commented Jun 24, 2014 at 11:53
6

I would like a tidy up button as well, but primarily for answering purposes. I often answer questions with code "ideas", non tested pieces of code which should illustrate the idea itself, not a 100% copy-pasteable solution.

Even if I copy-paste my solution from a sandbox environment, it's often a snippet, being part of a larger solution, so often the indentation is wrong (because it's part of something), for example, let's say I'm answering someone who wants to know how he can add two numbers in JavaScript, I would write the following code in the sandbox environment:

var sum = function(a, b) {
  return a + b;
}

console.log(sum(2, 3));

However, I would only answer with the relevant code, which in this case would be:

  return a + b;

However, due to the extra indenting (from the function), the indenting of the answer is odd. I'm well aware that this is only one stupid line of code, but imagine more lines of code.

So, I would like to make reindenting my code easier.


However, that's from my perspective, from a reviewer perspective I don't know what I would say. I agree with most people that the OP should be made aware of his/her lack of indentation and the importance of it.
But I'm also pretty sure I re-indented some code already, most of the cases because I couldn't give a proper answer without spitting through that code. In most cases that turns out well, but lately I encountered someone who, after I fixed his indentation, updated his question with more lack-of-indentation, so I gave up and downvoted the question in the process.

So I would personally say yes, but maybe it should be implemented like close reasons, several closing reasons automatically add a comment to the answer/question telling the reason why it got closed, maybe automatically add a comment like:

Your question/answer has been reviewed and to improve readability of your code, the code was re-indented. Please take a look at [an interesting wiki/FAQ/page about indentation] for more information.

2
  • 1
    Select the code and press Ctrl+K (or click the toolbar button). That'll toggle the indentation by 4 spaces, perfect for fixing this problem.
    – Cody Gray Mod
    Commented Jun 25, 2014 at 5:20
  • @CodyGray didn't know that one :o Thanks! Commented Jun 25, 2014 at 5:59
4

I have seen at least one case where someone "helpfully" tidied up some CSS code and, in doing so, fixed the bug which was being asked about. I can foresee many more problems along these lines if an autoformatter were implemented.

In that case, the asker didn't realise that spaces before the colon in a:visited had any meaning whatsoever, so they wouldn't have objected to an autoformatter which removed those spaces. And no one asking would have been any the wiser that the original code had spaces.

Of course, in this case, a competent autoformatter would not have removed the spaces, but subtle bugs can happen.

2

I realize there may be a few problems with the tidy up button but I do think an indent block button would be great.

It would save a lot of time for people copy/pasting blocks of code and often avoid problems with the code looking different after it is pasted.

The way it is now I often see code that was copied in and now looks different because of the way the first indent is treated as a code block rather than an indent. So most code blocks could be better formatted or at least easier to format with an indent block button.

2
  • Tidy up = indent block
    – Amit Joki
    Commented Jun 23, 2014 at 15:39
  • well then it would work fine with everything because the only thing it would change is it would make everything code block, other than that it would keep the format of the code. Commented Jun 23, 2014 at 16:08
1

Having serious code formatting tool would be beneficial

I would suggest to add a small button, on the top next to other buttons, that would perform the language specific formatting if so desired by the user or editor. It must be implemented seriously, auto-detecting or allowing to specify the exact language, and would save time by eliminating the need to copy-paste to/from some external code formatting tool.

I am strictly against a "big button" that formats all languages from Assembler to Python same way.

0

There are so many negative Nancie's here who are screaming no about the feature. Questions should be answered, and making it harder for the person giving the answer on the SO side is unacceptable if it can be easily fixed. If the feature were to be implemented it should be done something like this:

The question viewer (not asker) should see a little button at the corner of the source block that says Tidy-up, and then they get to choose a language for the Tidy-Up button to format to. If we feel like getting really advanced we can have a check box for when a person has compound code, and then they ctrl click thru a dropdown menu. This would be (relatively) easy to implement and just make things simpler for people answering questions and still keep the grouchy people.

8
  • 1
    "Questions should be answered, and making it harder for the person giving the answer on the SO side is unacceptable if it can be easily fixed" - which is exactly why everybody dealing with python would absolutely detest this feature, as it would make newbie questions even harder to decipher than they already are.
    – l4mpi
    Commented Jun 23, 2014 at 9:07
  • 1
    @l4mpi: Exactly why I answered with a no. Bad formatting can be the very reason the question exists. Fixing the formatting in a question isn't always constructive.
    – Cerbrus
    Commented Jun 23, 2014 at 9:10
  • 3
    @l4mpi try reading the entire post before you try to argue against it (and make yourself look stupid). The option to select which language to format it to would be the opposite of what python users "detest". Commented Jun 23, 2014 at 13:29
  • 1
    @Tim.DeVries no, I was aware of this. And I'm fairly sure such a feature would still be horribly abused. Python not in the list of available languages? Just select C instead! Newbies will always find a way to abuse a potentially helpful feature like this. It would have to be completely impossible to ever use any type of automatic formatting on python (or haskell, or prolog, or lisp, etc) code in order for me to get behind this feature...
    – l4mpi
    Commented Jun 23, 2014 at 13:35
  • 2
    if you pay attention to my description this would be a feature available AFTER the question has been posted, for the person answering the question. There is no pre-formatting of the text prior to posting the question.
    – Skyler
    Commented Jun 24, 2014 at 0:20
  • @Skyler Maybe I'm still misunderstanding you, are you suggesting a button which is client-side only? If not, making it available after the question was asked would just shift the problem from asking to editing. And either way, not having it available while asking would also fail to improve the two main benefits OP sees in this button, namely (1) reducing the amount of manual formatting edits and (2) letting the asker quickly fix his indentation in the code, e.g. if it was copied out of a larger snippet.
    – l4mpi
    Commented Jun 24, 2014 at 11:54
  • i kind of agree with some of what the other people wrote about #2, in the sense that the original formatting may tell you lots about the question author. However, this allows for people to just copy paste the code, and then IF the answerer wants it to be clearer for their sake they can see the code formatted. This addresses #1 by removing the need for editing poorly formatted code, and it actually gets rid of the need for #2 since your answerers can fix that at the push of a button.
    – Skyler
    Commented Jun 26, 2014 at 7:21
  • To pull off of one of the answers here: "Anything that would encourage the OP to post fake code instead of real code (the kind that's properly formatted by his programming editor) should be very strongly avoided." If he just copy-pastes code from his editor without having to worry about its formatting he's more likely to do so.
    – Skyler
    Commented Jun 26, 2014 at 7:27
0

I have made a Chrome extension - ProKeys - that will let you do the required task

I don't know whether it is related or not, but I have developed a Chrome extension ProKeys that has an option to replace a tab with 4 spaces. That can at least, help the process of tidying up, and is much like the Tidy up! button.

1
  • FOUR !!???? Let the flame-wars begin !!
    – Mawg
    Commented Jan 29, 2015 at 9:13
-3

NO

Although it can save us from going over and over again to select the code part and format it one by one still formatting all of the text with one click can erase some errors and sometimes believe me even the pro coders makes small mistakes about bracets semicolons etc so NO.

But it will be better for us to have coding sections rather than tidying up everything one click and the code section is activated for posting and getting the code out.One click again and it returns to the writing mode.

Easier to write or paste the code.Easier for users to read and not fixing the posters mistakes.

-3

No:

Sometimes, the exact reason a user asks a question is his bad formatting.

Case in point:

A question about the difference between:

for(var i = 0; i <= num; i++) {
array.push(i);
return array;
}

And

for(var i = 0; i <= num; i++)
array.push(i);
return array;

Properly indenting that code may make it unclear why exactly that question is a question in the first place.

Besides, this encourages lazy edits where users only hit the "tidy up" button, and save, without paying attention to what's actually changed.

6
  • 2
    Does it really matter if the question is the place where the OP finally "gets it" because the identation is now correct? Those questions are generally a waste of our time anyway, regardless of whether the question framework indents the code or an answerer points out the problem. They should be swiftly closed either way. Commented Jun 23, 2014 at 8:47
  • 1
    Questions shouldn't be answered in the question by edits. Especially not if that makes it unclear what the question was in the first place. (or why it was a question)
    – Cerbrus
    Commented Jun 23, 2014 at 8:48
  • Putting aside languages, such as Python, in which indentation affects logic, I'd say it doesn't matter to me. No question in which indentation was the cause of the problem is worth keeping around on this site. If the OP figures out the problem because someone corrected the indentation (note: something that this feature only makes easier), then I'm happy. Close the question and move on. Commented Jun 23, 2014 at 8:50
  • 1
    @Duncan you would have to make sure that these questions are then swiftly closed and deleted, as to not leave a confusing non-question around. Given the current state of question closure and deletion, I doubt this would work. But realistically it probably doesn't matter all that much as I think this would be a rare case...
    – l4mpi
    Commented Jun 23, 2014 at 9:16
  • 1
    Your example is confusing! Having no parenthesis has a different meaning from having them. If you omit them, it means only the first line is included in your loop. This is not formatting, this is code changing :) Commented Jun 23, 2014 at 13:20
  • @AdelBoutros: That question was about the difference between having and not having the brackets. Those 2 snippets are literaly copied from the question (They're not meant as a before / after formatting edit example)
    – Cerbrus
    Commented Jun 23, 2014 at 13:31
-5

It's more or less completely nuts that:

technology can't solve 90% of the problem of poorly formatted code

It's not like you need cutting-edge AI to achieve this.

If google owned SO, they assign a few engineers to make it so that incoming questions/answers were checked for what looks like poorly formatted code. It would make a judgement based on how experienced the user is about whether it's worth rejecting, alerting the user about in strong terms, or flagging, or leaving it.

Instead we have all this blether about social engineering.

In this day and age .. it would be much like discussing spell-checking in posts. ("Should we ban people who can't spell, what to do about people who can't spell!" etc.)

How many more years, how many more decades, until SO stops blethering about code formatting?

2
  • 1
    Yes. Technology already does solve 90% of the problems of poorly formatted code, in most languages. It just doesn't do it on Stack Overflow. Commented Jun 24, 2014 at 22:10
  • 2
    Stack Overflow does do something like this. Jeff Atwood got a few engineers to decide how to determine whether incoming questions contain poorly formatted code so they can be rejected accordingly. (For the record, I'm firmly on the side of rejection, not automatic fix-up.)
    – Cody Gray Mod
    Commented Jun 25, 2014 at 5:22

You must log in to answer this question.

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