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

1

u/temisola1 Mar 30 '21

There are different ways I approach it.

Sometimes I write pseudo code... I know exactly what I wanna do and how I wanna do it, so I’ll put the functions down along with whatever parameters I might need then fill in the details later.

Other time I just start writing, and I refactor as I go. I start in the main def, and when I come across a snippet of code that I would most likely need to reuse the functionality I’ll turn it into a function. And also if I come across something that behaves like a physical property I’ll turn it into a class. Sometimes I decide I might need this object/functionality in other files, so I’ll convert it to a module.