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/chatziiola Mar 30 '21

This is a magnificent thread idea, at least from my point of view. Having spent a lot of time working on projects with different languages (for example, I hadn't studied python at all, yet I developed a handy script to upload my uni pdfs to Google drive in less than 1 hour), I am a strong believer than you should first take 5 minutes without writing anything on your computer but simply making a template. Of course, it could be just me that does this the old-fashioned way (with a pen and piece of paper) and you could, instead, utilize any fancy (or not) modern tool you can find, if that is what makes you feel most productive and effective. After that you should devote another 5-10 minutes to think of the implementation of each "step" you wrote down in the previous step. If you do so, you will not write so much code, but it will be on point right from the beginning and, something that I find beneficial when the time comes for documentation, you have in your hands the algorithm used in your application, meaning that you do not need to overthink what you just typed: Your code will be beautifully commented and you could just write in a concise matter your notes in your program's docs.

P.S. I think that the "timeframe" I gave can easily be misunderstood. Take the time needed to nail a specific goal and then simply move on.