Which free font should I use to write code in my text document?

  • I'm writing some articles, mainly in blogs or small, approximately 1 paper long, printed articles.

    Now this text contains some code. Most often it is not a code block but just a method name, a variable name or a class name.

    For that I have some questions:

    • Is it a good idea to write this part of the text in another font?
    • If I chose to use another font, which one can I take to make it optimally readable?
    • If I should not chose another font, is there any other way to highlight/differentiate this part of the text? Or shouldn't I do it at all?
  • Ben Brocka

    Ben Brocka Correct answer

    10 years ago

    I think it's very important to differentiate code text from normal text as it is very much another language, and it's standard proceedure to differentiate text written in other languages--usually using italics.

    A good example of sites using code differentiation is the Stack Exchange family, as you may have noticed it's very important to differentiate code when asking programming questions, in no small part because the different, usually monospaced font, makes code easier to read.

    Here's a good list of "Good programming fonts", monospaced fonts are usually the default for readability's sake as they clearly separate letters so you never have to wonder if that's an rn or an m. Clarity of l, I and 1 (l, I and 1) are very important as well, as you can see l and I are identical using UX.SE's default font, but clearly identifiable in the nice monospaced fonts they use for code.

    While I do recommend using a different font, using other methods of highlighting can be helpful too; if you have a programmer audience they will likely appreciate some consistant variable name/method name/ect highlighting similar to popular IDEs and editors like Notepad++. I believe Stack Overflow attempts to do this.

    Very good and complete answer.

License under CC-BY-SA with attribution


Content dated before 7/24/2021 11:53 AM

Tags used