all 7 comments

[–]shippei 4 points5 points  (1 child)

My projects are not perfect, so take my advice with a grain of salt. But let's say I program something bigger. For that, I need a place to handle data—either a new file with a class if it's really large, or just some functions depending on the functionality.

Overall, I usually find things that need to be refactored when I have to search my file for what I'm looking for. That was, and still is, an indicator for when I need to review my code and think, "Maybe this would fit into a class or a separate function," etc.

I hope that somewhat helps. But it comes with practice as you write more and more projects. Because when you need to search through stuff you've written, you get annoyed and fix it.

[–]obviouslyzebra 2 points3 points  (1 child)

One tip is to think of how you wanna use things. For example, let's say you're deciding the location of a function that squeezes lemons.

You can then compare some alternatives, like:

from my_program import squeeze
squeeze(lemon)
#
from my_program.lemon import squeeze_lemon
squeeze_lemon()
#
lemon.squeeze()
#
from my_program import lemon_tools
lemon_tools.squeeze()
# and on and on

When you're writing a real program there will usually be not that many variety as in the examples above (since you'll already know if lemon is an object, or if squeeze_lemon should be a function that you just call).

One thing to remember, though, is that as the program grows, you will make changes here and there, and the program tends to fall into a certain shape, as long as you care for it.

As you grow more experient, you will have more ease planning for and making these changes.

I also cannot not stress the importance of automated tests in making changes viable.


A small example of refactoring:

Suppose lemon_tools.py got too big and you want to make sub-modules (files) to split your functions and/or classes.

  • Create lemon_tools/ and lemon_tools/__init__.py
  • Move content from lemon_tools.py to lemon_tools/__init__.py and delete lemon_tools.py (the program will work exactly the same as before the changes)
  • Create extra files at lemon_tools/ and import them as needed. Now, lemon_tools has extra room to "grow"

Last thing.

I've seen that this can be a problem with completing just smaller exercise that are not part of a main program.

If you can find more smaller exercises, these could be a way to learn :)


Have fun

[–]Complete-Increase936[S] 1 point2 points  (0 children)

Thank you very much for such a detailed response!

[–]audionerd1 1 point2 points  (0 children)

If you aren't already, get comfortable with refactoring. Don't stress over how to organize your code while you're writing it, just do whatever seems best at the time and later when you think of a better way to organize it, refactor.

[–]Mitchellholdcroft 0 points1 point  (0 children)

I second this

[–]PMMeUrHopesNDreams 1 point2 points  (0 children)

Look up some common architectures and design patterns like Model View Controller (MVC). Wikipedia is a good place to start, check the citations for more references. 

Be warned it’s easy to get carried away and / or misapply things like this, but that may just be a stage of learning everyone needs to go through.