Design Computation

## What is code?

The Matrix (Warner Brothers, 1999) used the world of hackers and computers to portray a philosophical battle between choice and control. Computer code is inherently about control, exemplified by “the machines” of The Matrix.

Code is a means of control. It is a symbolic representation of computational language used to describe computational processes and reasoning.

Understanding the underlying reason for how code has evolved means learning a little about how the internals of a computer works.

### Outline

1. Introduction to Code
1. What is Code?
2. Definitions
3. Examples
4. Before diving in…

### Computation and Code

Computation is rational and mathematical, but this doesn’t mean deterministic.

More important, it embodies a way of thinking, and is thus more relevant to the process of design than to a particular formal output.

Code represents the computational process as a set of instructions. Each step is “encoded” in a particular way so it can be fed into a computer, and the computer can evaluate those steps for us.

### Linguistic Analogy

Code is organized into programming “languages” that take on some of the same characteristics as spoken languages: syntax, meaning, etc.

You already know this way of thinking. If you were to write down each step of modeling a building in Rhino (draw line, extrude line up in to a surface, duplicate surface, rotate, etc.), those steps would comprise a script that could be automated to repeat those steps. Whenever you think about what you need to model next in terms of a logical statement (“If this exists, then I should do that…”), that adds a rule to your process that, if consistent enough, could probably be encoded as well.

### Types of Code

Code is really about description. We want to be able to describe parts of a design problem carefully and precisely, working our way from abstract ideas to code-driven implementation.

1. Pseudocode
An English analog to source code.
1. Machine code
A low-level, machine-only readable representation of computer instructions. This is ultimately what the computer executes, and some programmers and computer scientists program at this level.
1. Source code

### Scripting versus Programming

Internal architectures of computers are often diagrammed as a series of “layers.” Applications are built on top of an operating system, which is built for a particular processor and computer system. Generally speaking, scripting talks to the application layer, and programming can interface to the operating system and CPU layers.

#### Scripting

Scripting was originally a process of writing code to automate applications. AppleScript, for instance, can be used to automate typical commands for software on Mac OS X. “Shell scripting” for Linux and Unix can automate file-handling tasks, compiling, installations, among others. RhinoScript is used to command Rhino, for the sake of efficiency or to add complex functionality.

#### Programming

Programming is a more general term for writing sets of instructions to command a computer or operating system. It is usually on a “lower-level” than scripting, which commands applications.

The distinction between the two is often blurry (e.g. Ruby is a full-featured programming language used as a scripting language for SketchUp), and the terms are often used interchangeably in architectural discourse (though “scripting” seems to be more fashionable than “programming”).

### Low-Level Languages

A small portion of assembly code from http://www.assembly.happycodings.com/code1.html that reproduces the Fibonacci sequence, where (starting with 1 and 1) the sum of two adjacent numbers determines the successive number: (1, 1, 2, 3, 5, 8, 13, 21, 34, …). Observe how many lines of code it takes to produce this simple sequence.

Low-level languages are those closest to the mechanical workings of a computer’s processor. They typically have a degree of human unfriendliness and take a trained eye to read them.

#### Assembly Code

The assembly code above is almost as low-level as we can get. These instructions give individual commands to tell a processor to store numbers in registers, add values together, manage a stack or queue, etc.

It is also not immediately obvious what these instructions will do when they are executed, except for the human-readable comments the programmer has inserted for us,

This is still a level above “machine code,” which is completely human unreadable and takes special software to view and edit the code in a meaningful way.

#### Machine Code

Machine code is binary code, usually represented as hexadecimal, that are the fundamental instructions for a CPU, like an Intel Core 2 Duo.

#### Binary

“Binary” is a number system that uses only two digits (instead of decimal’s ten digits) to represent numbers. It’s easy for computers to represent either 0 or 1, using the absence or presence of voltage (usually 0 or 12 volts, and sometimes -5 V and 5 V).

``````
Binary = Decimal
0 = 0
1 = 1
10 = 2
11 = 3
100 = 4
101 = 5
110 = 6
111 = 7
1000 = 8
1001 = 9
1010 = 10
1011 = 11
1100 = 12
1101 = 13
1110 = 14
1111 = 15
``````

Particular sets of binary digits have names that you may recognize.

1 digit = “bit”, 4 digits = “nibble”, 8 bits = “byte”, 16 bits = “word”, 32 bits = “dword”, 64 bits = “quadword”.

#### Other “Low-Level” Languages

C
Probably the most popular programming language in the world, C is the basis for C++, and is syntactically the basis for Java, JavaScript, Processing, and others. It is human-readable, and extended through innumerable libraries. Once compiled into machine code, well-written code is fast and extremely powerful (meaning it makes few assumptions, so the programmer has more control).

C++
The “object-oriented” version of C. It has the notion of “classes” of “objects,” or user-defined data types (instead of numbers, you can program in terms of “People” or “Chairs”). We will discuss objects and classes of objects towards the middle of the course.

### High-Level Languages

Code written in Ruby that reproduces the first n digits of the Fibonacci sequence. See how much shorter this is than the assembly code version?

High-level languages are those that are less directly related to the workings of a computer and more closely related to what a human is trying to achieve.

They could be thought of as attempting to mimic the thinking and language of humans, but this isn’t entirely accurate. They still have a kind of computerized abstraction to them, so their defining characteristic is really in the number and types of assumptions they make.

Programmers work in high-level languages when they want more automation in terms of how the inner workings of the computer are handled, so they can write code faster. The resulting programs however, often sacrifice performance and consume more memory, among other side effects. But this loss in performance often isn’t a concern for us.

#### Processing

Processing is a high-level language. We can use this fact to our advantage, since its commands are those that are more directly applicable to design and visualization tasks, like drawing shapes, specifying drawing colors, etc. All the extra instructions needed to manage a computer’s inner workings are hidden from us.

#### Other High-Level Languages

Ruby
A language developed by Yukihiro “matz” Matsumoto that has gained a large following in recent years, mainly in the web application sector. Twitter, for example, is built with a web framework called Ruby-on-Rails, which uses Ruby as a base language. Ruby is also the scripting language for SketchUp.

Python
An extremely popular programming and scripting language that is similar in structure to Ruby. It has a clarity of syntax and flexibility that many programmers find attractive, as well as a rigorous implementation of computer science concepts that other languages lack. It has numerous pre-packaged libraries for complex functionality, everything from Internet interfaces to scientific computing (so does Ruby).

Java
A language developed by Sun Microsystems that was intended as a cross-platform delivery system. That is, applications could be programmed once, then run on Linux, Unix, Windows, and Apple systems without any further effort. Java also runs on server platforms (the Enterprise Edition) and can drive applications on mobile devices as well. It is the foundation for Processing, which allows us to make applets for web browsers and applications for any system.

### Before Diving In

These lectures will be interactive, learn-by-doing sessions; they will be online so you can refer to them later.

``````
Code will be written like this for whatever lesson we're working on. You can usually just copy and paste the code into the Processing IDE window.
``````

I recommend that when you create variations of the code, you paste them into text files or into some application that manages text to record them for later reference. I also recommend you add comments to the code as we go along, so you can remember what each statement or piece of code does.

Applications like Evernote turn your code snippets into searchable databases accessible from multiple devices. (It also has incredible optical character recognition.) I just use simple text files and Spotlight in Mac OS to make code searchable.