2020-10-20

Interface First

  • One switch has improved my programming more than anything else: [[Interface First Design]].
  • For example, say you want to scrape the web for stock prices. Before you install beautiful soup and open up the inspect window in google chrome, spend 15 minutes thinking about what you want the final interface to look like
# 1) maybe all you need is a function
my_result = my_useful_function()
# done! go code

# 2) maybe you want a class
my_scaper = MyScraper()

my_scraper.collect_interesting_data()
print(my_scraper.interesting_data_i_collected_)

## Then maybe you want a generator for some something
for item in my_scraper.generate_items():
    do_useful_thing(item)

## It's all up to you 
  • Luckily for you, it doesn't matter the the code above doesn't run. It's not for the computer, it's for you. After this step, you should look at your skeleton and think "wow it would be cool if that code existed" - then make it exist!
  • This allows your creativity to flourish. Forget that the code doesn't run, that's the point. In this stage, you aren't bogged down by reality forcing you to write code the computer likes. This is the stage where you get to write code you like.
  • I wish I had picked this up much sooner in my programming journey. A clear goal makes gives you a much better idea of how to interact with libraries and new code you write. The worst case scenario is that you spend an extra 15 minutes designing this before you find out what you want to do is not possible. Even if that happens, you've spent time improving your design skill itself, and have have a clear artifact to show you what exactly you were trying to do.
  • The alternative is to dive right into the first thing you can think of. Instantiate a beautiful soup object and figure out which html tags you need to grab. This is how I used to work on all projects. I'd think "why waste time when I don't even know if what I want to do is possible." If I could go back, I'd tell myself
    1. It doesn't take much time (especially relative to the whole project)
    2. It's an important skill to improve
    3. Allows you to be more creative
    4. It's a rewarding/enjoyable part of the process in and of itself (often more so than writing code for the computer)
  • Of course you can dive right in, and for beginners, that is often the best way. But as soon as you start building some intuition for how you want your code to look, or if you start to take inspiration from the great interfaces of other python projects, this is a great way to improve not only your code, but also your development experience.


My Linked Notes

One last thing

If you liked these notes, hit me on Twitter!