24

At Stack Overflow, many (if not most) questions are provided with some code that, for some reason or another, has an undesired behavior. For users who answer these (or at least for me), it makes a huge difference when the code is properly formatted, as opposed to crappy code that is difficult to read. I believe that providing well-formatted code improves the chances of a question getting high quality answers. Therefore, wouldn't it be a good idea to have some sort of automatic beautifying process (in the style of JS Beautifier or AStyle for C++) done to the users' code, such that:

  1. Overall questions' quality would be improved.
  2. Code-formatting edits would become unnecessary.

This process could potentially take place in the client (using JavaScript), such that server workload isn't affected at all.

EDIT: Actually, it doesn't even have to be an automatic beautification, or a button. It could just be a message that reminds the user that it's good to follow readable and consistent code style schemes when a code detects that they aren't (and maybe even stop them from posting a question when the issues are significant and the reputation is not).

EDIT 2: Here is the complete suggestion:

A preliminary test for this idea could be made using already existing code. For example, it could be employed only when the JavaScript tag or keyword are present, and could be done with the existing JS Beautify library. The code could calculate the beautified version of the user code, and then compare it to the original one, calculating the change by measuring the number of characters of difference (or Levenshtein Distance) for which working code exists here. There should be a threshold of warning where the user receives the following message:

Example code styling error.

Note: The previous image is only an example, and it definitely was not product of working code (although I claim it wouldn't be too difficult to implement a working experiment).

8
  • 14
    Isn't a tidy-up button what you want?
    – Spikatrix
    Commented Jan 4, 2015 at 4:05
  • 1
    @CoolGuy I think most users who don't take the time to format their code would click the button unless it is significantly large/noticeable. I've edited the question with an alternative suggestion.
    – JCOC611
    Commented Jan 4, 2015 at 4:11
  • *wouldn't click - couldn't edit the comment twice
    – JCOC611
    Commented Jan 4, 2015 at 4:23
  • 5
    If your suggestion was implemented, the number of questions being asked daily in the python tag would decrease as whitespace does matter in python.
    – Spikatrix
    Commented Jan 4, 2015 at 4:27
  • 2
    @CoolGuy: It depends on how it is implemented. The way I see it, Python forces programmers to indent correctly (which wouldn't be detected by a well written program as bad code-styling). Also, who says that the code wouldn't be able to parse question tags in order to adapt to certain languages. Also, it would potentially be able to catch simple IndentErrors that would make for closed typo-error questions.
    – JCOC611
    Commented Jan 4, 2015 at 4:29
  • 1
    Add gofmt to that--applies the one standard Go style, and I think you can call out to the official service at play.golang.org to do it. (play.golang.org is also standard for sharing (and running) Go snippets, and answerers typically copy sample code there for people to play with.)
    – twotwotwo
    Commented Jan 5, 2015 at 8:27
  • 1
    I kno ho to code I swear : I kno ho to indent I swear I just don want to ;)
    – Abhitalks
    Commented Jan 5, 2015 at 9:02
  • 2
    For languages where whitespace is semantically irrelevant, you can apply formatting to it. For languages where it's important, you wouldn't want to. But it might still be a bit of a warning bell, if it's not very good. But I often run code through perltidy before even starting to look at it - one of the downsides of SO, is the 4 space indent makes copy-pasta a little more inconvenient than normal.
    – Sobrique
    Commented Jan 5, 2015 at 16:53

1 Answer 1

9

What if the user was posting a piece of code that they want to leave formatted incorrectly because they don't understand why it's not working? I'm thinking maybe they post a few for loops that are written incorrectly. They'd want to leave it as is so people can explain it to them.

I like the idea though. Maybe make it a button so people can choose it if they want format their code.

Edit

Following the OP's edit, the message would simply go in the box that appears when typing a question or answer.

Edit 2

A second suggestion: Why even check if the code is styled correctly? On Stack Overflow, the questions have to do with code, and many of them are asking about problems that may be a result of spacing error. So, rather than even checking if the code is written correctly, why not just show a suggestion to users?

When the user is writing their question, and they start typing code, show a message that suggests users take the time space their code correctly. Example:

Remember! Although not required, spacing and indenting code correctly will help people answer your questions more efficiently.

6
  • 3
    By formatting I mostly mean indenting, spacing and keeping each operation in a single line. I think no non-space characters should be added/removed from the code.
    – JCOC611
    Commented Jan 4, 2015 at 4:05
  • I know what you mean, but that's my point. If someone posts two for loops (an outer and inner), and the've indented things wrong. Braces are wrong. etc. They'd want to leave it like that so it can be fully explained. This is why I think it should be an optional feature (like a button). Thoughts?
    – user3857868
    Commented Jan 4, 2015 at 4:06
  • 1
    I edited the question with a new suggestion. I definitely agree that if the beautification process makes it more difficult for somebody to answer a question, then it shouldn't be there.
    – JCOC611
    Commented Jan 4, 2015 at 4:14
  • Perhaps the OP's suggestion is viable if the system detects a code block?
    – AStopher
    Commented Jan 4, 2015 at 21:09
  • 3
    Again, in most languages spacing and formatting have nothing to do with whether or not the code is correct (yes Python is definitely an exception). Thus, having good structure makes it easier to tackle other problems. The eventual code issues related to styling shouldn't even be asked in the first place since they are basically typo errors that require minimum effort to fix.
    – JCOC611
    Commented Jan 5, 2015 at 1:04
  • @JCOC611 Yep, that is a great point.
    – user3857868
    Commented Jan 5, 2015 at 3:26

You must log in to answer this question.

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