Loading...
Hello, my name is Mrs Holborow, and welcome to Computing.
I'm so pleased that you've decided to join me for the lesson today.
In today's lesson, we're going to be looking at good programming practises, which you can use to improve your code and make it easier for other people to understand the code you have written.
Welcome to today's lesson from the unit, Programming Selection.
This lesson is called Good Programming Practises.
And by the end of today's lesson, you'll be able to explain what is meant by good programming practises, and adopt these practises when writing your own code.
Shall we make a start? We will be exploring these keywords throughout today's lesson.
Comment, comment, text within source code that is ignored by the compiler or interpreter, used to help programmers understand the code.
Identifier, identifier, the name that a programmer gives to a variable, constant, or subroutine.
Maintainability, maintainability refers to the ease in which maintenance activities can be performed on code, such as correcting errors.
Look out for these keywords throughout today's lesson.
Today's lesson is split into two parts.
We'll start by recognising good programming practises, and then we'll move on to modify a programme to use good practise.
Let's make a start by recognising good programming practises.
Software engineers often work in teams or on collaborative projects.
Mastering good programming practises is essential to working productively with others.
Good programming practise involves taking steps to make your code easier to read and understand for anyone who may need to improve, use, or maintain it in the future.
You may not necessarily be working on a project with someone during the development, but someone else may need to adapt and maintain your programme in the future.
Izzy says, "But I'm only studying programming for my GCSE.
Nobody else is going to be using my code." Andeep has a really good point.
"It's a good idea to start following good programming practises now so they become good habits for the future." Good programming practises include comments, programme layout, identifiers, and naming conventions.
We're going to look at each of these in a bit more detail in a minute.
Comments are used to make the code more readable to a programmer who seeks to understand it.
Comments should be used to explain parts of the code that are not obvious, or to give a summary of a subroutine or particular piece of code.
Comments can either be placed at the end of the line that they relate to, or on the line immediately beforehand.
Careful spacing is also useful to make it clear which comment goes with which line of code.
On the code on the screen, you can see an example of a comment and the syntax highlighting that's applied to it in the RPF code editor.
The layout of your programme is important because a consistent and logical layout makes it much easier for other programmers to understand your code.
Code within a file should be adequately separated by white space, this means return and space characters, to make it more readable.
Indentation is a vital part of a program's layout.
In some languages like Python, the indentation is an essential part of the syntax that determines how the programme is executed.
In other languages, the indentation is not required, but not indenting the code makes it more difficult to read.
Time to check your understanding.
What good programming practise could be used to improve this code? Is it, A, comments? B, programme layout? Or, C, error checking? Pause the video whilst you have a think.
Did you select B? Well done.
The programme layout of this code makes it difficult to read.
This code could be split over multiple lines and white space added.
The identifier is the name given to a variable by a programmer.
Identifiers are also used for other programme components, such as subroutines.
Variables should always be named in a meaningful way.
This makes them much easier for a human to read and follow in a programme.
The identifiers also contribute to making your code self-documenting.
By self-documenting, we mean the code explains itself.
Here is some code.
In this example, X is not a helpful variable name as it doesn't tell us what the variable actually represents.
The code on line two has improved because we've now called the variable score.
So that tells us a bit more about what the variable is holding.
Most programming languages have their own naming conventions.
These are a standard way for identifiers to be written in code.
Some of these standards are non-optional rules, such as not allowing variable names that include spaces or start with a number.
When creating a variable in Python, the convention is to write it in lowercase with an underscore separating the words, which is actually known as snake case.
Here's an example, so we have last_name, all lowercase.
Time to check your understanding.
What identifier would be most suitable for a variable holding a pupil ID number? Hint, you need to follow common naming conventions here.
Pause the video whilst you have a think.
Did you select C? Well done.
The most suitable identifier would be pupil_id.
The identifier should easily identify what the variable holds, as well as follow common naming conventions.
So you can see here we've used all lowercase, and we've used an underscore where the words are separated.
Okay, we're moving on to our first task of today's lesson.
And you're doing a great job so far, so well done.
Look carefully at the code.
Annotate the code to explain the good programming practises that have been used.
Pause the video whilst you complete the activity.
How did you get on? Did you manage to spot some good programming practises? Let's have a look at some sample answers together.
So on line six, we have a variable called total, which is assigned to zero.
This is a meaningful identifier name in the programme.
On line six, we've also used a comment to explain the variable and how it's used.
On line seven, we've used some white space to separate the parts of the programme.
So we've separated the variable assignment from the selection blocks.
And then down on line 17 and 18, we've used some indentation of selection blocks.
This is really important in Python, and the programme wouldn't run without it.
But in some programming languages, this is more of a design feature.
Okay, we're now moving on to the second part of today's lesson where we're going to modify a programme using good practise.
Andeep says, "What would happen if I didn't follow good programming practises?" Maybe pause the video and have a think.
Did you think of some consequences of not using good programming practises? Here are some we can share together.
Your code may become difficult to maintain or fix.
Other programmers or your teacher may not be able to understand your code.
You may find your code difficult to understand if you haven't worked on it for a while.
Time to check your understanding.
Match the programming practise to the correct description.
Pause the video whilst you have a go.
How did you get on? Did you manage to match the practise to the descriptions? The first one was comments.
These explain parts of code that are not obvious to someone else looking at your code.
Identifiers, variables should always be named in a meaningful way.
This makes them much easier for a human to read and follow in a programme.
Programme layout, a logical layout makes it much easier for other programmers to understand your code.
Naming conventions, a standard way for identifiers to be written in code.
Some languages have non-optional rules.
How could this code be improved? Perhaps pause the video whilst you have a look carefully at the code.
Ah, Jun's got an improvement.
Jun says, "Some white space could be added to aid readability." That's a really good point, Jun.
How could this code be improved? Perhaps pause the video whilst you have a look carefully at the code.
Ah, Jun has spotted that the identifiers could be given more meaningful names.
Here we've got an identifier called H and another identifier called HINF.
What do these stand for? What would be a more suitable identifier on line three of this programme? Is it, A, height? B, length? Or, C, height_in_feet? Pause the video whilst you have a think.
That's right, the most suitable identifier is height_in_feet.
That tells us much more about what this variable holds.
Okay, we're now moving on to the second set of tasks for today's lesson.
I'd like you to open the starter programme, oak.
link/improve-code.
I'd like you to use good programming practises to make some improvements to the code.
And then I'd like you to explain what you changed and why.
Pause the video whilst you complete the activity.
How did you get on? Did you manage to make some improvements to the code? If you want to have a look at a solution, you can go to oak.
link/improve-code-solution.
Look carefully at the code and see what modifications have been made.
Here's a sample answer for you.
I started by changing the variable names to more meaningful identifiers.
For example, I changed A to chocolate_price.
I then added some white space to improve the readability of the code.
Finally, I added in some comments to explain sections of the code.
Did you do some similar things to improve the code? Remember, if you need to, you can always go back and make some more improvements to the code, and pause the video now.
Okay, we have come to the end of today's lesson.
And you've done a fantastic job, so well done.
Let's summarise what we've learned together today.
Good programming practise involves taking steps to make your code easier to read and understand.
Variables should always have meaningful identifiers so that the reader of the code understands how they are used.
Comments are used in programmes to help make it easier for a programmer to understand what the code does.
I hope you've enjoyed today's lesson, and I hope you'll use these good programming practises in your code going forwards.
See you again soon, bye.