"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

Dispatch 019 🦃

Thursday, November 28, 2024

Watching the Parade • Paying for the Parade • Pirate TV Raid • US v. Microsoft Round 2 • Trump's Anti-Antitrust? • Google Anti-Antitrust The Spyglass Dispatch is a free newsletter sent out daily on

PHPWeekly November 28th 2024

Thursday, November 28, 2024

Curated news all about PHP. Here's the latest edition Is this email not displaying correctly? View it in your browser. PHP Weekly 28th November 2024 Hi everyone, It's Thanksgiving and Black

Widgets With Glance: Beyond String States

Thursday, November 28, 2024

View in browser 🔖 Articles Widgets With Glance: Beyond String States I had read about using a CustomGlanceStateDefinition but I couldn't find much about it in the official documentation so here is

Post from Syncfusion Blogs on 11/28/2024

Thursday, November 28, 2024

New blogs from Syncfusion All Things Open 2024 Takeaways, Part 3: Freedom vs. Sustainability By Marissa Keller Outten The role of sustainability and freedom in open source was discussed at the All

Top Tech 🏆 HTC VIVE Focus Vision Review — These Clip-on Earbuds Are Simple and Sound Good

Thursday, November 28, 2024

Also: Plugable Portable Monitor Review, and More! How-To Geek Logo November 28, 2024 😎 Question Reality If you're wondering what to buy on Black Friday or during the holiday rush, this week we

Hackers Exploit Popular Game Engine to Distribute Cross-Platform Malware

Thursday, November 28, 2024

THN Daily Updates Newsletter cover Programming with GitHub Copilot: Write Better Code--Faster! ($48.00 Value) FREE for a Limited Time Accelerate your programming with the most popular AI coding tool on

Edge 452: The AI Magic Behind Google's NotebookLM Audio Features

Thursday, November 28, 2024

How does NotebookLM generate such cool podcasts? ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏

Check Out These Awesome Gifts!

Thursday, November 28, 2024

Let us help you check one thing off your to-do list with this guide to the best gifts for the holiday season. Make the holidays a little brighter with these great gift options. From health and wellness

🚀 Accelerate Your Growth As a Software Architect

Thursday, November 28, 2024

What students are saying about my courses More than 4300+ students already completed my courses. And they gave them a 4.9/5 ⭐ rating. I'd love to see your success story next on this wall of

🫵 Android Developer Previews Are Not For You — Virtual Reality Might Finally Be Socially Acceptable

Wednesday, November 27, 2024

Also: iPhone Camera vs. Digital SLR, and More! How-To Geek Logo November 27, 2024 Did You Know The band Radiohead was originally called "On a Friday"—the band was formed by high school