Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

From repeating code to reusing logic

In the previous lesson, you learned how to control program flow using loops and conditional statements.

That was an essential step to automate repetitive tasks, but it raises a new challenge:

What if you need to perform the exact same spatial calculation in five different parts of your notebook, or share it with a colleague?

So far, the only way to do that would be copy-pasting code. This lesson introduces a fundamental shift in how you organize your work. Instead of writing long, repetitive scripts, you will learn how to package your logic into functions.


1. From processes to reusable tools

Programming can be broken down into core actions: storing data, repeating actions, deciding outcomes, and reusing logic.

Copying and pasting complex mathematical formulas leads to cluttered code and makes fixing errors incredibly tedious. This violates the DRY (Don’t Repeat Yourself) principle, which states that repeating code makes future work much harder.

Functions allow you to move from writing “do this specific math right here” to creating a reliable, named tool that says “give me any coordinates, and I will give you the distance”. By wrapping your code in a function, you hide complicated details and can focus on the big picture of your spatial analysis.


2. Why this matters for SDS

In spatial data science, your pipelines will quickly grow complex. You will frequently need to clean coordinates, convert units, and apply different mathematical models, such as Euclidean or Haversine distances.

By keeping these operations in separate functions that call each other, your code becomes modular. If a calibration error is discovered, you only have to fix it in exactly one place inside the function definition. Furthermore, real-world spatial data is unpredictable. You will need flexible tools that can handle an unknown number of GPS waypoints or varying metadata attributes, which functions handle elegantly using flexible arguments.


3. Learning objectives

After this lesson, you will be able to:


4. Lesson structure

This lesson is structured to take you from basic mechanics to professional implementation:

  1. Why Functions Matter: The limits of copy-pasting and the power of the DRY principle.

  2. Core Mechanics: The anatomy of a function, arguments vs. parameters, and printing vs. returning.

  3. Function Design Concepts: Managing variable scope and safe default parameters.

  4. Flexible Function Interfaces: Handling arbitrary inputs with *args, **kwargs, and lambda functions.

  5. Writing Professional Functions: Creating standard docstrings, using introspection, and avoiding common bugs.

  6. Practical L4: Applying these tools to process a realistic dataset of Swiss cities and calculate different geometric distances.


5. Looking ahead

Lesson 4 is about building safe, scalable tools.

You learn not only how to write a function, but how to:

If Lesson 3 was about thinking in processes, Lesson 4 is about thinking in structure and reuse. In the next stages of the course, these custom tools will be the essential building blocks you use to automate large-scale geospatial analyses.