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

2

u/ciskoh3 Mar 30 '21

So I recently got to second place in Hackathon also thanks to the fact that, despite most of us being data scientist and newbies, we had a clear project structure and could organise well.

And I'll tell you my secret: I got a premade folder structure (in my case from cookiecutter) and just fucking sticked to it!

For the first time I didn't invent weird helpers, structures or classes and I trusted what is generally accepted as a good structure: and guess what? it worked! Admittedly the app is far from finished but we had exactly 0 problems figuring out how the modules could interact and so on !

Every module has max a couple of wrapping functions that are taken up by main.py, no interaction between packages, clear names for folders and a good readme.

I cannot believe how many weeks I have spent reinventing the wheel and making a mess out of it.