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/[deleted] Mar 30 '21 edited Mar 30 '21

use pseudocode (google it) e.g.

REPEAT
	OUTPUT 'What is the best subject you take?'
	INPUT user inputs the best subject they take
	STORE the user's input in the answer variable
	IF answer = 'Computer Science' THEN
		OUTPUT 'Of course it is!'
	ELSE
		OUTPUT 'Try again!'
UNTIL answer = 'Computer Science'

Basically, this is a simplified version of your intent representing a logical workflow - notice it can be converted into any programming language, Java, C, Python

You plan your approach before writing any code.

A similar concept is called "requirements engineering" which a whole discipline in itself... finding out what the actual requirements are BEFORE coding anything - even clients sometimes don't know what they require e.g. attributed to Henry Ford - "if I asked what people wanted, they'd have said faster horses"

2

u/baubleglue Mar 30 '21

That is procedural approach, if you have pure flow - it helps a lot, but you can't build a project that way. Just introduce to your example few more things to consider for answer as user.topic.competency_level, user.age, user.occupation it may branch to so many directions.