Symbols
&
Data Types

© 2017 Morgan C. Benton code4your.life

Symbols

The ability to create and manipulate symbols distinguishes humans from nearly every other organism on the planet, and may be at the root of our intelligence.

 

Computer programmers explicitly harness the power of symbols to build software.

© 2017 Morgan C. Benton code4your.life

Two Types of Symbols

  • Variables: symbols whose values can change
  • Constants: symbols whose values can't change

© 2017 Morgan C. Benton code4your.life

// examples of constants
const KPM = 1.609344; // kilometers per mile
const GPL = 0.264172; // gallons per liter

// examples of variables
let mpg = 30; // miles per gallon
let kpl;      // kilometers per liter

// calculate kpl
kpl = mpg * KPM * GPL;

Key Issues

Other than the basic differences, there are basically two things to worry about:

  1. What to name your variable or constant
  2. What data type it has

© 2017 Morgan C. Benton code4your.life

What's in a name?

That which we call a rose
By any other name would smell as sweet.

--Shakespeare, Romeo & Juliet (II, ii, 1-2)

TAKE HOME MESSAGE:

It's important how
you name stuff.

© 2017 Morgan C. Benton code4your.life

Naming Conventions

  • Rules for how to determine the names of things like variables, constants, projects, and files
  • Enforced by people, NOT software (mostly)
  • Evolved to help people collaborate on projects over time, keep things organized, not lose them, etc.

Wikipedia article: https://en.wikipedia.org/wiki/Naming_convention_(programming)

© 2017 Morgan C. Benton code4your.life

Common Strategies

  • Abbreviate: mpg or MPG
  • Camel Case: milesPerGallon or MilesPerGallon
  • Snake Case: miles_per_gallon
  • Kebab Case: miles-per-gallon
// GOOD: short, meaningful symbol names
const KPM = 1.609344; // km/mile
const GPL = 0.264172; // gal/liter
let mpg = 30; // miles/gal
let kpl;      // km/liter

// calculate kpl
kpl = mpg * KPM * GPL;
// BAD: short but cryptic
const x = 1.609344;
const y = 0.264172;
let a = 30;
let b; 

// calculate kpl
b = a * x * y;

© 2017 Morgan C. Benton code4your.life

Best Practices

  • Be consistent
  • Be concise
  • Be meaningful
  • Know and follow community standards

© 2017 Morgan C. Benton code4your.life

Data Types

© 2017 Morgan C. Benton code4your.life

What are the Types?

  • Primitive/Scalar Types
    • Boolean (true/false)
    • Integer
    • Floating point
    • Character
    • String

© 2017 Morgan C. Benton code4your.life

  • Composite Types
    • Enum
    • Array
    • Object
    • Struct

Dynamically vs. Strongly
Typed Languages

  • Defining feature of a programming language
  • Important consideration in language choice

© 2017 Morgan C. Benton code4your.life

Strong

  • Symbol type defined at time of declaration
  • Can't be changed easily (or at all), but value may be converted explicitly
  • More rigid, but errors can be found at compile time, not run time
  • Examples: Java, C, C++, C#, Go, TypeScript

Dynamic

  • Symbol type not defined, inferred by usage
  • Can change on-the-fly
  • More flexible, but type-related errors can't be found until run time
  • Examples: JavaScript, Python, Ruby, R, PHP

© 2017 Morgan C. Benton code4your.life

Code Examples

© 2017 Morgan C. Benton code4your.life

There are a lot of differences in how data types are implemented across various languages. These examples are not exhaustive, but should give you an idea and also motivate you to study further...

JavaScript

// JavaScript is dynamically typed
// variables need not be declared
// before use
x = undefined;  // undefined
x = true;       // boolean
x = 3;          // number
x = NaN;        // number
x = Infinity;   // number
x = "3";        // string
x = () => {};   // function
x = null;       // object
x = [];         // object
x = {};         // object
x = new Date(); // object

© 2017 Morgan C. Benton code4your.life

Primitive Types

  • undefined
  • boolean
  • number
  • string

Object types

  • object
  • function   
  • array
  • null

See this survey by Douglas Crockford for a good overview

PYthon

# Python is dynamically typed; variables
# do not need to be declared before use
x = True        # bool
x = "hi"        # str
x = 3           # int
x = 3.0         # float
x = 3j          # complex
x = None        # NoneType (null value)
x = [1, 2, 3]   # list (an array)
x = (1, 2, 3)   # tuple (immutable list)
x = {1, 2, 3}   # set
x = {"one": 1}  # dict (a hash table)
x = lambda x: x # function
x = b"hello"    # bytes (binary data)

© 2017 Morgan C. Benton code4your.life

  • Everything is an object
  • Every expression has a boolean value
  • Important types:
    • boolean
    • number
    • string
    • byte
    • list
    • tuple
    • set
    • dictionary

R

> # R is dynamically typed; variables do not
> # need to be declared before using them
> x <- 5.25             # numeric
> x <- 5                # numeric
> is.integer(x)         # FALSE
> x <- as.integer(5)    # integer
> is.integer(x)         # TRUE
> x <- 1 + 2i           # complex
> x <- TRUE             # logical
> x <- "hello"          # character
> x <- c(1, 2, 3)       # numeric vector (array)
> x <- c("1", "2", "3") # character vector

> x <- matrix(c(1,2,3,4), nrow=2,
              ncol=2, byrow=TRUE)
> x                     # matrix (2D array)
     [,1] [,2]
[1,]    1    2
[2,]    3    4

> x <- list(c(1, 2, 3), # list (array of vectors)
        c("1","2","3"))

> # data frames are special and unique to R
> # and worthy of study on their own

© 2017 Morgan C. Benton code4your.life

  • Everything is an object
  • Important types:
    • numeric
    • integer
    • complex
    • logical
    • character
    • vector (array)
    • matrix (2D array)
    • list
    • data frame

See this R Tutorial for a good overview

Ruby

# Ruby is dynamically typed; variables
# do not need to be declared before use
x = nil                  # Nil (null)
x = true                 # Boolean
x = 5                    # Integer
x = 5.0                  # Float
x = "hello"              # String
x = [1, 2, 3]            # Array
x = {"one": 1, "two": 2} # Hash
x = :x                   # Symbol

# all looks pretty standard except Symbol...
# what the hell is Symbol?

© 2017 Morgan C. Benton code4your.life

  • Everything is an object
  • Primary types:
    • Nil
    • Boolean
    • Numeric
      • Integer
      • Float
    • String
    • Array
    • Hash
    • Symbol

See this wikibook for a good overview (including an explanation of the mysterious Symbol type)

PHP

# PHP is dynamically typed; variables
# do not need to be declared before use
$x = null;                  # NULL
$x = true;                  # boolean
$x = 5;                     # integer
$x = 5.0;                   # double
$x = 'hello';               # string
$x = [1, 2, 3];             # array
$x = (object) ["one" => 1]; # object
$x = fopen('x.txt', 'w');   # resource

# a resource is something like a file
# reference or a network or database
# connection

© 2017 Morgan C. Benton code4your.life

  • Types:
    • NULL
    • Boolean
    • Integer
    • Double
    • String
    • Array
    • Object
    • Resource

W3Schools has good tutorials on most things, and their PHP data type overview is no exception

Java

class Main {
  public static void main(String[] args) {
    byte    y = 0;    //  8-bit int: +/- 2^7
    short   s = 1;    // 16-bit int: +/- 2^15
    int     i = 2;    // 32-bit int: +/- 2^31
    long    l = 3;    // 64-bit int: +/- 2^63
    float   f = 4.0f; // 32-bit precision
    double  d = 5.1;  // 64-bit precision
    boolean b = true; //  1-bit
    char    c = 'a';  // 16-bit unicode char
    String hi = "hi"; // equivalent to below
    char[] h2 = {'h', 'i'}; // array of char
  }
}

© 2017 Morgan C. Benton code4your.life

  • Primitive Types:
    • Integers
      • byte
      • short
      • int
      • long
    • Floating Points
      • float
      • double
    • boolean
    • char

The Java™ Tutorial is the official place to read up on this

Go

package main
import "fmt"
func main() {
  var // indicates we're starting our 
      // variable declarations
  bool    b = true // 1-bit boolean
  string hi = "hi" // string
  // unsigned integers
  ui   uint   = 0   // 32 or 64 bit
  ui8  uint8  = 0   //  8-bit: 0 ~ 255
  ui16 uint16 = 0   // 16-bit: 0 ~ 2^16
  ui32 uint32 = 0   // 32-bit: 0 ~ 2^32
  ui64 uint64 = 0   // 64-bit: 0 ~ 2^64
  y    byte   = 0   // alias for uint8
  // signed integers
  ui   int    = 0   // same as uint
  ui8  int8   = 0   //  8-bit: +/- 2^7
  ui16 int16  = 0   // 16-bit: +/- 2^15
  ui32 int32  = 0   // 32-bit: +/- 2^31
  ui64 int64  = 0   // 64-bit: +/- 2^63
  rn   rune   = 0   // alias for int32
  // floats
  f32 float32 = 4.0 // 32-bit precision
  f64 float64 = 4.0 // 64-bit precision
  // complex numbers: 64- and 128-bit
  c64   complex64 = (1.0 + 2i)
  c128 complex128 = (1.0 + 2i)
}

© 2017 Morgan C. Benton code4your.life

In addition to the types shown to the right, Go has:

  • Array
  • Struct
  • Pointer
  • Function
  • Interface
  • Slice
  • Map
  • Channel

Try this tutorial if you're interested

Summary

Symbols, i.e. variables and constants, are a fundamental component of every programming language

 

You should have an explicit strategy for naming things

 

Whether dynamic or strong, every language implements some form of data type system which determines how memory is allocated and how symbols are stored

 

While most of the time it won't be necessary to spend a lot of time focusing on the data type of your variables, it's important to understand that it exists, and have a sense of the possible ramifications

© 2017 Morgan C. Benton code4your.life

Symbols & Data Types

By Morgan Benton

Symbols & Data Types

Introduction to the concept of symbols in modern programming languages, more commonly referred to as variables and constants. There is also an introduction to the concept of data types. Both concepts will be illustrated with code samples in various languages.

  • 1,185