Kodokon kodokon.com

Why code is nothing but text

Discover that every program is just a text file, and why that changes the way you work.

6 min · 3 questions

Open this lesson in Kodokon

Here is the revelation that surprises almost everyone: code is nothing but text. Not a secret format, not some mysterious binary file. A website, a phone app, a video game: it all starts from files you could open in Notepad and read character by character. There is nothing hidden inside, and that is precisely what makes programming learnable.

One caveat though: we are talking about plain text, not rich text. When you write in Word, LibreOffice or Google Docs, the file holds far more than your letters: the font, the size, the colors, the margins, all stored as invisible formatting. A code file contains only the characters, in order, without a shred of decoration. The colors you will later see in your editor are not in the file: the editor adds them on screen, to help you.

TEXT
The real contents of the file index.html, character by character:

<h1>Hello</h1>
<p>My first page.</p>

Nothing else. No font, no color, no margin.
A code file contains nothing but the characters you type.

So what about the extension, that little suffix after the dot: .txt, .html, .py, .js? It is nothing more than a label stuck on the box. It changes nothing about the contents. It is there to tell programs, and you, what the file is supposed to hold. Renaming photo.jpg to photo.txt does not turn the image into text: the contents are unchanged, only the label is lying. The other way round, the same text saved as page.txt or as page.html is strictly the same file, but your browser will only render it as a web page in the second case.

There is another consequence to all this: never write code in a word processor. Word and Google Docs autocorrect what you type, and in particular they swap your straight quotes for pretty curly typographic ones. To you, the difference is invisible. To the machine, those are two completely different characters, and your program will simply refuse to run.

TEXT
What the machine expects   "hello"     straight quotes
What Word writes           “hello”     curly typographic quotes
Another common trap        'hello'  ->  ' turns into a curly apostrophe
Two characters that look alike are not the same character.

Knowledge check

Make sure you remember the key points of this lesson.

  1. What does a code file actually contain?
    • Only the characters you typed, with no formatting
    • The characters plus the colors they are displayed in
    • A compressed format unreadable without special software
  2. What is a file extension such as .html for?
    • Telling you what the file is supposed to contain
    • Compressing the contents of the file
    • Protecting the file against changes
    • Changing the contents of the file
  3. Why shouldn't you write code in Word?
    • It automatically swaps certain characters, such as quotes
    • It cannot save files
    • It caps the number of lines allowed