Understanding Code

Week 2 of Data Science for NGA LTER REU Students

Liz Dobbins

NGA LTER / Axiom Data Science

2023-07-27

Last Week

  1. Open Science
  2. Scripting/Programming/Code
  3. Version Control
  4. Data Life Cycle

Why Coding?

  • Code replicates processing steps
  • Code reduces errors
  • Documented code creates a record of processing steps
  • Other people’s code = less work!
    • Don’t reinvent
    • Use standard libraries when possible

How Do You Learn Coding?

  • Learn the syntax
  • Practice
    • Really helps to have specific problem to solve
  • Look at examples

Programmers spend more time reading code than writing code

What can cause problems?

  • Short term memory
    • Limited to 2-6 items
    • Works better when recognize patterns
  • Long term memory
    • Practice
    • Memorization
  • Working Memory
    • Tricks

Extraneous Cognitive Load


6 + 8


vs


a = 6
b = 8

z = a + b

Trick 1: Comments

Add human readable (non-executable) annotations within the code

  • Document code purpose and design
  • Should not need to explain every single step

Trick 2: State Table

Write down the values of variable for each step in the code

x = 12
y = 5
z = y
y = x
x = z
x y z
12
12 5
12 5 5
12 12 5
5 12 5

Trick 3: Refactoring

Transform code to “improve” the internal structure without changing the external behavior

  • Make a copy and change it
  • Replace syntax you don’t understand
  • Perhaps revert changes once you figured it out (source control)

Trick 4: Self-Documenting Code

Write code using human-readable names and phrases that reflects the symbols’ meaning

  • Descriptive variable names
  • Repeated tasks should be functions (with descriptive names)
def swap_values(first_value, second_value):
    holding = second_value
    second_value = first_value
    first_value = holding
    ...

Trick 5: Tell your troubles to a Duck

Or any little toy you have sitting on your desk

Trick 6: Integrated Development Environment (IDE)

Application that provides tools for programming

  • Debugging
  • Tracking variables
  • Syntax checking
  • Autocomplete

Let’s Take a Break