Presentation is loading. Please wait.

Presentation is loading. Please wait.

Creating readable code

Similar presentations


Presentation on theme: "Creating readable code"— Presentation transcript:

1 Creating readable code
Learning Python Creating readable code

2 Code Readability Always try to make any code that you write is easy to read and understand This benefits you and anyone else who reads your code When you go back after a short time its easy to forget exactly why you wrote the code the way you did You should apply good readability principles to pseudocode and real code

3 Good Techniques - 1 At the beginning of the code include :
Your Name Date Purpose Use descriptive names for variables eg userChoice instead of num Add blank lines between different block of code Add comments that explain what each part of the code does You can over comment Commenting each line is unnecessary and makes the code less readable

4 Good techniques - 2 Technique Description Comments
Comments should be used to: Who wrote the code Date of writing Explain the purpose of the program Explain what each part does Explain any part where logic or setting variables is difficult to understand Descriptive Names Using descriptive identifiers for variables, constants and functions helps to make their purpose clear Use camel case for multi-word names eg firstName, totalScore Indentation Indentation makes it clear where each block of code starts and finishes. Getting this wrong can result in the program not running or producing incorrect results White space Spaces between Assignment operators and values Selection tests Blank lines between different blocks of code to make them stand out : If blocks While and For loops

5 Use the Python comment character # to start your comments
Exercise 1 Rewrite this algorithm using appropriate techniques to make it easier to read: Set X to 10 While X >= 0 Do If X > Print (X) Else Print(“Blast Off”) End If X = X -1 End While Use the Python comment character # to start your comments

6 Exercise 1 - Solution # Name: your name # Date: Today’s date # Purpose: Count down from and display a message #initialise variables counter = 10 While counter >= 0 Do If counter > Print (counter) Else Print(“Blast Off”) End If counter = counter -1 End While


Download ppt "Creating readable code"

Similar presentations


Ads by Google