Skip to content

Latest commit

 

History

History
72 lines (47 loc) · 2.4 KB

writing_code.md

File metadata and controls

72 lines (47 loc) · 2.4 KB

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

  1. Write a line of code
  2. Execute it
  3. Check whether it is doing what you want
  4. 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

  1. Copy a small working piece of code
  2. Execute it without modification
  3. Make sure the code is working
  4. Understand what the program is doing
  5. 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

  1. Begin with a working piece of code
  2. Modify a few lines
  3. Execute the program
  4. 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

  1. Write class and function definitions, but leave the bodies of the functions empty
  2. Make each function return dummy values
  3. Write a main section that uses the classes / functions
  4. Execute everything and make sure the program runs without Exceptions
  5. 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:

  1. Write the entire program first
  2. 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.