r/learnpython • u/brushygiraffe • 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)
4
u/LeoJweda_ Mar 30 '21 edited Mar 30 '21
Perhaps I can shed some light on this as a professional developer who’s dabbled with making a Python Snake game tutorial and planning a hangman solver one.
When I’m just developing regularly, my personal style is to write it all in one place then extract functions but, in the back of my mind, I know which parts I’ll extract into functions. The reason I don’t do it from the beginning is that the added mental load of managing parameters is too much and breaks my concentration.
For the two above, I put extra care into code organization both because they’ll be open source that future employers will see but, also, because I’ll need to explain them and, if they’re too messy, they’ll be hard to understand. But, let me tell you, they did NOT start that clean.
One thing I’ll mention is that, if you want to use Test-Driven Development (TDD), you’ll have to plan our your code ahead of time and smaller functions are easier to test.
At the end of the day, do what works best for you. I wish I could do it right from the beginning because extracting functions later is slower. If you can do it in one sweep, go for it. There’s no right answer here, honestly.