The Gist of Static Typing
More languages today are based around Static Typing than Dynamic Typing. There are some languages that support both, but we won’t get into that here.
While statically-typed languages usually run faster than their dynamic brethren, they’re less trendy in the realm of web development and data science, although WebAssembly has been looking to shake up that game a bit. In general, these languages are better suited for games and applications, and won’t be going away any day soon!
Statically-typed languages are typically not beginner friendly, but that doesn’t mean they aren’t worth learning. Let’s jump in.
What is Static Typing?
It’s a little like this: Static Typing is a method where all variables must have a type, and that type cannot be changed during runtime.
Before we get confused here: the values of these static variables can change! They’re not like constants, whose values can’t change (and whose data types also can’t change, but I digress).
The only caveat is, quite predictably, that any new value assigned to the variable must be of the same type as the last value.
// In C++, we define a variable myInt of type int.
int myInt = 7;
// This reassignment to another int value is okay!
myInt = 8;
// This reassignment to a String is no good.
myInt = "JonOfOz";
Type checking happens at compile time. The compiler first verifies that the code doesn’t break any type rules, and if not, the program runs. Else, you’ll get type errors you need to fix first. This doesn’t guarantee you won’t run into errors, but you won’t run into ones related to type.
Static Typing has a number of benefits:
- Static Typing results in faster, leaner code, since all data types are declared and checked at compile time.
- There is less chance of running into errors in the middle of long processes due to simple mistakes. Imagine running a process that takes two days to finish, then bam, the code fails at hour 46 due to a single line where an integer was expected, but the string version of that integer gotassigned, and everything fell apart. Ouch.
Static Typing also has drawbacks:
- Debugging can be frustrating in and of itself; that’s even more so the case with statically typed languages. The error messages can be pretty vague, and you’re dead in the water if you can’t start your debugger until you compile your code… which can’t compile until you fix all of the bugs. (You can get around this, but it can be a pain.)
- Static typing is like an overprotective parent. If it sees you are about to learn a harsh lesson due to a type error, it won’t have it. This sort of sounds great, but giving the developer guardrails isn’t always a win: we learn a great deal from failures. So, what happens when we fail less?
How Does Static Typing Compare to Dynamic Typing?
For variables of both statically-typed and dynamically-typed languages, one principle is the same: whatever those variables are, they are references to addresses in memory.
In the case of the statically-typed example above, it’s referencing 7— more accurately, the memory address represented by 7.
The differences lies in the variables.
When a variable like
myInt is declared, it is bound to the
int data type for the duration of the runtime. The variable calls the shots: if it starts out as an
int and you try to assign it a new
String value, you’ll get an error.
When a variable like
myInt is declared, it’s nothing more than a reference to some address in memory. It might represent
null, it might be an
String, it doesn’t matter. As long as you don’t perform illegal operations on whatever type it is at any point in runtime, you’re good to go.