Bookmark Topic Watch Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
One of the best ways to make your post easier to read is to put code tags around any code examples you include in your posts.

You can do this by putting [code] at the beginning of the block of code and a matching [/code] tag at the end of the block.

For example, if you did this in the message editor

It would get posted as this:

Hi! I just started learning Java. Is this the right way to display a message?

An even easier way to do that is by using the "Code" button that's in the row of formatting buttons above the post editor. Simply select all of the text you want to be formatted as code and then click the "Code" button. That will add the appropriate code tags around the selected text.

Syntax Highlighting Support
By default, the code tag will use Java syntax highlighting rules but highlighting for other languages like Python, XML, and SQL is also supported. The easiest way to change the language rules used for syntax highlighting is to select the language from the dropdown list (it shows "Java" by default) before you click the Code button. If you already know what the right value is for the language you want, you can also add that value manually in the code start tag. For Java, it would be code=java, for Python, it would be code=python, code=xml for XML, and code=sql for SQL. Check out the dropdown list to the left of the "Code" button in the message editor window to see all the languages supported.

Pro Tip: Don't try to use other formatting tags inside code tags
Only write code inside the code tags. Anything else in the tags makes it difficult for people to help you. Don't add any formatting tags; if the code contains [b]...[/b] tags or [color=#x003f00]...[/color] tags, people replying to your question cannot copy'n'paste the code to execute it for themselves.

Pro Tip: Change the starting line number to match your actual code
By default, the code tag will also show line numbers on the left margin of the code listing, starting at line #1. This makes it easy to reference a particular line when discussing your code. For example, people can say "Look at line 5 in the code you posted."

Sometimes it helps to match the line numbers in the code you're posting to the actual line numbers that you have in your program editor. This makes it easy to talk about any error or exception messages that include line numbers. For example, if the stack trace tells you that there's an exception on line 42 of your code, you might want to post just lines 40-44 to give people an idea of what's going on around that particular line without posting all 50 lines of code you've written. However, since the code tag starts line numbers at Line 1, it makes it harder to relate the code you post to information in the exception message. It will be helpful if the line numbers in the code you posted also matched so instead of starting from 1, the line numbers would start from 40.

To do this, just add a firstline option to the code start tag, like so: [code=java:firstline[40]]. In the editor, it would look something like this:

and this is what it would look like when you post it:

I got an error message about a NullPointerException on line 42. This is what I have written:

Advanced options and examples

The RanchGuide contains more examples for using other advanced options (e.g. using a different language than Java) as well.

It's about being nice -- when you're nice to people, they'll be nice to you

Formatting your code helps spare people who read your post from wasting valuable brain cycles on deciphering it - they can use them instead to formulate the answer to your question! Taking the time to ensure that your code is presented properly shows that you respect everyone's time enough to ShowSomeEffort when posting your question. And surely it goes without saying that you will post correctly‑formatted code. Except, maybe, if you think your own poor formatting is hiding some other error.

If you forget to do this when posting some code, you can edit your post for a short period afterwards by clicking on the Edit icon at its right top.

NOTE: Please keep lines inside code blocks under 80 characters. Firstly, it's easier to read; secondly, excessively long lines tend to mess up the windowing, making threads hard to read - particularly if you don't have very high screen resolutions.

Tip: (and this applies to any post) Before you submit, always use the 'Preview' button to check your post. If you see an active scrollbar at the bottom of any of your code blocks; it means that it contains lines that are definitely too long, but 80 characters is even better.

Finally, note that most people won't even try to read your code if you post much more than about 15 to 20 lines. Before you post your entire program and say "what's wrong with this?", try to isolate the portion where the problem lies and just post that.

When possible, post an SSCCE (Short Self-Contained Correct Example).

Also take a look on our other tips on HowToAskQuestionsOnJavaRanch.
    Bookmark Topic Watch Topic
  • New Topic