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)
2
u/baubleglue Mar 30 '21
You aren't finding videos about planning code because it is not a Python question. If you search "how to design program" instead of "how to code Python" you will get results (but still probably not the results you are looking for). It is a huge topic. It will be impossible to cover it in single answer.
You can start from https://en.wikipedia.org/wiki/Top-down_and_bottom-up_design - that is related to your question.
> Should I write 90 lines of code then reorganize it into functions?
Not a good idea. 90 lines is a lot of code - you already put some logic to make it working.
> Should I write down the steps as comments and then fill in the code after?
that is better approach, I do it sometimes when I have many steps
> Should I write down the steps as comments and then fill in the code after?
That is bottom-up design - very valid method, but the way you described it is not correct: "getjson() Function will accept json string and return TSV". Look Java's interface concept and how it is done
Python has type annotation now - you can use it
but most important, name inputs and outputs
Now about the names - common beginners' mistake. "getjson" is a bad name - really bad. My English is not good, but as I understand "get json" should return json and get it from somewhere - "convert_json_to_tsv" is a better name but still probably is not good. Why it is not good? Can it convert any JSON to TSV? You probably writing code for some specific reason "convert_[user_statistics_api_data]_json_to_tsv" - something like that:
There is a concept of domain specific language, if you think of coding as process building a new vocabulary, that may simplify your task a lot. There is a lot of very important things to consider like I will just put links:
but using domain specific language (DSL) is a good shortcut to start with.
It is a bit different when you design your code top-down, but it mostly applicable for bigger code base (I think). I personally combine methods:
When stuck and not sure where to start - write few lines of working code, make them a function. Repeat for some other tasks.
When it starts to be clear what is end-to-end flow - make POC (working code).
Then make top-down thinking: define components (ex. parser, api, configuration, main_app) .
Then - rewrite code. Ex.
split methods to relevant modules/classes
parametrize all hard-coded values, add them to configuration
move ALL global variables to configuration
make main_app to operate only DSL