Today: ethics: privacy, program style: readability and decomposition, bits and bytes
Ethic: Privacy
- Veronica Rivera - Ethics fellow - talk about privacy
- Ethic: have some privacy
- Nick editorial...
- Where are you today?
College in the liberal tradition
- College - embodies the liberal tradition
Freedom of thought
Freedom of speech
Privacy can think of as adjacent to these freedoms
- The story of the next 50 years
Democracy vs. Autocracy
- Look at WhatsApp
WhatsApp is end-to-end encrypted, so messages are private from the government
- USA:
WhatsApp is legal
- Russia, Iran, China - authortarian
WhatsApp is forbidden
- There is some association between privacy and freedom
- China also bans wikipedia
Big Picture - The Truth About Software
We'll start a the highest level, seeing the truisms that guide software building. There's software in everything, so you should know the lay of the land.
Goal #1 - Code That Computes the Correct Answer
The main thing we want from code. If code produces the wrong answer, do we really care how fast it runs?
Problem: Natural Sate of Code = Broken
"Broken" is the natural state of code. It's easy to type in some code, and have it not work. We need a plan to work in this environment. Code can work so nicely, we should keep in mind that even more easily it can fail to work.
Can You Judge Code By Looking at It?
Can you judge code correctness by looking at it? The surprising answer is - no. To really judge, you need to simulate what the loops and if-statements will with various inputs. In effect, you need to run the code to see what it does.
How To Judge - Run Tests
We need to run the code against a few inputs, checking the output for each case. If the code works against a few cases, suggests it is probably correct. It is not a 100% proof, which is surprisingly difficult or impossible to obtain, but tests are very good in practice.
Corollary: Code Not Run is Probably Buggy
Code that the computer has never run over likely has bugs in it.
This can happen if an if-test is always false in a program. This happened with the AT&T phone network, where there was some code in the phone-switching system like this.
if rare_error_condition:
code to
route around # un-noticed bug here
error condition
The error handling code within the if_statement had a simple bug in it, but those lines had never run, so nobody noticed. Until one day the if-statement was true and the code ran (for the first time) and crashed, taking out a part of the US phone system for a while.
Code tests can help with this. There are modern "code coverage" tools that look at all the tests, making sure that every line has been run in some test or other.
Goal #2 - Clean Code
Clean code with good style. This helps reduce bugs in the first place, and it's easier to fix and add features to code that is already clean. Stanford has always put an emphasis on writing clean code with good style.
Goal #3 - Run Fast
If the code is works correctly and looks good, we might also want to tune it to run fast or use less memory. For some bits of code, speed is crucial. However, the best strategy is generally getting the code working first before messing with it for maximum performance.
Program Design Strategy
Why is code written the way it is? Today we tell the outside, strategic story, driving what forms of code work best.
We'll look over to the Python guide chapters on style readability and decomposition.
Python Guide: PEP8 Tactics
Python Guide: Readable Code
Python Guide: Decomposition
Bits and Bytes
At the smallest scale in the computer, information is stored as bits and bytes. In this section, we'll look at how that works.
Bit
- a "bit", like an atom, the smallest unit of storage
- A bit stores just a 0 or 1
- "In the computer it's all 0's and 1's" ... bits
- Anything with two separate states can store 1 bit
Nick's tennis racket example
- In a chip: electric charge = 0/1
- In a hard drive: spots of North/South magnetism = 0/1
- A bit is too small to be much use
- Group 8 bits together to make 1 byte
Byte
- One byte = grouping of 8 bits
- e.g. 0 1 0 1 1 0 1 0
- One byte can store one roman character, e.g. 'A' or 'x' or '$'
How Many Patterns With N Bits?
How many different patterns can be made with 1, 2, or 3 bits?
| Number of bits | Different Patterns |
| 1 | 0 1 |
| 2 | 00 01 10 11 |
| 3 | 000 001 010 011 100 101 110 111 |
- Combare 3 bits vs. 2 bits
- Consider just the leftmost bit
- It can only be 0 or 1
- Leftmost bit is 0, then append 2-bit patterns
- Leftmost bit is 1, then append 2-bit patterns again
- Result ... 3-bits has twice as many patterns as 2-bits
- Every row - double the number of patterns of previous row
| Number of bits | Different Patterns |
| 1 | 0 1 |
| 2 | 00 01 10 11 |
| 3 | 000 001 010 011 100 101 110 111 |
- In general: add 1 bit, double the number of patterns
- 1 bit -> 2 patterns
- 2 bits -> 4 patterns
- 3 bits -> 8 patterns
- n bits -> 2n - 2 to the nth power
- number of patterns is exponential of number of bits
- Few things in life are exponential!
Compound interest (note: try to put something in 401k when young)
Spread of novel pathogen in population
- Exponential growth is so fast, it is unintuitive
| Number of bits | Number of Patterns |
| 1 | 2 |
| 2 | 4 |
| 3 | 8 |
| 4 | 16 |
| 5 | 32 |
| 6 | 64 |
| 7 | 128 |
| 8 | 256 |
One Byte - 256 Patterns
- 1 byte is a group of 8 bits
- 8 bits can make 256 different patterns
- How to store an int number in 1 byte?
- Each number gets its own pattern
e.g. binary 110 is the int 12, we're not going to worry about the details
- Imagine assigning each number its own pattern, starting with 0:
- 0 = pattern 1
- 1 = pattern 2
- 2 = pattern 3
- ...
- 254 = pattern 255
- 255 = pattern 256
- There are 256 possible patterns, so 255 is the max int stored in one byte
- pixel.red takes in a number 0..255, why?
- The red/green/blue numbers of a pixel are each stored in one byte
- That's why it's 0..255
"HDR" Image
- HDR High Dynamic Range - more than 256 values
- HDR uses 10 bits per color
- How many more colors is that than 8 bits? 258 colors?
- No it's exponential, doubling with each bit
- 8 bits = 256 colors
- 9 bits = 512 colors
- 10 bits = 1024 colors - HDR
- 10 bit HDR is maybe close to the human perceptual limit anyway
- Uses a little more space, looks better
Future Image Format: AVIF
- JPEG is 8 bits per color
- And it's compression is not the best by modern standards
- Next gen format: AVIF
- Free/open standard, like JPEG
- Compresses much better
- 10, 12 bit HDR supported
- Alternative format: HEIF heavily patented (Apple)
Data formats where you need a patent license to be permitted to look at the data, probably not a good idea
HTML, HTTP, JPEG, PNG ... free/open standards, super successful