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

1

u/MightbeWillSmith Mar 30 '21

I usually start with a list of #TODOs. For a new project it is a couple bullet points.

TODO get data from warehouse

TODO convert x,y,z to time series

TODO get correlations between a,b,c

Or something like that. Then within each of those, create subheadings of the specific step.

TODO get data from warehouse

 #TODO get x,y,z from table j, dates        01/2020 - 04/2020 

When I have it mostly mapped out. I get to work. If I notice there's something I'm repeating several times, I'll write a function to clean it up. Although that might be in later stages after I have something that works as is.