r/learnpython • u/TheGrapez • 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
15
u/Stabilo_0 Nov 05 '20
Hi. I'll try to share my view on the answer, but its not 100% best one.
Overal structure: Check out older languages for that: c and c++, pascal.
Python can help with that too, being scripting language your program is executed line by line. Try addressing the variable before declaring it and you get error.
Ive read and wrote some considerable amounts of code and while and cant point you to specific book that defines good code structuring, almost all codepieces i've seen follow the same pattern:
divide you code into blocks,
1st contains includes\imports
2nd constants
3rd variables
4th classes and functions
5th if necessary, calling and using all those from 1 to 4.
Proper way to use functions: you use functions when you have some action be done more than one time. Function is just that, a wrapper for one or more actions that needs to be done on its own (a procedure) or that should get, process and return something like a variable.
When you write something and find out you're repeating yourself, make a function out of it, call it and let it do the work for you.
What a class is: class is a blueprint for something that can be described with code (with varying levels of abstractions pretty much anything could be). Most of the time you write a class and use objects that python creates when you call the class, instances. Other approach is to look at class as a wrapper for functions and data. Function is there when you want some action to be made again and again. Class is there when you need to represent some object and change its state with functions. Look at some standard library entrances like Time or Random. Its a class. You dont have to make a Time object all the time, you call the functions in there, so classes can be used as dictionary of functions as well, or lets better call it a 'namespace'.
Why people have multiple files in a program: the larger your project gets, the harder it is to maintain. What if you need the same piece of code in two places at once? Would you copy the code or make a separate py file, put your code there and just import the file (block 1 from overall structure part of the answer). If you happen to find a mistake in your imported file, you will now have to fix it just once instead of all those files you used it it. Divide and conquer, sort of. Also imagine if you start working with a friend and both of you add code to the same program, how would you even do that if all your code is in one file?
Speaking of that...
What is github and how people use it: first, let me say i hate it/ Look at this http://www.evan.org/Fun/Git/index.htm years go by and its the same. Instead of making some steps towards being more user friendly devs at git decided they'd better change default branch name because code is about politics now. Github is a cloud storage for your projects. You can share these projects with other people and work on them in collaboration. You can split your code into branches for testing new features and then merge these branches into main branch. It uses git program with awful controls that you just have to endure but its still very useful and really the best thing there is if you are going to work with other people on anything remotely serious. Make a habit out of it, use it as an online backup storage for your project first, get the basics.
Is there a how to organize your stuff book: sort of, https://www.python.org/dev/peps/pep-0008/