(functions, reuse, and DRY code)

Why All the Logging Examples

You may have noticed that almost every lesson uses a logging example.
That is not because I want you to become a logging engineer.
It is because logging is the simplest stand-in for real work.

If you can write clean, readable code that logs what it is doing, you can write clean, readable code that does anything.
Logs make invisible logic visible. They are safe to test, easy to read, and perfect for learning how to structure thought.

context.log.info("Crew counted", extra={"ship": ship_name, "crew_size": crew_size})

That single line covers almost every concept in this course:

  • clear naming
  • clean data flow
  • structured context
  • traceability

If you can do it with a log, you can do it without a log.
But doing it with a log is always better than without.


A parrot repeats what it hears.
A real engineer teaches the parrot to take parameters.

If you have ever written the same few lines of code twice, you have met the Copy-Paste Parrot.
He sits on your shoulder and squawks “just duplicate it” while quietly doubling your technical debt.


Example: The Parrot at Work

context.log.info("Crew counted", extra={"ship": "Thousand Sunny", "crew_size": 9})
context.log.info("Crew counted", extra={"ship": "Going Merry", "crew_size": 7})
context.log.info("Crew counted", extra={"ship": "Oro Jackson", "crew_size": 8})

Three identical lines, different values.
It works, until someone asks you to change the message.
Now you have three places to fix and one parrot laughing at you.


Teach the Parrot to Take Parameters

def count_crew(ship_name, crew_size):
    context.log.info("Crew counted", extra={"ship": ship_name, "crew_size": crew_size})

count_crew("Thousand Sunny", 9)
count_crew("Going Merry", 7)
count_crew("Oro Jackson", 8)

Now the logic lives in one spot.
Change it once, and every call learns the new tune.


Pirate Rule #2: Do not Copy What You Can Teach

  1. If you see the same pattern twice, make a function.
  2. If the only difference is a name or a number, make it a parameter.
  3. If you catch yourself saying “I will just paste this,” walk the plank and refactor first.

Why It Matters

Copy-paste feels fast today but costs hours later.
One bug copied three times becomes three bugs.
A good function is like a well-trained crew. It knows its job and repeats it perfectly every voyage.


Exercise: Train Your Parrot

Find a snippet you have copy-pasted this week.
Wrap it in a function, give it a clear name, and make the differences parameters.
Run it once, smile, and toss your parrot a cracker.


© 2025 Monkey D. Data · Powered by Hugo & PaperMod


A function is reusable logic.
If you find yourself repeating code, extract it into a function and give it parameters so you can reuse it cleanly.

Repetition creates maintenance debt. Every time you copy-paste, you multiply your future workload.


Example: Repetition

context.log.info("User processed", extra={"name": "Alice", "status": "active"})
context.log.info("User processed", extra={"name": "Bob", "status": "inactive"})
context.log.info("User processed", extra={"name": "Charlie", "status": "active"})

Simple, but fragile.
If the message format changes, you will need to fix it in three places.


Refactor with a Function

def log_user_status(name, status):
    context.log.info("User processed", extra={"name": name, "status": status})

log_user_status("Alice", "active")
log_user_status("Bob", "inactive")
log_user_status("Charlie", "active")

Now there is one definition and three clean calls.
Change the message once, and all instances update automatically.


Engineering Rule: Do not Repeat Yourself

  1. Reuse logic through functions or methods.
  2. Expose differences through parameters, not copies.
  3. Keep one source of truth for any behavior.

Why It Matters

Copy-pasted code increases risk and reduces clarity.
Functions create consistency, improve readability, and make testing easier.
If you change logic in one place, every dependent call benefits immediately.


Exercise

Find a repeated block of code in your last project.
Turn it into a function.
Test it once.
Enjoy the silence when your linter stops yelling about duplicates.


© 2025 Monkey D. Data · Powered by Hugo & PaperMod