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.

Possible Duplicate:
Easy way to either fix up code formatting or educate noobs
Could the Help be better? (code section)

As a frequent reader, answerer, and editor of Python questions, I find that by far, the most common error new users make is to "format" their code by simply indenting top-level lines (function definitions, class definitions, etc.) by four spaces. In c or java code, (for example) this is not a very big problem, because the beginnings and endings of code blocks are explicitly marked with { and }. If I want to fix the indentation, I can reconstruct the correct indentation by paying attention to braces.

But in Python, this can lead to code that is not only incomprehensible, but also unfixable by editors, because it is impossible to tell where a code block is supposed to end. Most of the time, it's possible to guess at the correct block structure, but even so, it's just a guess; and there's always the risk that in guessing, the editor might inadvertently fix the very problem the OP was asking about.

Would it be possible to send a special message to users with under 50 rep asking Python-tagged questions? Something like "Are you certain your code is indented correctly? Highlight your code and press Ctrl-K or the code formatting button ({}) above for correct formatting."

This might be too domain-specific to be a high priority. But it drives me nuts trying to comprehend malformed Python code all the time. (And it seems these users must not be getting the message described in this answer, or they wouldn't be making this mistake.)


After seeing some reactions to this proposal, I believe I may not have fully explained what I think is happening. My hypothesis about why this mistake happens is as follows:

  1. User pastes PEP-8 compliant code, using indentation of 4 spaces. User then looks at code preview and sees that indented code is formatted correctly, while unindented code is not.
  2. User correctly surmises that the way to format a code block is to indent it by four spaces.
  3. User does not look for any additional information, and so does not learn that there is an easy way to indent the code.
  4. User lazily decides to indent only top-level lines, befouling Python blocks, and escaping any warning message about unformatted code -- because the code is formatted.

I did a lot of looking around; this question is not a duplicate of this question, because it isn't really about code formatting, but is rather about code indentation. I'm seeking to fix a related, but subtly different problem. This code is correctly formatted by users who are concerned about formatting, but are lazy and think they've got it all figured out already. The result is not poorly formatted code, but poorly indented code. My somewhat irritable tone above notwithstanding, I deeply identify with these people, and want to help them.

It might be that there are other ways to help this problem. Perhaps the "How to Format" box should explicitly mention the ctrl-k shortcut; it doesn't right now, at least based on what I see to the right of this box as I type.

share|improve this question

marked as duplicate by Cody Gray, kiamlaluno, amanaP lanaC A nalP A naM A, ChrisF, Anna Lear Mar 29 '12 at 14:53

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.

    
I guess the script checks for four-space indented lines directly adjacent to non-indented lines. Since the inner blocks in Python are indented, they won't have that after indenting the outermost level (def etc.). –  Daniel Fischer Mar 27 '12 at 22:47
2  
    
@senderle: If you find (what you think is) a better duplicate, you're allowed/encouraged to leave a comment or edit it in. But it's generally discouraged to completely remove the original question that this one was closed as a duplicate of. 5 people did agree that it was a duplicate of that one. –  Cody Gray Apr 2 '12 at 23:09
    
@TheEstablishment, OK, I understand. Thanks for putting them both up for me then. –  senderle Apr 2 '12 at 23:09

2 Answers 2

up vote 2 down vote accepted

I wouldn't asked them to double check it, but a small and simple walkthrough over the coding formatting options should do the same. I'd really like to have a feature like this one applied for the first few questions of a new user.

Stackoverflow most important thing is the code, so it should be very very clear how to properly format it.

The first time I asked a question I didn't understood that the {} button could be applied to multiline code, and I find out the Ctrl+K shortcut only later on from a comment to another question. I used the <code> tag, but we all know that is not the best solution.

This is why I also think that a simple walkthrough on how to properly format the code could be extended to all the languages, hence be unrelated to any tag.

share|improve this answer
    
This is very helpful suggestion and as a new user, I was not aware of this option until now. However, I'm not sure if admins will be willing to do the extra work for a problem not many people complain about and possibly not many people will use. A simpler option might just be informing the user about the problems in formatting and telling them about the Ctrl+K option. –  Faisal Mar 28 '12 at 1:13
    
See this feature request. (cc @fai) –  Cody Gray Mar 28 '12 at 3:33
    
I'm deferring my proposal in favor of yours which you posted in a comment here. –  senderle Mar 29 '12 at 13:48
    
@senderle: You touched a very important problem, bad formatted code is a plague. I hope it could be solved in a way or the other :) –  Rik Poggi Mar 29 '12 at 13:58

Unfortunately, this isn't going to work.

The small subset of users who might heed the message you wish to show:

Are you certain your code is indented correctly? Highlight your code and press Ctrl-K or the code formatting button ({}) above for correct formatting.

is the exact same subset of users who will actually check the rendered preview below their question before submission to ensure that their question is readable. Or, failing that, they'll at least notice after they submit the question, and use the "edit" link to fix the problem.

We already provide plenty of help to users regarding how to use the existing code formatting tools, so those people who want to get this right will.

The rest, well, you'll either have to edit, nag, or ignore.

share|improve this answer
    
I disagree with "We already provide plenty of help", a new user don't know about the meta site. I'm a very meticulous person, I care a lot about formatting, but the first time I couldn't get it right, the help message (simple and advanced) under Code it's just not complete enough imo, it just tells to use 4 spaces, but not of the shortcut or about the {} button for multiline. I think I'll open a question to discuss an improvement to the code help. –  Rik Poggi Mar 28 '12 at 7:44
    
@The Establishment, I disagree with your assessment. You're right that these users are looking at the preview. That's the problem. They're looking at the preview and seeing that their code looks wrong, and then "fixing" it in a lazy way. Unless there's an attention-grabbing message telling them about a better way, they'll keep doing this. See my edit above for my line of reasoning. –  senderle Mar 28 '12 at 13:15
    
@Rik: The point of those links wasn't that new users would refer to them, it was to provide a description of the help that we already do provide for new users. The content of the answers is what matters, not the mere fact of their existence. –  Cody Gray Mar 28 '12 at 16:02
    
@TheEstablishment: Ah ok, but I don't think anyway that the help is good enough. I opened a question a couple of hours ago, to explain what I'd like to see in the help. –  Rik Poggi Mar 28 '12 at 16:08

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