Variable

From Jonathan Gardner's Tech Wiki

Jump to: navigation, search

Introduction

In most programming languages, variables are defined to temporarily hold values.

What you can do with a variable

  • Define a new variable in either the static or dynamic scope.
  • Change the value of the variable. (Some languages like Haskell do not allow this.)
  • Access the current value.

Typing

Some languages are statically typed, while others are dynamically typed.

Statically typed languages allow only certain types of values to be stored in certain variables. Normally, as the variables are defined, their type is also specified. Some languages, such as Haskell, can infer the type, while others, such as C and Java, need specific directions. Almost every statically typed language allows the "any" type.

Dynamically typed languages allow allow any value for any variable. This is equivalent to declaring all variables as type "any" in a statically typed language.

Languages may also have strong or weak typing. This refers to how well values retain their type.

Strong typing means that you cannot access the internals of the value nor treat the value as if it were a completely different type. These languages carry the type information as part of the value, and restrict how the value can be used based on that.

Weak typing means that you can easily access the internals of the value and treat the value as if it were a completely different type. These languages do not carry the type information as part of the value, and do not restrict how the value can be used.

Python is a dynamic, strong type language. C is a static, weak language. Java is a static, semi-strong language. (Some values are strong, while others are not.)

Why would someone choose a static language over a dynamic one? It is believed that there are fewer bugs because the types are enforced at the language level. This is particularly important for weak languages.

Why would one choose a dynamic language? Usually, these programmers believe that static typing is something the compiler can handle for them, and besides, they end up using the "any" type for more than a few situations. These programmers tend to not rely on the compiler for program correctness.

Why would one choose a weak language? Usually, for speed and simplicity. Having to check the type of values slows down the program and increases the memory footprint.

Why would one choose a strong language? For robustness at the cost of speed and storage.

As of today, I believe that dynamic, strong languages are more productive than the alternatives. However, Haskell is rocking the boat, showing that static languages can be useful, as long as the syntax allows a proper typing scheme and the compiler does the vast majority of the work.

Personal tools