I Finally Understand Static vs. Dynamic Typing and You Will Too!

When I decide I want to understand something, I become obsessed until I finally do. My latest attempt? Static vs. Dynamic Typing.

Stack Overflow’s answers were confusing, long, and even contradictory. Turns out these terms are commonly misunderstood, so it makes sense that my search would prove difficult. I continued reading whatever I could find but nothing satisfied my hunger for something approachable and concise.


I’ll begin with some basic terms and gently lead you to enlightenment…


RIGHT: “man I should read that article about type systems too…”
Compiled vs. Interpreted
“When source code is translated”

Source Code: Original code (usually typed by a human into a computer)
Translation: Converting source code into something a computer can read (i.e. machine code)
Run-Time: Period when program is executing commands (after compilation, if compiled)
Compiled: Code translated before run-time
Interpreted: Code translated on the fly, during execution
Typing
“When types are checked”

"3" + 5 will raise a type error in strongly typed languages, such as Python and Go, because they don't allow for "type coercion": the ability for a value to change type implicitly in certain contexts (e.g. merging two types using +). Weakly typed languages, such as JavaScript, won't throw a type error (result: '35').

Static: Types checked before run-time
Dynamic: Types checked on the fly, during execution
The definitions of “Static & Compiled” and “Dynamic & Interpreted” are quite similar…but remember it’s “when types are checked” vs. “when source code is translated”.

Type-checking has nothing to do with the language being compiled or interpreted! You need to separate these terms conceptually.

Python Example
Dynamic, Interpreted

def foo(a):
if a > 0:
print 'Hi'
else:
print "3" + 5
foo(2)
Because Python is both interpreted and dynamically typed, it only translates and type-checks code it’s executing on. The else block never executes, so "3" + 5 is never even looked at!

What if it was statically typed?

A type error would be thrown before the code is even run. It still performs type-checking before run-time even though it is interpreted.

What if it was compiled?

The else block would be translated/looked at before run-time, but because it's dynamically typed it wouldn't throw an error! Dynamically typed languages don't check types until execution, and that line never executes.

Go Example
Static, Compiled

package main
import ("fmt"
)
func foo(a int) {
if (a > 0) {
fmt.Println("Hi")
} else {
fmt.Println("3" + 5)
}
}
func main() {
foo(2)
}
The types are checked before running (static) and the type error is immediately caught! The types would still be checked before run-time if it was interpreted, having the same result. If it was dynamic, it wouldn’t throw any errors even though the code would be looked at during compilation.

f:id:yufowose:20190411222825j:plain

Dynamic Typing

Performance
A compiled language will have better performance at run-time if it’s statically typed because the knowledge of types allows for machine code optimization.

Statically typed languages have better performance at run-time intrinsically due to not needing to check types dynamically while executing (it checks before running).

Similarly, compiled languages are faster at run time as the code has already been translated instead of needing to “interpret”/translate it on the fly.

Note that both compiled and statically typed languages will have a delay before running for translation and type-checking, respectively.

More Differences
Static typing catches errors early, instead of finding them during execution (especially useful for long programs). It’s more “strict” in that it won’t allow for type errors anywhere in your program and often prevents variables from changing types, which further defends against unintended errors.

The Discovery of DNA Fingerprinting

In September 1984, Dr. Alec Jeffreys, a geneticist from the University of Leicester in Great Britain was studying hereditary diseases in families. He was focusing on methods to resolve paternity and immigration disputes by demonstrating the genetic links between individuals. 

f:id:yufowose:20190411222553j:plain

Jeffreys' DNA fingerprint technique would be catapulted into the world of forensic science when two murders were committed not far from the University of Leicester. For the first time, genetic fingerprinting would be used to exonerate a suspect and convict the guilty person.

The quiet little village of Narborough in Leicestershire, East of Birmingham, would be put on the forensic map when two crimes shocked residents and law enforcement. In 1983, 15-year-old Lynda Mann was found raped and murdered. Three years later, Dawn Ashworth, also fifteen, was also raped and murdered.

With the use of RLFP based DNA technology, Dr. Jeffreys was asked to compare semen samples from both murders against a blood sample from a seventeen year old suspect, Richard Buckland, who was in police custody and who had confessed to the second crime. This confession raised suspicion among some detectives, as Buckland had to be 14 years old when he committed the first crime. Dr. Jeffreys' DNA tests proved that the DNA fingerprint from the semen found on the two murdered victims was not the same as Buckland's. It also proved that the same killer was responsible for both crimes.

With this information, law enforcement then took an unprecedented task to catch the killer. The first DNA dragnet was used. A total of 4,582 men, from three towns, were tested for the killer's blood type, (type A) and enzyme PGM +1 retrieved from his semen (Wambaugh, 1989) Ten per cent of the men tested had the same blood type and enzyme as the killer and their blood was subsequently tested by Jeffreys' genetic fingerprinting technique.

A local baker named Colin Pitchfork avoided having his blood tested. He gave a colleague, Ian Kelly, his passport in which he had replaced his picture with Kelly's photo. Then Pitchfork had Kelly submit his own blood for him. Months later his ploy was discovered by law enforcement when a resident came forward after hearing a conversation at a local pub in which Kelly admitted that he was paid by Pitchfork to have his blood tested. This led to the arrest of the twenty seven-year old Pitchfork. Dr. Jeffreys compared Pitchfork's DNA with the DNA of the semen found on the two victims. It was a perfect match. Instead of going to trial, Pitchfork pled guilty to both rapes and murders.

In 1987, Pitchfork became the first person in the world to be identified, captured and successfully prosecuted as a result of DNA evidence. He was sentenced to life, minimum term of 30 years, and is currently in prison for both murders. In May 2009, Pitchfork had his life sentenced reduced by 2 years.

In 1994, Professor Jeffreys was knighted by The Queen of England and today he is still a Professor in the Department of Genetics at the University of Leicester in Great-Britain.

 

Genetic identity testing involves identifying the patterns of genetic material that are unique to almost every individual. Although over 99% of the DNA sequences in the human genome are identical between individuals, a small number of sequence differences are used to distinguish all humans (1). Those different sequences are usually targeted for identity testing. The techniques that are applied in identity testing are DNA fingerprinting, DNA profiling, and DNA typing. Although there are some technical differences between these tests, the terms have been used interchangeably.

Go to:
DISCOVERY OF THE DNA FINGERPRINT
Historically, identity testing in the forensic field started with the analysis of the ABO blood group system. Later, new markers for identity and paternity identification were based on variations of serum proteins and red blood cell enzymes; eventually the human leukocyte antigen system was used (2). It was not until 20 years ago that Sir Alec Jeffreys, professor and geneticist at the University of Leicester in the United Kingdom (UK), pioneered DNA-based identity testing (3).

 

i love typing

Professor Jeffreys was interested in studying the genetic variation between individuals and had done some of the early work to detect genetic differences in humans. However, the answer did not come to him on the initial project he was interested in but rather on an unrelated project: analysis of the myoglobin gene in seal meat at the headquarters of the British Antarctic Survey. When he and his colleagues compared the myoglobin gene in seals with the human counterpart, they found that some short repeating sequences were homologous between seals and humans. When they compared those sequences with the published sequences of tandem repeats called minisatellites, they found that they were the same (4). Professor Jeffreys recognized that the repeating sequences “could be highly variable, informative genetic markers” (3). His group developed a radioactive probe, made up of short sequences, that could latch onto those repeating sequences and ultimately reveal patterns that were unique to each individual: a DNA “fingerprint” (5).

The steps involved in DNA fingerprinting are as follows. First, the DNA is extracted from the specimen (i.e., blood, semen, skin, hair). After DNA extraction, restriction enzymes are added, which work like scissors to cut the DNA into the smaller segments that are different between individuals. The DNA seg-ments are sorted by agarose gel electrophoresis and visualized by staining with ethidium bromide. A Southern blot is performed to transfer the DNA onto a membrane. A radioactive probe is applied to the membrane, and the pattern of DNA is detected by exposing the membrane to x-ray film. The result is a pattern of DNA bands that looks like a supermarket bar code. Each individual has a signature fingerprint (5).

Professor Jeffreys looked at a DNA fingerprint of a human family; he also looked at the fingerprint of a cow, a baboon, a mouse, and a tobacco plant. The pattern of DNA segments, composed of perhaps 15 to 20 bands, was different for each one. However, closer inspection of the patterns of the human family revealed that the mother and the father each had their own pattern and that the child had a composite of both, having inherited an allele from the father and the mother.