WEBVTT

00:00.000 --> 00:05.000
Debugging, testing, and refining OOP projects.

00:05.000 --> 00:10.000
In this video, we will learn to identify and fix bugs in OOP code.

00:10.000 --> 00:13.000
Use UnitTest to check if a class works correctly.

00:13.000 --> 00:17.000
Refine code to follow best practices and run smoothly.

00:17.000 --> 00:22.000
Let's start by learning how to find and fix bugs.

00:22.000 --> 00:25.000
First, we'll write a buggy code on purpose.

00:25.000 --> 00:29.000
Here, we have a simple bank account class with a withdrawal method.

00:29.000 --> 00:34.000
Now, if we try to take out more money than we have, it should block the withdrawal.

00:34.000 --> 00:37.000
But watch what happens when we run the program.

00:37.000 --> 00:40.000
The balance goes negative.

00:40.000 --> 00:41.000
That's the bug.

00:41.000 --> 00:45.000
Our code says insufficient funds, but still subtracts the money.

00:45.000 --> 00:48.000
So now we know exactly where the problem is.

00:48.000 --> 00:51.000
Debugging the code.

00:51.000 --> 00:54.000
To fix the bug, we add a condition.

00:54.000 --> 00:59.000
If the amount is greater than the balance, we stop right there and return false.

00:59.000 --> 01:03.000
This way, no money is removed when the withdrawal is invalid.

01:03.000 --> 01:04.000
Let's run it again.

01:04.000 --> 01:10.000
This time, when we try to take out more than we have, the program says insufficient funds.

01:10.000 --> 01:12.000
But the balance stays the same.

01:12.000 --> 01:16.000
And when we withdraw a valid amount, it works perfectly.

01:16.000 --> 01:19.000
The bug is gone.

01:19.000 --> 01:21.000
Testing the code.

01:21.000 --> 01:25.000
A unit test is like a checklist that makes sure our class works correctly.

01:25.000 --> 01:29.000
We test deposits, valid withdrawals, and invalid ones.

01:29.000 --> 01:37.000
When all tests pass, the program shows OK, which means our code is reliable.

01:37.000 --> 01:40.000
Refining the code.

01:40.000 --> 01:45.000
First, we change the balance variable to underscore balance and make it private.

01:45.000 --> 01:49.000
Then, we add a property so balance becomes read-only.

01:49.000 --> 01:52.000
We can check it, but not change it directly.

01:52.000 --> 01:55.000
Next, we improve the withdrawal method.

01:55.000 --> 01:59.000
We add checks so if the amount is zero, negative, or more than the balance,

01:59.000 --> 02:02.000
the program prints an error and stops.

02:02.000 --> 02:05.000
Only valid withdrawals are allowed.

02:05.000 --> 02:09.000
Finally, when we run it, the account starts with 100.

02:09.000 --> 02:14.000
A withdrawal of 150 is blocked with an error, but withdrawing 80 works fine.

02:14.000 --> 02:18.000
The final balance is updated correctly.

02:18.000 --> 02:21.000
Debugging helps us find and fix mistakes.

02:21.000 --> 02:25.000
Testing makes sure the code works and refining improves quality.

02:25.000 --> 02:30.000
Together, they turn an OOP project into a clean and reliable program.

