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)
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.
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.
Once I have a rough outline, I'll translate that to what I expect my functions to be with type hints:
E.g.,:
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.