"If the story sucks, the execution sucks"

Hey there,

So how should you structure your Python programs?

If you're writing anything that goes beyond a few lines of code, how should you lay out your functions?

Is there a "structure to the chaos?"

Interesting thought on that topic from a newsletter member:

~~~

I have always felt that programming is telling a computer a story. 

If your story is good, the computer will execute it efficiently, if the story sucks, the execution sucks. 

I write code like I would write a technical paper, it has direction and flow, and you can tell when you have reached the conclusion of the story.

~~~

Cute! And true:

I've had good results with this approach. It works especially well for single-file automation or data crunching scripts. 

Imagine the following high-level logical flow for a simple report generator program:

  1. Read input data
  2. Perform calculations
  3. Write report

Notice how each stage (after the first one) depends on some byproduct or output of its predecessor:

  1. Read input data
  2. Perform calculations (based on input data)
  3. Write report (based on calculated report data)

You can either implement this logical flow from *the top down* or from *the bottom up*.

If write your program bottom-up, your function layout will match the logic flow—it'll go from the fully independent building blocks to the ones that depend on their results.

Here's a sketch for a "bottom-up" implementation:

def read_input_file(filename):
    pass 

def generate_report(data):
    pass

def write_report(report):
    pass 

data = read_input_file('data.csv')
report = generate_report(data)
write_report(report)

This structure "makes sense," doesn't it?

We first need to read the input file before we can generate a report, and we need to generate the report before we can write it out to disk.

This logical structure is reflected in the program's layout.

(Or, in scary-sounding Computer Science terms: This is basically a topological sorting of the dependency graph.)

For a "top-down" approach you'd flip the same structure on its head and start with the highest-level building block first, fleshing out the details later:

def main():
    data = read_input_file('data.csv')
    report = generate_report(data)
    write_report(report)

def write_report(report):
    pass

def generate_report(data):
    pass

def read_input_file(filename):
    pass 

# Application entry point -> call main()
main()


See how I started with the high-level, "most dependent" functionality first this time around? 

The "main()" function at the top states clearly what this program is going to do—without having defined yet *how exactly* it will achieve the desired result.

Now, which approach is better, "top-down" or "bottom-up?"

I don't think there's much of a practical difference between them, to be honest. 

The important thing for me is that they both encode a logical narrative—they're both "telling the computer a story" and they have "direction and flow."

This is the key insight for me.

The worst thing one can do is deliberately obfuscating this logical structure, thereby killing the narrative:

def write_report(report):
    pass 

def read_input_file(filename):
    pass 

def generate_report(data):
    pass


(Yuck!)

Now, obviously I'm using a small "toy" example here—

But imagine what happens with programs that consist of 10, 100, 1000 steps in their "narrative" and they're organized incoherently?

Maybe it's my German need for order and stability speaking—but in my experience the result is usually utter chaos and madness.

Anyway, the more you practice this "narrative flow" mindset as a way of structuring your programs, the more natural it will feel and the more automatic it will become as a behavior while you're coding.

Plus, you can extend the idea to other "building blocks" like classes and modules as well...but more on that at some other time.

Happy Pythoning!

— Dan Bader

Older messages

Your "Python support group"

Friday, December 11, 2020

Hey there, I've been in your shoes. I know how hard it is to grow your programming skills without a good support group. That's why I founded PythonistaCafe, a unique peer-to-peer learning

here's what's wrong with the internet today

Tuesday, December 8, 2020

Hey there, Have you ever interacted with an online community and got a horrible reaction that made you feel like crap? You're not alone. In a nutshell, here's what's wrong with public

Can I ask a favor?

Sunday, December 6, 2020

Hey there, it's Dan... I noticed that you decided to pass on The Complete Guide to Setting up Sublime Text for Python Developers—that's totally cool. I'm wondering if you could help me out

bad news

Friday, December 4, 2020

Hey there, This is it— The 20% off deal on Sublime Python is ending NOW. And the bad news is I don't know when or if I'll offer this deal again. If you're still wondering if the course is

Tick-tock

Friday, December 4, 2020

Hey there, Just a quick heads up that the 20% off discount for "Sublime Python: The Complete Guide to Sublime Text for Python Developers" expires in just a few hours. -> Click here to get

You Might Also Like

Import AI 399: 1,000 samples to make a reasoning model; DeepSeek proliferation; Apple's self-driving car simulator

Friday, February 14, 2025

What came before the golem? ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏

Defining Your Paranoia Level: Navigating Change Without the Overkill

Friday, February 14, 2025

We've all been there: trying to learn something new, only to find our old habits holding us back. We discussed today how our gut feelings about solving problems can sometimes be our own worst enemy

5 ways AI can help with taxes 🪄

Friday, February 14, 2025

Remotely control an iPhone; 💸 50+ early Presidents' Day deals -- ZDNET ZDNET Tech Today - US February 10, 2025 5 ways AI can help you with your taxes (and what not to use it for) 5 ways AI can help

Recurring Automations + Secret Updates

Friday, February 14, 2025

Smarter automations, better templates, and hidden updates to explore 👀 ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏

The First Provable AI-Proof Game: Introducing Butterfly Wings 4

Friday, February 14, 2025

Top Tech Content sent at Noon! Boost Your Article on HackerNoon for $159.99! Read this email in your browser How are you, @newsletterest1? undefined The Market Today #01 Instagram (Meta) 714.52 -0.32%

GCP Newsletter #437

Friday, February 14, 2025

Welcome to issue #437 February 10th, 2025 News BigQuery Cloud Marketplace Official Blog Partners BigQuery datasets now available on Google Cloud Marketplace - Google Cloud Marketplace now offers

Charted | The 1%'s Share of U.S. Wealth Over Time (1989-2024) 💰

Friday, February 14, 2025

Discover how the share of US wealth held by the top 1% has evolved from 1989 to 2024 in this infographic. View Online | Subscribe | Download Our App Download our app to see thousands of new charts from

The Great Social Media Diaspora & Tapestry is here

Friday, February 14, 2025

Apple introduces new app called 'Apple Invites', The Iconfactory launches Tapestry, beyond the traditional portfolio, and more in this week's issue of Creativerly. Creativerly The Great

Daily Coding Problem: Problem #1689 [Medium]

Friday, February 14, 2025

Daily Coding Problem Good morning! Here's your coding interview problem for today. This problem was asked by Google. Given a linked list, sort it in O(n log n) time and constant space. For example,

📧 Stop Conflating CQRS and MediatR

Friday, February 14, 2025

​ Stop Conflating CQRS and MediatR Read on: m​y website / Read time: 4 minutes The .NET Weekly is brought to you by: Step right up to the Generative AI Use Cases Repository! See how MongoDB powers your