r/eli5_programming • u/Neemulus • Mar 08 '21
Why do Python courses never explain how to architect your code?
I can do the syntax, I even understand OOP but I’ve never seen anyone explain how best to structure your code. I get MVC models etc. but as a relative beginner who can write working code, how do I figure out how best to structure modules and the scope of modules efficiently and in a reusable way. I’m having to make it up as I go along. 🥺
14
Upvotes
8
u/yogert909 Mar 08 '21
Google "Clean Code Python". There are a lot of principles people have come up with over the years. At a certain point it is a matter of opinion, but understanding the reasoning for various principles is helpful to understanding the trade-offs.
2
u/CriusNyx Mar 08 '21
The skills involved in code architecture generally don't apply to just a single language so it wouldn't make sense to teach the topic in a course for a single language. Additionally most code architecture courses have a great deal of prerequisite material beyond just reading and writing python code. For example, my university only offers the course at a graduate level.
However, if you are having difficulty structuring your code I would consider taking a data structures class and an algorithms class. A book on common programming patterns may also be helpful.
2
u/no1joel Mar 09 '21
I think this is a fantastic and very under-represented topic in learning programming.
I think Clean Code in Python is a great book for this. I got it in a humble bundle sale a while back, probably worth keeping an eye on their book sales :)
Also I find a lot of tutorials on TDD (Test-Driven Development) go through the Refactor step which can show you better ways to structure code for future changes, maintenance etc.
To give some immediate pointers, when we define classes we think of them as an object having functions directly related to that object and its purpose; start to think of modules as having functions directly related to that module, and packages having modules directly related to that package.
I like using
__init__.pyfiles as the interface for a package as it expands from a module into a package - so say you have one file and you're just keeping a bunch of vaguely related functions in there. As you develop more and more, you may notice similarities between some, and that some are only used internally within that file, e.g. by other functions that are imported in other files in your project. I'll take this as an opportunity to create a package named after the module, splitting out the functions into their own modules which describe the purpose, importing the "public" functions in__init__.py(and listing them in__all__, though I dislikeimport *) which will mean any previous imports will continue to work.These are just a couple of techniques and points for thinking that come to mind immediately, hope they help!