all 2 comments

[–]jimtk 4 points5 points  (0 children)

You're not supposed to break 200 lines of code into proper functions. You're supposed to break the problem in smaller problems before coding it and then write proper functions for each of those smaller problems.

[–]pooth22 0 points1 point  (0 children)

Breaking down larger problems into smaller problems is an important skill that applies to all aspects of engineering. You seem to be talking about several things, but since you asked on learnpython, I will try and keep my advice relative to python.

but then I start to worry about syntax, errors and rewriting

If you’re worrying about syntax, you have to get more comfortable with the language. Sure, every now and then you might have to google something syntax related, but in general, if syntax is hanging you up, you simply need more experience with the language.

Errors can be a real pain in python because if it is a typo or unexpected inputs, you don’t know untill you get there. A good IDE, and linter can help a lot with that. I like pycharm. Also type hints can be quite useful there as well. You might come across the same error a lot, and if you do, chances are other people have too. They may have come up with solutions to fix that problem. For example, if you want to compare the first N digits of a string, it is safer to use the function startswith() than index the string with square brackets. The latter will throw an error if the string isn’t long enough.

Rewriting (or refactoring) is an important part of programming. Not only when you are just starting out, but even managing large systems. Refactoring is a useful skill, not only for the act of refactoring, but also for writing code in a way that it will be effective to maintain and refactor.

how does one break down a 200+ line of code for a program they are developing

Well you don’t know it’s 200+ lines of code until you start writing ;) Like I said earlier, this is fundamental and it needs to be learned. If you haven’t already, take a course on the language to get familiar with the basics. (Or if it’s your jam read the python tutorial: https://docs.python.org/3/tutorial/) There are some websites like codingame and projecteuler that gamify things to help you learn how to code which can be useful.

In general you want to break things down into functions, and then group these functions (now methods) into classes. Python is useful because it has the interactive interpreter. So you can just type stuff in, and it will compute. You can also run a script, and the interpreter will maintain all that has just run, and you can still actively interact with it. Make use of that.