r/learnpython • • Mar 30 '21

How are you ‘planning’ your code?

There’s a thousand videos on Youtube on how to code Python - but they seem to be more like function references - which you can look up in the documentation?

How you guys and girls think through what you’re about to write?

Should I write 90 lines of code then reorganise it into functions? Or start writing functions and then finally ‘use’ the functions at the bottom of your code/script?

Is there a best practice way of doing this? I’m trying to make it easier for myself so everything works like ‘lego’ - otherwise I find I get really confused

Should I write down the steps as comments and then fill in the code after?

‘’’ getjson() Function will accept json string and return TSV def getjson():

‘’’ getinputs() function will read argv[1:] and return the first parameter, or exit with error message if none specified def getinputs():

‘’’ writetosql() function will accept TSV and write it to final.db def writetosql():

‘’’ Run the functions inputs=getinputs() tsv=getjson(inputs) writetosql(tsv)

349 Upvotes

107 comments sorted by

View all comments

184

u/IlliterateJedi Mar 30 '21 edited Mar 30 '21

My step one is to open Notepad and write out what I need to do.

Process these 10,000 images to new image and file sizes with new file names

I try to set the specific outcome.

I then write out a rough guide of what steps I need to take and in what order to have a semi-'optimal' approach.

- Read in images
  • Check dimensions
  • Resize (if needed)
  • Save new version

Once I have a rough outline, I'll translate that to what I expect my functions to be with type hints:

E.g.,:

def get_images(fp: pathlib.Path) -> Generator[Image]:
    pass

def is_dim_exceeding_max(input: tuple[int, int])-> bool:
    pass

def calculate_new_dims(dims: tuple[int, int]) -> tuple[int, int]:
    pass

def resize_image(img: Image, dims: tuple[int, int]) -> Image:
    pass

etc.

Depending on what I'm doing I may write out the code at this point in PyCharm or I may work through the steps in a Jupyter Notebook.

I also tend to write my 'tests' as I go under 'if name is main' then move those over to a test directory once things work. That way you can just run your tests as you modify things down the line.

37

u/Banishlight Mar 30 '21

This is a very good way to work through code. Coming from experience I recommend doing it this way. Start with your higher level concept and work your way down

16

u/ingwe13 Mar 30 '21

I’m still very much a novice, but this is the way I work. I think of it as taking passes at the program and each time you take a pass, your implementation gets more detail. The size of the project will affect how many passes are needed. Sometimes you have to back up if you can’t do something you believe you can (like a module doesn’t work the way you thought it did)

5

u/jerodg Mar 30 '21

Things like this seemed like a huge waste of time when I was a new code monkey. But looking back this is the same process I have developed in my mind and that I follow every time I write. The difference is now I don't have to write anything down because I have memorized my process.

I do on occasion make flow charts for complicated applications especially when orchestrating several different platforms at once. It helps keep track of the data flow. Also, management at any level can easily understand a flowchart but probably have no idea what you're saying when you explain it verbally because their brain screeches to a halt once you use terminology they aren't familiar with. It can be a big help when you're new or new to a team to help others understand that you actually do know what you're talking about/doing. But don't always do it unless you know it always gets you points with the boss.

So yeah, develop your process using pseudocode or paragraphs of text with explanations of what you want to set out to do. Refine it over time and when you've got something that works for you, you'll eventually commit it to memory. And your brain will automagically layout and organize a complex program for you before you even start.

Organizing into functions... We use functions/methods when that bit of code is or could be reusable. Creating a bunch of functions for a simple script might be more than needed. If your code gets more complex separating into functions can be helpful just to move a section of complex code out that seems to interfere with the logic.

e.g. I tend to keep my main method/class the holder of logic. It has just the steps that organize the flow of the program. If any parts of the flow become too large and start to muddy up the flow I pull that section out into a method. This allows any other person to look at your code and see right away what your code is doing without getting bogged down in how it's doing it. This works great when jumping into someone else's complex program as it allows you to find the section you need to make changes to more quickly.

2

u/CabalCapital Mar 30 '21

As another brand new python learner, this was great jnfo. Thanks!

1

u/coconutxyz Mar 30 '21

I just watched the video thinking like a programmer and this should be the best way

1

u/PanTheRiceMan Mar 30 '21

Doing the same. Breaking down your task into simpler, more explicit formulations definitely helped me making projects manageable.

1

u/gootecks Mar 30 '21

In what situations would you use PyCharm vs. Jupyter?

3

u/IlliterateJedi Mar 30 '21

Technically I do all of it in pycharn, but I run a jupyter instance through the project I'm working on when I'm working. Usually I use jupyter when I'm dealing with Pandas and need to see data displayed readily as I work. If I'm doing actual algorithm writing I'll use pycharn so I can more easily step through the code as I go.

1

u/gootecks Mar 30 '21

Oh ok good to know, thank you!

1

u/MisterMoo22 Mar 31 '21

I am just starting to learn this so thank you for walking through your normal steps.