How do you plan or outline a program?
I’m currently studying Python Object Oriented programming and got to a point where logic and syntax are the least of my problems. I always get to a stage where I’m completely lost between modules, classes, objects and a sea of “selfs”.
I’m not doing anything too complicated, just small projects for practice, but I think I would benefit from planning. My mental processes are highly disorganized (ADHD) and I need all the help I can get with that.
I don’t need an automated tool (even though it might come in handy) -- sketching things out on paper is probably enough.
I only know about UML, which seems fine. Can anyone recommend a tutorial about this and other tools?
Edit to link my last attempt at following a tutorial:
This is the last tutorial I tried to follow, a Pygame project from the book Python Crash Course 2ed
. Following tutorials is frequently mostly typing, so what I achieved there is not a real representation of my abilities -- I would not be able to do something like that on my own. In fact, I failed to answer the latest exercises, which were basically smaller versions of this project.
My problem is not with syntax and the basics of how OOP works, but rather with memory and organization of information.
That was, without exaggeration, the best explanation on the usefulness of OOP I have ever read. Thank you very much for this.
I already knew the basics of how
self
Python OOP works in general, but the books and tutorials I've read before jump straight from functions to classes without providing consistent reasons for their use. "cohesion" and "method cohesion" are new concepts for me, but I can already see their usefulness.This book looks awesome. Does it contain information similar to what you shared?
And why did you use a
dataclass
in one example and a regular class in the other? Is there a clear disadvantage in using them? I prefer a cleaner syntax myself. Won'tdataclass
works pretty much like a regular class, sharing itsself
with other methods?Yes, a dataclass is a regular python class and python doesn't treat them any differently.
I think there are 2 main reasons to use them:
Nit: at the beginning of this explanation, in the first bit of code, there are technically no methods yet, only functions.
I would be careful not to get too deep into object orientation and other ways of organizing code too early.
My approach is to delay organization until I'm sure I need it, refactoring as I go. I stick with a single file until it gets unwieldy. Maybe start with "hello world" (whatever that means for your app) and start coding in the main method. Extract a function after you've written the code inline and you have a block of code that works and would make sense on its own. Once you have a few functions that operate on the same data structure, then it's time to consider a class. Once you have a class, consider what functionality you've already written that belongs there.
At least, that's what I do when in doubt. Sometimes you know ahead of time the structure you want.
Edit: It's kind of hard to talk about in the abstract, though. If you're willing to post some code publicly then you could get more specific advice.
I don't intend to. I'm just focusing on it for learning purposes. I don't even like OOP, to be honest.
This is the last tutorial I tried to follow, a Pygame project from the book
Python Crash Course 2ed
. Following tutorials is frequently mostly typing, so what I achieved there is not a real representation of my abilities -- I would not be able to do something like that on my own. In fact, I failed to answer the latest exercises, which were basically smaller versions of this project.I must notice that I don't have a real problem with syntax and logic, but rather with memory and organization -- I literally cannot keep more than two or three things in my active memory at the same and will eventually forget what's going on. Thins can quickly become overwhelming. Curiously, I don't have this issue with Emacs lisp. Lisp just feels reasonable for me. Sadly, it won't help to get a job, and that's something I need right now.
This is great advice, thank you very much!
Since you are just starting out you might just want to start programming and see how well it turns out. If it really is not important, you can write absolute spaghetti code so you can get an idea where you personally need to organize.
Personally I start off by writing a skeleton of all the classes and methods I need. Then I try to write the documentation first. Then I realize as I am writing that I made logical mistakes as I was planning and rewrite everything. 😸
I appreciate the advice and it's certainly good for most cases, but this approach makes me extremely anxious and triggers my ADHD big time. It's hard for me to do anything without a neat environment, I can't read or study in a messy room, for example. Minimal irregularities become incredibly distracting.
So I tend to (over?)compensate by being rigorous and organized in my affairs.
Thank you very much for answering!
In general you just get better the more you work on a variety of programs. A general strategy is to get down a minimum viable product -- the base basic possible implementation of what you're trying to do. From there, it's all incremental, which is nice.
Additionally, try to think about the parts of the program which radiate outwards design wise. Typically UIs, for instance, I do last, since the core functionality will dictate what the UI has, so if you make it first, you'll probably have to change it by the end.
In terms of actual tools, I've been using Whimsical at work. It's just a nice diagram builder. Good for collaboration, though I suppose you won't use that.
That's quite reasonable. Thank you very much.
Tools are awesome, but one must know what to do with it! Do you use it freeform, or are there any documented methods that can make tools like this one especially productive?
Do you ever use libraries or modules that other people wrote? I've found that using a well-written library teaches you how to organize your own code even if you don't intentionally set out with that goal in mind. There are certain concepts that are well known that you start to see in other people's code and you can start to use in your own.
One of the most important concepts that helps with organizing code is called separation of concerns. It's also sometimes referred to as "separating your business logic from your display logic." The idea is that the code that displays your data is completely separate from the code that manipulates your data. This is probably the single most helpful concept I've learned when it comes to writing better code. Concepts that go along with this include loose coupling and information or data hiding. These help with making your program easier to modify and less fragile.
That’s great advice, thanks!
I'm not a skilled programmer (com-sci dropout for Information Systems, lol), and I'm pretty bad at commenting, but I don't have issues tracking my admittedly simple programs. However, the way I design a program is to think about what I need, and choose object, function and variable names that are descriptive, either in their capacity as a variable, which I assume you're already doing. Better commenting a habit I should start doing.
Basically what I've learned is do something like this:
If your names are meaningfu, it goes a long way, the rest is just keeping things clean and sort of having a flow to your code.
Thanks, I'll keep that in mind!
One thing to keep in mind about object-oriented programming is that you have a choice of ways to organize your functions and these choices are sometimes arbitrary. For example, let's say you have two classes, Foo and Bar, and a function that takes two arguments:
There are three places that you could put this function:
Which one you choose is often a matter of taste: where do you expect to find this function? It's often useful to look at a method's arguments and consider whether you can move the method to some other class, or when looking at a standalone function, you can decide whether it should really be a method on one of its parameters.
But, you don't want to get too caught up in tidying up. This is internal organization for the benefit of you and your team and the users aren't going to care. Moving things around can be helpful, but someone who expects to find them in their old location might be confused.