r/learnpython • • Nov 05 '20

How does one learn to make well-structured programs? Mine are dumpster-fires.

So I just (kind-of) finished reading Automate The Boring Stuff With Python, and for the past month I've felt like a big-brained super-hero automating myself out of a job (Data Analyst).

The problem is, I feel I missing something along the way. I don't understand the proper way to use functions, what a class is, why people have multiple files in a program, how/why people use GitHub...probably some other things I haven't discovered yet.

They all seem to be related to organization? Is there a "How To Structure Your Python Stuff With Python" book?

478 Upvotes

98 comments sorted by

View all comments

370

u/pbw Nov 05 '20

There is an art and science to programming that must be learned that’s totally independent of any specific language. It takes time and practice and study.

You need to develop an engineering aesthetic so when you look at code you immediately see how it could be improved. Then you need the skills to push it into that shape without breaking tests or functionality in general.

There are books you can read but it’s somewhat like reading a book on how to play basketball. It can give you ideas but you have to practice it yourself, figure out your own ways of looking at things and doing things.

In writing (English) they say everyone has 100,000 words of poor writing in them. So the best you can do is speed through that to get the good stuff.

78

u/TheGrapez Nov 05 '20

This is oddly inspirational thank you!

39

u/postb Nov 05 '20

Adding to this excellent answer. I really started to improve on code structure and usability once I started writing code and programs within a team. Having to adhere to standards and norms to enable co working.

27

u/Solonotix Nov 05 '20

Some tips: consider cyclomatic complexity. This is a fancy way of saying "How hard is it to understand what can happen?" There are formulas that can calculate roughly the cognitive burden of understanding code, but the simple take away is this: each if counts as 1, and every nested level adds a multiplier of 2n where N is the number of nested levels.

So code that reads if(if()else if(if()) else... would be 11 (if my math is correct). Depending on what you're writing, set a limit on this value (probably 6 to start). If you go over this, you should probably break the logic into parts, and make those parts functions, or class methods, depending on the type of work you're doing. This won't simplify the action, as these steps are still being performed, but you can at least better understand what each step does in a vacuum.

Another good practice is to start with your intended result, in the form you want it, and then work backwards until you connect that logical goal to your logical start. Working with a friend who is learning some rudimentary programming in school, she frequently struggles with visualizing the final data structure she wants, so I figured I would call that out. Doing this frequently reduces the number of intermediate steps you need to do.

6

u/IDontLikeBeingRight Nov 05 '20

You'll also improve once you have a job, because you'll be collaborating. You'll share skills & approaches & techniques with your workmates. You'll start to see why some of the "superficial" guidance really does make it demonstrably easier to work in a team. You'll hear disaster stories of what happened when someone tried a quick hack they knew was bad form, and you'll start to collect your own such stories too.

All of this is part of "experience", the nurturing of judgement to not just be able to write code, but to know which code to write.

1

u/[deleted] Nov 06 '20

Make your code public and get people to look at it. Someone will inevitably tell you what you could have done better :)

14

u/[deleted] Nov 05 '20

This is brilliantly put.

I'm no programmer, but I have an ambitious project I want to create and it's inspired me to try to learn C# (primarily due to the ease of Windows GUI design...). I'm a consummate planner, but even with this project my code is monolithic and, in a word, gross. I figure a weekend of organization could actually save me weeks, if not months, of recoding, as good organization prevents one from stepping on their own dick on the regular.

6

u/Solonotix Nov 05 '20

When working in C#, it's usually a good practice to create a class for every complex value you work with, each class should be its own file, and frequently it should have an accompanying interface in a separate namespace (sometimes it will be it's own project). What this does is separate definition (Interface) from implementation (Class), so that you can readily import the code that lets you interact, without needing the code that dictates how it all works.

From there, avoid the urge to put everything in your Program class, or Main method, and make each distinct data type a Class, and each distinct action should be a method. Any method signature that takes more than about 20 lines to spell out probably could be broken up into more manageable chunks (Note: 20 lines is not a hard rule, and instead just a soft indicator that you might be getting long-winded).

5

u/[deleted] Nov 06 '20

Pretty much the way I've decided to restructure things...Put keyboard handlers in their own class, mouse handlers in their own class, etc., etc., just to keep things clean (and portable, for future projects!).

Now if only I had time to dedicate to the project, I'd be on my way :D

1

u/cyberst0rm Nov 06 '20

consistency and reasonable expectations.

1

u/Nixellion Nov 06 '20

I would say its the same with almost everything. There are aspects of any kind of occupation thay you just cant teach/learn (effectively), it comes with experience.

3

u/pbw Nov 06 '20 edited Nov 06 '20

For sure. One of my favorite examples are the charismatic “sales gurus” who hold seminars teaching their sales techniques. Hundreds of earnest people cram into a hotel conference room to hear them speak.

But again it’s like listening to Tom Brady explain how to be a great quarterback. It might not be useless, but it’s certainly not going to bring you up to his level. Maybe it will get you 1% of the way.

I think a legit benefit of this conferences or reading software books by engineers you admire is motivating you. And giving you ideas. But then you have to take those ideas and motivation and work out your own ways of doing things with practice.