Lesson video

In progress...

Loading...

Hello, my name is Mrs. Jones and I'm really pleased you decided to join this lesson today.

In this lesson, we will look at the software design principles and how these benefit coding practises and also look at the different ways to improve your code using these principles.

So let's get started.

Welcome to today's lesson.

Today's lesson is called Software Design Principles from the unit Python Programming Project, and by the end of this lesson you will be able to define and explain key software design principles.

There are four keywords to today's lesson: Modularity.

Modularity is breaking software into independent, self-contained units or modules.

Abstraction.

Abstraction is focusing on the important parts of a problem and ignoring irrelevant details.

Readability.

Readability is how easily someone can understand your code.

Maintainability.

Maintainability is making software easy to change, fix, or update later on.

There are two sections to today's lesson.

The first is Define key software design principles.

The second is Recognise poor design and suggest improvements.

So let's start with Define key software design principles.

Sofia says, "What are software design principles?" Really good question and a good one to start us off with.

Software design principles are guidelines that help developers write clear, efficient, and maintainable code.

By following software design principles, developers can create software that works well, is easy to understand and can be changed and developed over time.

Modularity is the principle of breaking software into independent, self-contained units or modules.

By doing this, software will be easier to test and debug, reuse, and develop in parallel with other modules.

For example, if you are developing a shopping app, you may split the software into the following modules: products, user accounts, cart, payment.

It's a good way to lay it out, using that diagram there where you've got the shopping app as the starting point and how you break it down into those other sections, those other modules.

Abstraction is the principle of removing the details that don't matter, so that the most important details of a problem are easier to focus on.

By doing this, software developers can reduce the complexity of a programme or a problem, focus their attention on what matters, and make code easier to use.

For example, when developing an animal guessing game, you need to have a collection of images of animals to show the user.

In this case, photographic details of the animal are not required as you are simply testing the user's ability to recognise the animal.

Even the features of the animal, such as size and colour are not necessarily required if you focus on the most important information to effectively represent the animal.

And you can see this on the diagram below.

We've gone from a picture to an illustration to just the key areas, the most important bit to help us recognise that it is a cat.

Let's have a quick check.

What principle has been applied to this map? Is it A modularity? B: decomposition? Or C: abstraction? Pause the video, look at the change from the one map to the next and decide which one it is using, which principle, and then we'll check your answer.

Let's check your answer.

The answer was abstraction.

Well done if you got that correct.

Readability is how easy someone can understand your code.

Readability matters because it saves time in reviews and debugging, helps teams collaborate and work on the same projects, makes it easier for new developers or users to understand and develop the code if needed.

Some strategies which can be used to ensure readability include: using meaningful names for variables and functions, keeping functions and modules short and adding comments where needed.

Maintainability is the principle of making software easy to change, fix, or update later on.

Maintainability is important because it saves money and time in the long term, reduces bugs when requirements change, allows software to be kept up to date.

Maintainability can be achieved by: following coding standards, for example, using meaningful identifiers, adding comments; avoiding hard coding, instead, use variables or inputs that can be changed easily; writing modular, testable code.

Let's have a quick check.

Match the software design principle to the correct description.

You have on the left, modularity, abstraction, readability, maintainability, and you need to connect that to the right description on the right hand side.

Pause the video and then we'll go through the answers.

And let's check your answers.

Modularity is splitting code into smaller parts.

Abstraction: hiding complexity and unnecessary detail.

Readability: making it easy for someone to understand code.

And maintainability: making it easy to develop and improve code in the long term.

Well done if you got those correct.

Let's do the activity.

Write a short definition of each of the following, software design principles: modularity, abstraction, readability and maintainability.

Pause the video, go back through the slides, use your worksheet and then we'll go through the answers.

Let's check your answers.

Modularity means splitting code into smaller parts called modules, so each part does one thing.

Abstraction is hiding the details that are not needed and only focusing on the important parts that help solve the problem.

Readability is how easy code is to read and understand.

If someone else can look at your code and understand it, then it is readable.

Maintainability means how easy it is to fix or update code later.

If code is well structured and readable, it's easier to change and develop things later on.

Well done if you got those correct.

Let's move on to the second part of today's lesson: Recognise poor design and suggest improvements.

Sofia says, "But how can I spot poor software design?" Really good question.

Poor software design is easy to spot if you know the software design principles, modularity, abstraction, readability, maintainability.

Poor modularity.

Let's have a look at this one.

What is wrong with this code? Pause the video for a second and look at that code and see if you can see what is wrong.

Let's have a look.

Jun says, "Everything is in the same programme.

The programme mixes input, calculation and output all in one place." Poor abstraction.

What is wrong with this code? Pause the video and have a look at that code.

Can you see what's wrong? Let's have a look.

Jun says, "The programme contains lots of unnecessary information.

This could be confusing for the user." If you look there, everything is an output statement, isn't it? Print and it's saying the user would be shown all of that information.

Let's have a quick check.

What poor software design does this code exhibit? Is it A: poor modularity? B: poor abstraction? Or C: poor readability? So we've got def f, and inside the brackets x, y, z.

And we've got a=x multiplied by y, b=a multiplied by z and then print("Final price is", b).

Pause the video to consider which software design principle is it poor on? And then we'll go through the answer.

Let's check your answer.

The answer was readability, because it is not clear what the function or the variable names actually represent there.

Well done if you got that correct.

Here is an improved version of that code.

So Sofia has done this.

Now we've got def, so define calculate_final_price, so the name is much better for that function.

And then we've got price, quantity, and tax rate.

All of those calculations now are a lot easier to read and understand and we can get a clear picture of what it is that code is meant to do.

Let's have a quick check.

How would the improved code also improve maintainability? Pause the video to consider your answer and then we'll check it.

Let's check your answer.

The clear variable and function names not only improve readability, they also allow future developers, or you in a few months time, to see what each part of the programme does.

This makes it easy to update or fix the programme if needed.

Well done if you got that correct.

Let's do the activity.

The code below takes a user's name and age.

If the user is over 18, they are allowed to enter.

If not, they are informed that they are too young.

So let's have a look at the code.

We've got print, enter your name.

Text equals input, print("Enter your age:") number equals integer input.

If the number is greater than or equal to 18, then it will output "Hello, you are allowed to enter", else it will output "Sorry, you are too young." And there's two parts to this task.

One, what poor design decisions can you recognise? And two, suggest some improvements to the code.

Pause the video to consider your answers and then we'll go through them.

Let's have a look at the answers.

So for the first part, what poor design decisions can you recognise? Well, we've got non meaningful variable identifiers here.

We just have text, which doesn't really mean anything, so it doesn't make it make sense at all as a variable name.

No abstraction, everything is in one place.

So if you wanted to use a piece of this code in another programme, you would have to use it all.

Low levels of maintainability.

The user's age is hard coded, so if we wanted to lower the age, we would have to find this in the programme and change it.

For the second part, well, let's suggest some improvements to the code.

So now we've got age limit, as a new variable at the top, equals 18.

Then we have a function, get user info.

And in this function we're asking for the user to input their name and we're storing it in the variable name.

We're asking the user to input their age and we're storing it in a variable age.

And the output of that function is name and age.

We also have another function which is is old enough, and this time we've got age and limit and we're returning the age if it's greater than or equal to the limit.

We also got another function to define print message, which is name and allowed is going in the brackets there.

So if allowed, it's going to say "You're allowed to enter" else, it's gonna say "sorry, you are too young." It's continued on the next page as well.

And then we have the main programme.

We've used a comment here, you can see by the hashtag to make it clear that this is a comment.

And underneath we've got, user_name, user_age = get_user_info().

We've got can_enter = is_old_enough, and we've got the user age and age limit.

And then we've got print_message, user_name, and can_enter, so all those functions are called in the main programme.

Well done if you completed the activity and identified the areas as well as the improvements to the code as well.

In summary, software design principles are guidelines that help developers write clear, efficient, and maintainable code.

Good design makes code easy to read, test and change.

Well done for completing this lesson on software design principles.