What is monospaced font?

Monospaced font is the preferred typeface for printing computer program source code. Pick up any programming textbook and examine a listing of the source code. Compare the type against that used for printing the narrative. A monospaced font is one in which every character takes up the same amount of horizontal space.

Notice the difference between these two examples:
Monospaced Font
Proportional Font
Notice how each character takes up the same amount of space on the page. So the letters M and I use the same amount of space In this example each character takes up an amount of space that is proportional to the width of the character. So M takes up more space than I.

 

Similarly, every programmer's IDE that I've ever seen displays the source code in a monospaced font. When viewed in an IDE, source code always looks like the example on the left, never on the right.
 
An IDE display looks like this ... never like this. 
if (args.length > 0)
choice = args[0].charAt(0);

if (choice != 'H' && choice != 'h')
{
   System.out.println("Enter IP address:");
   connectIP = stdIn.readLine();
   if (connectIP == "")
      connectIP = "127.0.0.1";

if (args.length > 0)
choice = args[0].charAt(0);

if (choice != 'H' && choice != 'h')
{
   System.out.println("Enter IP address:");
   connectIP = stdIn.readLine();
   if (connectIP == "")
      connectIP = "127.0.0.1";

Why is monospaced preferred for source code? When one is inspecting the code for errors, typographical mistakes are much easier to spot if the text is monospaced.

Sometimes simple typo's can be devastating. Many years ago a rocket was launched that was controlled by software with a simple typographical error. In a FOR loop, the loop initialization value was supposed to be 1 but was mistyped as I. This compiled correctly as the compiler assumed I was a variable. At runtime the loop used the value of the variable I instead of 1 and a critical defect occurred, resulting in destruction of the rocket.

Moral: Always use a monospaced font for printing source code.

In most other situations Monospaced fonts are not good to use because they interfere with comprehension. They are hard to read because they are set too loosely. Proportional fonts are superior because they are quicker to read.

What are some examples of monospaced fonts? Courier, Andale Mono, and Lucida Console are popular monospaced fonts.



 
 
Home