1 of 37

Computer science you might (not) want to know

Andy Andrea�Rails Conf 2022

2 of 37

Who I am and who I am not

3 of 37

Things to know

  • CS: Computer Science
  • Space: Memory (typically RAM)

  • Whenever we’re discussing logs/logarithms, assume base 2

  • For further information:
    • 🔎 refers to a term you can search for more information

4 of 37

Part 1: Numbers

5 of 37

6 of 37

What exactly is a number anyway?

Base 10

7053

7*103 + 0*102 + 5*101 + 3*100

7*1000 + 0*100 + 5*10 + 3*1

7 of 37

What exactly is a number anyway?�Bases

Base 2/Binary

Base 10/Decimal

Base 12/Dozenal/Duodecimal

0

0

0

1

1

1

10

2

2

11

3

3

100

4

4

101

5

5

110

6

6

111

7

7

1000

8

8

1001

9

9

1010

10

a

1011

11

b

1100

12

10

1101

13

11

8 of 37

Binary

11112

1*23 + 1*22 + 1*21 + 1*20

8 + 4 + 2 + 1

1510

  • Just another base where the max value for a digit is 1 instead of 9
  • A single digit is a bit
    • Eight bits is a byte

111110

1*103 + 1*102 + 1*101 + 1*100

1000 + 100 + 10 + 1

111110

9 of 37

Fractions: decimal versus bicimal

  • As in base 10, we define a certain position to be the radix point

481.235 in decimal

110.011 in bicimal (6.375 in decimal)

102

101

100

.

10-1

10-2

10-3

100

10

1

.

1/10

1/100

1/1000

4

8

1

.

2

3

5

22

21

20

.

2-1

2-2

2-3

4

2

1

.

1/2

1/4

1/8

1

1

0

.

0

1

1

10 of 37

Fractions

  • Many numbers can only be represented by a formula or an infinitely long decimal
    • e.g. ⅓10 → 0.3333333…10
  • Numbers can be represented with a finite number of digits in one base but not another
    • 10 → 0.13 → 0.412
    • 0.210 → 0.00110011...2
  • Infinite digits requires infinite storage

🔎 Why base 12 is better�🔎 How floating point numbers are stored (exponent, significand, etc)

11 of 37

The float problem

Solution 1: truncate to a certain number of digits and round

  • Ruby’s Float
  • Pros
    • Space
  • Cons
    • Imprecise

12 of 37

The float problem

Solution 2: Store fractions as an equation, e.g. ⅓

  • Ruby’s Rational
  • Pros:
    • Precise
  • Cons
    • Having to track numerators and denominators can be expensive, hard to read

13 of 37

The float problem

Solution 3: Store fractions as an array of digits with an exponent

  • Ruby’s BigDecimal
  • Pros:
    • Precise
  • Cons
    • Space

🔎 The Real struct in bigdecimal.h in the BigDecimal source

🔎 BigDecimal(Variable Precision Floating Library for Ruby)

14 of 37

Other issues with numbers

  • Large numbers
    • Arbitrarily large numbers require an arbitrarily high amount of space
  • Negative numbers
  • Overflows and underflows

0111 + 1

-8..7

0111

🔎 Two’s Complement

🔎 JavaScript’s Number.MAX_VALUE vs Number.MAX_SAFE_INTEGER

🔎 Ruby’s Integer (>= Ruby 2.4) and Bignum (< 2.4)

🔎 Gandhi Civilization Overflow

15 of 37

Why do you want to know: Binary

  • When to use floats versus decimals
  • When you’re at risk for an overflow or unsupported behavior

🔎Bitwise operators and methods e.g.

    • AND (&)
    • OR (|)
    • XOR (^)
    • NOT (~)
    • Left shift (<<)
    • Right shift (>>)

16 of 37

Why do you not need to know: Binary

  • Not often very relevant to Rails apps
  • We can understand how and when to use something even if we don’t exactly how it works

17 of 37

Part 2: Algorithms

18 of 37

“Algorithm” defined

“A process or set of rules to be followed in calculations or other problem-solving operations, especially by a computer”

https://www.lexico.com/en/definition/algorithm

19 of 37

Sorting algorithms

20 of 37

Comparing algorithms

21 of 37

Algorithmic analysis

  • When f(n) is an algorithm that takes input of length n
    • O(f(n)) (“Big O”) is basically the worst case
    • Ω(f(n)) (“Big Omega”) is basically the best case
    • Θ(f(n)) (“Big Theta”) is basically the average case
  • Quick sort
    • O(n2)
    • Ω(n*log(n))
    • Θ(n*log(n))

22 of 37

Time/space complexity

  • From best to worst:
    • 1
    • log(n)
    • √n
    • n
    • n*log(n)
    • n2
    • n3
    • 2n
    • n!

23 of 37

Constant time (O(1))

One line of code gets executed one time regardless of input

24 of 37

Linear time (O(n))

The two lines within our block get executed n (or numbers.length) times giving us 2n

The puts gets executed once giving us 1

0

25 of 37

Linear time (O(n))

  • Total number of executions: 2n + 1 => O(n)
  • Constants/lesser terms become negligible as n approaches Infinity
  • Effect of coefficients depend on operation and platform

26 of 37

Algorithms in real life

27 of 37

Algorithms in real life: O(n) example

28 of 37

Algorithms in real life: Improving on O(n)

29 of 37

Algorithms in real life: Improving on O(n)

30 of 37

Algorithms in real life: Improving on O(n)

31 of 37

The binary search algorithm

🔎Depth-first search

🔎Breadth-first search

32 of 37

Binary search in the wild: git bisect

man git-bisect

🔎RSpec bisect

33 of 37

Why do you want to know: Algorithms

  • Write and use more efficient solutions/data structures
  • Good to know how things will scale as data increases in size
  • Shared language

34 of 37

Why do you not want to know: Algorithms

  • Algorithmic complexity is often not the bottleneck for web apps
    • Database calls, network requests should take much longer
  • Algorithmic complexity is a poor metric in real applications
  • Algorithms can be misused
  • Ruby/Rails/databases/etc hide a lot of implementation and optimization

35 of 37

Conclusion: why do you want to know computer science?

  • Interviews
  • Shared language
  • Increased understanding of some tools
  • Handy when doing lower level or more CS-heavy work

36 of 37

Conclusion: why do you not want to know computer science?

  • Getting caught up in theory can impact reality
  • Disconnect between practical software development/academic CS/hiring

37 of 37

Conclusion: why do you not want to know computer science?

  • Getting caught up in theory can impact reality
  • Disconnect between practical software development/academic CS/hiring

  • You don’t need to know it because you can learn it