What is Code?

A coding conversation with Steve Rogers

Blanco Public Library

28 Jun 2016

Google Definition of Code

noun

  1. a system of words, letters, figures, or other symbols substituted for other words, letters, etc., especially for the purposes of secrecy.

  2. Computing: program instructions. "hundreds of lines of code"

  3. a systematic collection of laws or regulations e.g. "the criminal code"

  4. a set of conventions governing behavior or activity in a particular sphere; e.g. "a stern code of honor"

verb

  1. convert (the words of a message) into a particular code in order to convey a secret meaning, e.g. "only Mitch knew how to read the message—even the name was coded"

  2. write code for (a computer program). "most developers code C++ like C"

What are computers and why do they need code?

  • Computers are machines that compute/control things for us.

    • Analog, Digital, Quantum

    • Electronic, Mechanical, Hydraulic, Chemical, Biological

    • Programable, or Not

Are these computers?

  • no, but ...

Clocks with Extras -> Computers

  • Arithmetic and Logic

  • Memory

    • Transient
    • Persistent
  • Input Devices

  • Output Devices

  • Stored Program Digital Computers

Turing Machine

  • Invented by Alan Turing in 1936.

  • Abstract computer that can simulate any computer algorithm

Some Low Level Codes

Type Alphabet Size
Morse Code . - 2
Binary Code 0 1 2
Genetic Code C G A T 4
Hexadecimal Code 0 1 2 3 4 5 6 7 8 9 A B C D E F 16
ASCII Code 0...9, a..z, A...Z, ... 256

But we want to code at a higher level than binary or hex, so ...

Computer Languages

Code Flight

  • Assembly language

  • Fortran

  • Cobol

  • Lisp

  • C

  • APL

  • Java

  • Javascript

  • Python

What does code to print "Hello World!" look like in these languages.

Hello World in Assembly Language

section     .text
global      _start                              ;must be declared for linker (ld)

_start:                                         ;tell linker entry point

    mov     edx,len                             ;message length
    mov     ecx,msg                             ;message to write
    mov     ebx,1                               ;file descriptor (stdout)
    mov     eax,4                               ;system call number (sys_write)
    int     0x80                                ;call kernel

    mov     eax,1                               ;system call number (sys_exit)
    int     0x80                                ;call kernel

section     .data

msg     db  'Hello, world!',0xa                 ;our dear string
len     equ $ - msg                             ;length of our dear string

Hello World in Fortran

 PROGRAM HELLOWORLD
 10 FORMAT (1X,11HHELLO WORLD)
 WRITE(6,10) 
 END

Hello World in Cobol

 IDENTIFICATION DIVISION.
 PROGRAM-ID. Hello.
 ENVIRONMENT DIVISION.
 DATA DIVISION.
 PROCEDURE DIVISION.
    Display 'Hello, World'.
    STOP RUN.

Hello World in Lisp

    (DEFUN HELLO-WORLD ()
    (PRINT (LIST 'HELLO 'WORLD)))

Hello Word in C

 #include <stdio.h>
 #include <stdlib.h>


 int main(void)
 {
  printf("Hello, world\n");
  return EXIT_SUCCESS;
 }

Hello World in APL

'Hello World'

Hello World in Java

 public class Hello {
    public static void main(String []args) {
        System.out.println("Hello World");
    }
 }

Hello World in Javascript

  document.writeln("Hello, World");

Hello World in Python

 print "Hello World"

A Larger Example

Project Euler Problem 1

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000.

  • Solutions in
    • Java
    • Python
    • J

Java Solution

/* 
 * Solution to Project Euler problem 1
 * by Project Nayuki
 * 
 * https://www.nayuki.io/page/project-euler-solutions
 * https://github.com/nayuki/Project-Euler-solutions
 */


public final class p001 implements EulerSolution {

    public static void main(String[] args) {
        System.out.println(new p001().run());
    }


    /* 
     * Computers are fast, so we can implement this solution directly without any clever math.
     * A conservative upper bound for the sum is 1000 * 1000, which fits in a Java int type.
     */
    public String run() {
        int sum = 0;
        for (int i = 0; i < 1000; i++) {
            if (i % 3 == 0 || i % 5 == 0)
                sum += i;
        }
        return Integer.toString(sum);
    }

}

Python Solution

# 
# Solution to Project Euler problem 1
# by Project Nayuki
# 
# https://www.nayuki.io/page/project-euler-solutions
# https://github.com/nayuki/Project-Euler-solutions
# 


# Computers are fast, so we can implement this solution directly without any clever math.
def compute():
    ans = sum(x for x in range(1000) if (x % 3 == 0 or x % 5 == 0))
    return str(ans)


if __name__ == "__main__":
    print(compute())

J Solution

NB. Solution to Project Euler Problem 1
NB. by Håkan Kjellerstrand
NB. http://www.hakank.org/j/
 +/I.+./0=3 5|/i.1000

What is a Coder?

  • Someone who writes code

  • You'll also hear:

    • Computer Programmer
    • Software Hacker
    • Software Developer
    • Software Designer
    • Software Engineer
    • Software Architect
  • but ...

There's more to Being a Coder

  1. Understand the problem.
  2. Research the problem and possible solutions.
  3. Formulate a solution.
  4. Write code to implement the solution (or part of it).
  5. Test your code.
  6. Debug the code and correct any issues.
  7. Repeat 1 - 6 until your code correctly solves the problem.
  8. If your code is ugly, make it beautiful.
  9. If necessary, optimize your code for improved performance.

Should you learn to code?

  • It depends...

In [1]:
def compute():
    ans = sum(x for x in range(1000) if (x % 3 == 0 or x % 5 == 0))
    return str(ans)


if __name__ == "__main__":
    print(compute())


233168

Further Exploration