Understanding Compilation and Runtime

How a machine (like your computer) works and how your brain works are two entirely different concepts. All machines are basically as dumb as rocks because they're just machines. Your computer is nothing more than a mindless machine that can pump a few billion instructions per second through a little toenail-size chip. No thought or thinking or awareness is involved in any of that. It's all just electrons zooming around at the speed of light in a controlled manner inside a small area.

Each of those zooming electron creates a little friction as it travels, like when you rub the palms of your hands together really fast. That friction is what causes your computer to heat up.

Programmers often refer to how a machine processes information as low-level. For example, by the time information gets to the processor in your computer, that information is nothing more than a string of ones and zeroes, something like this:

0010101001100010101011010100001110101011010101101010101010101011 0101000111110101011000111110101010111000001010101100011001110001 1111010011000110001100111110000011101010111000111001101011011

In ProgrammerSpeak, you refer to the preceding lines as low-level machine language or machine code.

Human brains don't process information as ones and zeroes. Human brains process and communicate information by using higher-level concepts, like words, sentences, and paragraphs (not to mention pictures, sound, and video). Although you could write code by using just the 1 and 0 characters on your keyboard, it would be neither easy nor quick — thus the invention of high-level programming languages.

A high-level programming language is one that uses words and sentences, rather than ones and zeroes, to control the computer. For example, VBA is a high-level language. When you want VBA to do something — like open a form named MyForm — you don't have to type a bunch of ones and zeroes. Instead, you can type a sentence that looks more like words, like this:

DoCmd.OpenForm "MyForm"

The code that you type into the VBA Editor is often referred to as source code. Every line of source code that you type needs to be compiled (translated) into a lower-level language that the computer can process. To keep you from writing a whole lot of code that makes no sense to the computer (which makes for extremely difficult debugging), the VBA Editor quickly compiles each line of code you type the moment you finish typing that line.

Note that the VBA Editor doesn't run (or execute) each line the moment you type it. Rather, it just compiles each line to make sure that when you do run the code, each statement in that code will work. When you type a line of code that VBA can't translate to lower-level machine code, the VBA Editor gives you a Compile error message, like the example shown in Figure 12-1, to let you know that there's a problem with that line.

Figure 12-1:

Sample compile error caused by a mistyped statement.

Figure 12-1:

Sample compile error caused by a mistyped statement.

The real problem with the statement in Figure 12-1 is the comma (, ) between DoCmd and OpenForm. That comma should be a period.

Programmers refer to the brief instant when your code is converted to a lower-level language as compilation or compile time. At compilation, the source code gets converted to the lower-level language that the computer needs in order to do what the code tells it to do. Later, when you run the code, the lower-level compiled code is what gets executed. The moment when the code is executed is runtime. Figure 12-2 illustrates the basic idea.

Compile: Convert source code to "machine code"

Figure 12-2:

VBA source code gets compiled before being executed.

0 0101101 01101001 10110110 IQILOUC OOCOOOll 11101011 11011011 01011001

0011C 1101C 1010C 11001 11101 01101 0101C 01101

0C11101 1101011 111100C 011011C 0110101 1C11001 110 HOC lCllOll

01101011 oioicioi 00101010 10101111 10110001 10110110 11001011 OllOlOlO

Runtime: Execute machine code

Runtime: Execute machine code

01101011 oioicioi 00101010 10101111 10110001 10110110 11001011 OllOlOlO

Was this article helpful?

0 0

Post a comment