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

5

u/PercyJackson235 Nov 05 '20

Well, functions are, nominally, for any large enough chunk of code that you are going to reuse, either in the current program or in other program, without having to rewrite it. A class is a blueprint for creating an object that is a grouping of data and functions, that are in this instance called methods. Multiple files in a program is for logical separation. People will put data, functions, classes, etc.. that go logical together in the same file. GitHub is used for version control, collaboration, and, as a byproduct, backups. It allows you to keep a full history of a project, make and test changes without making a real impact on the project, and facilitates multiple people working on a project at once.

5

u/Malassi Nov 05 '20

functions are, nominally, for any large enough chunk of code

The idea behind what you said is good but I don't think it's the best way to separate your code by functions. What I mean is that if you start separating you function by large chunk is not the optimal way of separating you code in functions.

The better way, at my opinion (and uncle bob's) is to first make function small as possible. They also need to this rule. FUNCTIONS SHOULD DO ONE THING. I've seen a lot of programmer separating their code by function but doing huge logic in function and having a hard time debugging their functions. Well their error lies there. When writing function the best way to do it efficiently, is to make small function that do one thing (This doesn't mean they can't use other functions).

2

u/PercyJackson235 Nov 05 '20

I said "large enough". I have created three line functions because I use said functions in a multitude of places but I am also NOT about to make every three line for loop into a function, either. A large enough chunk of code is something that is up to personal discernment.

3

u/Malassi Nov 05 '20 edited Nov 05 '20

Hi

I just wanted to clarify because I felt like it could be misunderstood by a beginner programmer. That's why I'v said that the idea behind what you said is good it was just, at my opinion, not clear enough for a beginner programmer and felt like it needed a little bit more clarification about what is a large enough chunk of code.

2

u/PercyJackson235 Nov 05 '20

That's fine. You are right.