Understanding Compilation and Runtime

How a machine (like your computer) works and how your brain works are two entirely different things. All machines are basically 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-sized wafer. No thought nor thinking nor 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 electronics 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 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, video, and so on.) Although you could write code by using just the 1 and 0 characters on your keyboard, it would not be 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 out a bunch of ones and zeroes. Instead, you can type out a sentence that looks more like words, as follows:

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 actually process. To keep you from writing a whole lot of code that makes no sense at all 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 actually 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:

A sample compile error caused by a mistyped statement.

Figure 12-1:

A 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 of time 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 to do what the code tells it to do. Later, when you actually run the code, the lower-level compiled code is what gets executed. The moment of time 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.

Runtime: Execute machine code

Compile: Convert source code to "machine code"

Runtime: Execute machine code

Figure 12-2:

VBA source code gets compiled before being executed.

0 0

Post a comment