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

34

u/[deleted] Mar 30 '21

Weeks of coding can save you from hours of planning.

I never sit down to code unless I've got a pretty good idea as to what I'm going to do. Even if it's an experiment or tinkering. Having clear goals up front is critical. That means thinking through your project, writing notes, saying it out loud, and scribbling diagrams.

Then there are principles to follow. Functions should do one (and ONLY one) thing, and they should do it well. Functions are like digital tools. You have a hammer to drive nails and a saw to cut wood. You don't have one tool to do everything.

For more principles look up PEP8

-5

u/Ran4 Mar 30 '21 edited Mar 30 '21

Functions should do one (and ONLY one) thing, and they should do it well.

People keep repeating this, but it's so obviously false.

Any useful program must do more than one single operation, thus there must be multiple functions and there must be at least one function that call those functions, and that function thus does more than one thing.

It's much better to understand that a function should contain related code, and if there are parts of a function's body that's not at all related to the rest of the function body, then that function should probably be split up (a good function length is typically around 1-25 lines of code).

9

u/[deleted] Mar 30 '21

"Each function needs a clear purpose" might be a better way to say it.

3

u/duffer_dev Mar 30 '21

It depends on how you define the "one thing". 'process_image' should only process the image. Maybe if you want to draw stuff on it, you could have 'draw(text, rect)'. Which internally could have separate calls for drawing text and drawing rectangle. At all levels, it's doing the one thing it was 'defined' to do

2

u/RazzleStorm Mar 30 '21

That line seems similar to the S in SOLID principles, which IMO is a better way to put it.

0

u/[deleted] Mar 30 '21

Look up the difference between function and program.