"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

Architecture Weekly #178 - 6th May 2024

Monday, May 6, 2024

This time, we discussed biases. Biases on the perspective on our technologies, so not seeing their evolutions. We also checked how biases can impact our knowledge, collaboration and eventually also the

WP Weekly 192 - WP Biz - Brands Merged, Woo Cart Popup, Fastest Hosting

Monday, May 6, 2024

Read on Website WP Weekly 192 / WP Biz The 'business of WordPress' is buzzing for sure, be it the acquisition of plugins or the massive Envato ownership change. Also, WordPress content brands

SRE Weekly Issue #423

Monday, May 6, 2024

View on sreweekly.com A message from our sponsor, FireHydrant: FireHydrant is now AI-powered for faster, smarter incidents! Power up your incidents with auto-generated real-time summaries,

⚠️ Avoiding AI Scams on Social Media — An Open Source Google Photos Alternative

Sunday, May 5, 2024

Also: Reviewing the Customizable Drop Mechanical Keyboard, and More! How-To Geek Logo May 5, 2024 📩 Get expert reviews, the hottest deals, how-to's, breaking news, and more delivered directly to

Daily Coding Problem: Problem #1432 [Medium]

Sunday, May 5, 2024

Daily Coding Problem Good morning! Here's your coding interview problem for today. This question was asked by Snapchat. Given the head to a singly linked list, where each node also has a “random”

PD#572 Good Ideas in Computer Science

Sunday, May 5, 2024

Ideas every programmer likes and why Garbage Collection and Object Oriented Programming don't count ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌

RD#454 API Layer & Fetch Functions

Sunday, May 5, 2024

ixing API and UI code quickly leads to messy and unmaintainable code ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌

The Shiny Toy Syndrome & Tiny macOS utility apps I love

Sunday, May 5, 2024

Lex launching its redesign, Raycast shares another monthly update packed with AI updates, prompts should be designed not engineered, and a lot more in this week's issue of Creativerly. Creativerly

Hyundai antes up $1B for AV startup Motional and Elon unplugs the Tesla Supercharger team

Sunday, May 5, 2024

Plus, layoffs come for Luminar, Fisker and Ola View this email online in your browser By Kirsten Korosec Sunday, May 5, 2024 Image Credits: Motional Welcome back to TechCrunch Mobility — your central

C#504 Adventures serializing absolutely everything in C#

Sunday, May 5, 2024

A fantastic journey porting Newtonsoft.Json to System.Text.Json ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌