Possible Duplicate:
How do I format my code blocks?

How can I easily paste code into my questions so that it displays EXACTLY as it does in my local files? I'm asking because when I try and post a question, I have to indent each line by four spaces, which doesn't allow me keep the original formatting of my code, not to mention that other members always mention that my code is not formatted correctly.

First, is there a way that I could be doing this better, and if not, any ideas on how this could be improved, or made easier?

(I was recommended by another member to post this question here.)

| improve this question | | | | |
  • If you're already using the built-in indentation tool, then Michael has your solution below. – Shog9 Feb 25 '12 at 20:00

I'll take your most recent question as an example.

This isn't really a problem that's amenable to a technological solution. The code you pasted into that question is formatted poorly. Not in the sense that it's incompatible with Stack Overflow's formatting system and rules, but in the sense that you just format your code in a manner that's hard for others to read and understand. Sorry if that sounds harsh, but that's what I see.

The most obvious problem I see is inconsistent indentation. When starting a new scope, sometimes you fail to indent, sometimes you indent by one, four, or... negative one space. Stop that. Use four spaces, or a tab, consistently. Just do it. It may seem pointlessly controlling now, but give it a few months. You'll wonder why you ever did otherwise.

Some other quick comments: place your scoping braces on a new line, or not, but do it consistently. Don't type newlines or spaces needlessly.

I suggest that you review generally-accepted formatting best practices for your chosen technologies and languages, and adopt them. It'll make working with other programmers, and surfacing some types of bugs, much easier.

| improve this answer | | | | |
  • Truer words are rarely spoken. The little seemingly-insignificant things like code formatting really make a huge difference. If you just can't bring yourself to internalize this, do everyone a favor and get an editor with a "pretty-print" feature. – Cody Gray Feb 26 '12 at 4:55

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