I support this proposal. Based on the current behavior, the assumption would appear to be that we write posts like this:
I am having an issue with foo when I pass in an uninitialized bar: var bar;
//This is where the problem is
foo( bar ); The defined behavior should be a return value of baz... am I misunderstanding something?
Certainly some questions come in this way, but usually from low rep users who don't use the formatting tools in the first place. In practice, I see very, very few questions this poorly formatted. Most low rep users know to separate their code from the rest of their content. Professional programmers who spend time with the site (and, as I said, even most hobbyists who are new to the site) are much more likely to format like so:
I am having an issue with foo when I pass in an uninitialized bar:
var bar;
//This is where the problem is
foo( bar );
The defined behavior should be a return value of baz... am I misunderstanding something?
Now, when a knowledgable programmer who is used to using code editors selects starting from somewhere in the middle of the first line to somewhere in the middle of the last line and presses Ctrl + K, the following happens:
I am having an issue with foo when I pass in an uninitialized bar:
var b
ar;
//This is where the problem is
foo(
bar );
The defined behavior should be a return value of baz... am I misunderstanding something?
This then becomes a bit of a mess to clean up after1. Attempting to reverse the indent (as you might when using a code editor) leaves you with this:
I am having an issue with foo when I pass in an uninitialized bar:
var b
ar;
//This is where the problem is
foo(
bar );
The defined behavior should be a return value of baz... am I misunderstanding something?
Realizing your mistake, you can now press Ctrl + Z (once!) to get back to where you were, but at this point I've usually lost faith in the editor and have switched to using TextPad.
If we changed the behavior to match that of nearly all professional code and text editors, then someone attempting to use the formatting shortcut on the first example would end up with this:
I am having an issue with foo when I pass in an uninitialized bar: var bar;
\\This is where the problem is
foo( bar ); The defined behavior should be a return value of baz... am I misunderstanding something?
Which can be easily undone using the same stroke which caused the formatting faux pas, and then formatted correctly by adding the necessary whitespace. (Again, I can't think of any examples where someone has actually submitted a post like this.)
In conclusion (TL;DR), I think this request is justified for at least the following reasons:
- It better aligns with existing conventions within the programmer community (the goal is, after all, to edit code, even if the post contains other non-code content)
- It is
easier more intuitive to undo1
- It would be appropriate for (in my unresearched opinion) the majority of posts
- The current behavior is actually destructive to the original post for those caught unawares
1 As Dukeling pointed out, Ctrl + Z will undo the formatting in a single stroke. I don't know why this hadn't occurred to me before. I still think that pressing Ctrl + K twice should leave you with the same content as you started with, but this "easier to undo" point is a bit weaker now.