Coding Strategies¶
Python code can be developed using many strategies. Here you find a few beginner-friendly ones that are also used by experienced professionals.
Line by Line¶
Write a line of code
Execute it
Check whether it is doing what you want
Back to 1.
This strategy is useful mainly for experimenting with new commands and while working in an
interactive Python Shell or a Jupyter Notebook.
It also works with an editor as long as you either generate output with print() after every command or use your editors controls to step throught the program line by line.
Copy-Paste¶
Copy a small working piece of code
Execute it without modification
Make sure the code is working
Understand what the program is doing
Modify the code
This is a good strategy for trying out new tools or programming libraries. Most Python packages come with a set of examples that you can try out directly.
Copy-pasting code from documentation, tutorials or pages like StackOverflow is a totally legitimate coding strategy!
Modify a Program¶
Begin with a working piece of code
Modify a few lines
Execute the program
Observe what happens
Starting with an existing program is often more challenging than writing everything from scratch. The main difference to the copy-paste strategy is that in step 4. you observe. Often you can learn something new here.
Skeleton Code¶
Write class and function definitions, but leave the bodies of the functions empty
Make each function return dummy values
Write a main section that uses the classes / functions
Execute everything and make sure the program runs without Exceptions
Start filling the function bodies one by one
This is a somewhat different strategy that lets you think about the structure of a program without the details of the implementation getting in the way.
Write everything in one go¶
CAUTION: This is not an easy strategy:
Write the entire program first
Then execute it and make sure it works
The difficulty in Step 2 is that you not only have to deal with normal bugs. You are also confronted with semmantic mistakes and miscondeptions that you made while coding. It is very easy to get stuck here and give up.
Writing programs with more than 20 lines is not easy for experienced programmers. I hope the strategies listed here give you a few ideas for taking the next step.