Today: ethics: privacy, program style: readability and decomposition, bits and bytes

Ethic: Privacy

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

Byte

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
Number of bits Different Patterns
1 0 1
2 00 01 10 11
3 000 001 010 011
100 101 110 111
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

"HDR" Image

Future Image Format: AVIF