I'm a beginner Python programmer. What factors should I consider when writing code to write great code?

admin 72 0

A fundamental idea to keep straight is the difference between WHAT your program does and HOW it works. At the bottom level, most programming languages are procedural, which means: do this then do this then do this then do this. So it is easy to focus on HOW something gets done, the sequence of steps. But. We humans do not build bigger programs by just having more steps. We make groups of steps, typically called functions, and label them with WHAT they do. For example, consider the square root function. If the manual said HOW it works, “take the input number and find the order of magnitude and then cut it in half and put it as the exponent of Y and then take the first digit of the input and cut it in half and put it in mantissa of Y …”, you would never use it. Square root is WHAT it does and that is what you need to know to use it. The same thing will become true as your programs get bigger and you make functions. And most machines do not formally know WHAT a function does. WHAT is just for us, so it goes in the comments. And we all know that beginning programmers think comments are pointless. “I just wrote this function, I know how it works!” Yes, but that is the point. You want to free up all of those brain cells tied up in all of those steps of HOW it works. All you want to keep is WHAT it does, then move to the next level. And as you program, you should constantly be looking for groups of steps that you can hide under a single statement of WHAT they do. With a good comment saying WHAT a function does, you can fold up the code and never be bothered with HOW it works again.

Post comment 0Comments)

  • Refresh code

No comments yet, come on and post~