Type

One of the first thing a programmer learns is the various "types" that data can have.  Commonly-used types include things like strings, integers, and arrays.  Underneath the hood, all software data is, of course, composed of bits commonly represented by sequences of ones and zeros.  But typing gives structure to those bits, and not only allows programmers to understand and reason about the code they write (it makes more sense, for instance, to think of a string as letters or words rather than bits), but also prevents errors through creating restrictions on what certain types can and cannot do (for instance, you can add floating point numbers but you can't add arrays).

There are two major axes along which typing varies: a language can be either strongly or weakly typed; and a language can be statically or dynamically typed.  You can loosely think of these, respectively, as a language's ability to prevent type errors, and when a language catches type errors.

Static vs. Dynamic Typing

Statically-typed languages are also known as compiled languages.  Compiled languages are translated from the higher-level language that the programmer has used to write the code down to assembly code before execution; any errors related to type are identified during compilation and prevent its completion.  Compiled languages are compiled ahead of time, allowing type errors to be identified before the program is run.  In contrast, dynamically-typed languages are compiled at run-time.

There are trade-offs to both paradigms.  Statically-typed languages

* Do not have as many run-time errors, since these errors are caught ahead of time.

* Generally run faster, since checks are not performed at run-time

Statically-typed languages also occasionally have the ability to perform additional checks beyond just type checks - for instance, they can see if an object can actually run a method that is being called on it, and throw an error if the method is not allowed.

Dynamically-typed languages, on the other hand:

* Are generally easier to debug, since code can start running even if there are errors in some parts of the code

* Generally start up faster, since they do not need to be compiled ahead of time

Strong vs Weak Typing

It seems that no one agrees on what, exactly, makes a language strongly typed or weakly typed.  There are a number of qualities that can generally be associated with a language's typing, and impressions of a language's typing strength are generally related to how many of these qualities apply to that language, and the thoroughness of their application.  Some of the most common qualities assigned to a strongly typed language are that they

* Require that a variable's type be declared when the variable is created

* Have high type safety, which a language's inherently propensity for runtime type errors

* Prevent a variable from switching types

For example, JavaScript is considered to be a weakly typed language, and in JavaScript, you can do things like this:

let variable = "string"
console.log(typeof variable)
//prints string
variable = 4
console.log(typeof variable)
//prints integer

Our variable variable changed types without any problem.

Like static vs. dynamic typing, there are advantages and disadvantages to both systems.  Strong languages are generally more difficult and cumbersome to write, but are often easier to understand since some of the code annotation is essentially done by the mere written language of the code.  They also have less possibility, obviously, of type errors happening at compilation or run-time.  Weak languages, on the other hand, can be easier to write and understand how to write, and they can often be more flexible when it comes to maintaining code and erasing technical debt.  They also do not throw errors for the oftentimes harmless type conversions that strong typing prevents.

Comments

Popular posts from this blog

The Sorting Hat

Kadane's Algorithm

Loose Equality, the Null Operator, and Other Oddities