I studied computer science so you don’t have to
#enoughcs
I studied computer science so you don’t have to
#enoughcs
.
jqno.nl/talks
/enoughcs
Check flyer for
instructions
Codesmiths
Unite!
Why
Diversity matters
Science matters
If you remember only 1 thing
Mathy version
¬(a ⋁ b) ≡ ¬a ⋀ ¬b
¬(a ⋀ b) ≡ ¬a ⋁ ¬b
Java version
!(a || b) == !a && !b
!(a && b) == !a || !b
if (!(product.price() <= 10 || order.amount() <= 5)) {
applyDiscount();
}
if (!(a || b)) {
applyDiscount();
}
a = product.price() <= 10
b = order.amount <= 5
if (!a && !b) {
applyDiscount();
}
a = product.price() <= 10
b = order.amount <= 5
if (!(product.price() <= 10) && !(order.amount() <= 5)) {
applyDiscount();
}
if (product.price() > 10 && order.amount() > 5) {
applyDiscount();
}
a |
b |
!(a || b) |
!a && !b |
---|---|---|---|
0 | 0 | 1 | 1 |
0 | 1 | 0 | 0 |
1 | 0 | 0 | 0 |
1 | 1 | 0 | 0 |
a |
b |
!(a && b) |
!a || !b |
---|---|---|---|
0 | 0 | 1 | 1 |
0 | 1 | 1 | 1 |
1 | 0 | 1 | 1 |
1 | 1 | 0 | 0 |
If you remember only 2 things
1999 - 2005
│ EqualsVerifier │ jqno.nl │ jqno
#enoughcs
Lots of interesting people
1930 - 2002
“Testing shows the presence, not the absence of bugs.”
“The question of whether machines can think is about as relevant
as the question of whether submarines can swim.”
“The teaching of BASIC should be rated as a criminal offence:
it mutilates the mind beyond recovery.”
{Precondition}
Statement
{Postcondition}
{x = 3}
x += 1;
{x = 4}
Q.E.D.
int[] haystack = ...;
int needle = ...;
int i = 0;
while (haystack[i] != needle) {
i += 1;
}
var b: array[0..N] of int = ...;
var n: int = ...;
i := 0
; do b[i] ≠ n →
i := i + 1
od
var b: array[0..N] of int = ...;
var n: int = ...;
{ ⟨∃x : 0 ≤ x < N : b[x] = n⟩ }
i := 0
; do b[i] ≠ n →
i := i + 1
od
var b: array[0..N] of int = ...;
var n: int = ...;
{ ⟨∃x : 0 ≤ x < N : b[x] = n⟩ }
i := 0
{ i = 0 ⋀ ⟨∃x : 0 ≤ x < N : b[x] = n⟩ }
; do b[i] ≠ n →
i := i + 1
od
var b: array[0..N] of int = ...;
var n: int = ...;
{ ⟨∃x : 0 ≤ x < N : b[x] = n⟩ }
i := 0
{ i = 0 ⋀ ⟨∃x : 0 ≤ x < N : b[x] = n⟩ }
; do b[i] ≠ n →
{ 0 ≤ i < N ⋀ ⟨∀x : 0 ≤ x ≤ i : b[x] ≠ n⟩ ⋀ ⟨∃x : i < x < N : b[x] = n⟩ }
i := i + 1
od
var b: array[0..N] of int = ...;
var n: int = ...;
{ ⟨∃x : 0 ≤ x < N : b[x] = n⟩ }
i := 0
{ i = 0 ⋀ ⟨∃x : 0 ≤ x < N : b[x] = n⟩ }
; do b[i] ≠ n →
{ 0 ≤ i < N ⋀ ⟨∀x : 0 ≤ x ≤ i : b[x] ≠ n⟩ ⋀ ⟨∃x : i < x < N : b[x] = n⟩ }
i := i + 1
{ 0 < i < N ⋀ ⟨∀x : 0 ≤ x < i : b[x] ≠ n⟩ ⋀ ⟨∃x : i ≤ x < N : b[x] = n⟩ }
od
var b: array[0..N] of int = ...;
var n: int = ...;
{ ⟨∃x : 0 ≤ x < N : b[x] = n⟩ }
i := 0
{ i = 0 ⋀ ⟨∃x : 0 ≤ x < N : b[x] = n⟩ }
; do b[i] ≠ n →
{ 0 ≤ i < N ⋀ ⟨∀x : 0 ≤ x ≤ i : b[x] ≠ n⟩ ⋀ ⟨∃x : i < x < N : b[x] = n⟩ }
i := i + 1
{ 0 < i < N ⋀ ⟨∀x : 0 ≤ x < i : b[x] ≠ n⟩ ⋀ ⟨∃x : i ≤ x < N : b[x] = n⟩ }
od
{ 0 ≤ i < N ⋀ b[i] = n ⋀ ⟨∀x : 0 ≤ x < i : b[x] ≠ n⟩ }
Q.E.D.
Proving algorithms is hard
Speaking of algorithms
? - 850
? - 850
? - 850
Algorithm
We live in an
age of libraries
Implement an algorithm
Implement an algorithm
Prove an algorithm
Implement an algorithm
Prove an algorithm
Test an algorithm
Implement an algorithm
Prove an algorithm
Test an algorithm
Choose an algorithm
how?
Have someone do it for you
1918 - 2020
2016
Complexity
How many steps?
N = input size
public int linearSearch(int needle, int[] haystack) {
for (int i : haystack) {
if (haystack[i] == needle) {
return true;
}
}
return false;
}
N steps
public int get(int[] ints, int index) {
return ints[index];
}
1 step
public boolean hasDuplicates(int[] ints) {
for (int i : ints) {
for (int j : ints) {
if (i != j && ints[i] == ints[j]) {
return true;
}
}
}
return false;
}
N2 steps
public int fibonacci(int i) {
if (i <= i) {
return i;
}
return fibonacci(i - 2) + fibonacci(i - 1);
}
2N steps
Brute-forcing travelling salesman
N! steps
log2 N steps
log2 N steps
🤔🤔🤔
Way less than N steps
Abstraction
over complexity
1938 -
“Beware of bugs in the above code;
I have only proved it correct, not tried it.”
“Premature optimization is the root of all evil.”
“We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3%.”
steps | Big O |
---|---|
1 | O(1) |
log N | O(log N) |
N | O(N) |
N2 | O(N2) |
2N | O(2N) |
steps | Big O |
---|---|
2N | O(N) |
42N2 + 2N + 3 | O(N2) |
log2 N | O(log N) |
N | 2 | 10 | 100 | 1000 |
---|---|---|---|---|
O(1) | 1 | 1 | 1 | 1 |
O(log N) | 1 | 3,3 | 6,6 | 10 |
O(N) | 1 | 10 | 100 | 1000 |
O(N2) | 1 | 100 | 10.000 | 1.000.000 |
O(2N) | 2 | 1024 | 1,2×1030 | 1,1×10301 |
O(N!) | 1 | 3.628.800 | 9,3×10157 | 🤯 |
N | name |
---|---|
O(1) | constant |
O(log N) | logarithmic |
O(N) | linear |
O(N2) | quadratic |
O(2N) | exponential |
O(N!) | factorial |
N | |
---|---|
O(1) | |
O(log N) | |
O(N) | |
O(N2) | ↑ Polynomial |
O(2N) | ↓ Slow |
O(N!) |
Why do we care about this?
1912 - 1954
2014
Disclaimer
Cracking codes
worse than polynomial
Verifying codes
polynomial
“nondeterministic-polynomial”
finding an answer: slow
checking an answer: P
Obviously, P ≠ NP
jè.
In practice
1906 - 1992
Know the big O of your algorithms
Know your
data structures
operation | big O |
---|---|
access | O(1) |
search | O(N) |
insert | O(N) |
append | O(1) |
operation | big O |
---|---|
access | O(N) |
search | O(N) |
insert | O(1) |
append | O(N) |
operation | big O |
---|---|
access | n/a |
search | O(1) |
iteration | O(N) |
insert | O(1) |
Immutable collections
Vavr, Eclipse Collections, Guava
No:
ArrayList<String> myList = new ArrayList<>();
Yes:
List<String> myList = new ArrayList<>();
Yes:
List<String> myList = new LinkedList<>();
Yes:
List<String> myList = new CopyOnWriteArrayList<>();
Let’s
wrap up
1936 -
2017
Thing 1:
De Morgan’s Laws
Thing 2:
Truth tables
Complexity
& Big O
Choose wisely
Data structures
Choose wisely
Now you know enough CS!
What’s next?
Keep this stuff in the back of your mind
Look it up when you need to decide
Tip: Google “Big O cheat sheet”
Experiment
Experiment
Tip:
Implement your own HashMap!
Experiment
Advanced tip:
Implement your own compression algorithm!
#enoughcs
image credits: see website