WEBVTT

00:00.320 --> 00:00.960
Hi everyone.

00:00.960 --> 00:01.560
Welcome back.

00:01.560 --> 00:07.200
In this lecture we will code and learn about how unnamed variables can work alongside with pattern matching.

00:07.200 --> 00:10.000
So in this case let's open the record match over here.

00:10.000 --> 00:12.040
And let's go to the animal service class.

00:12.240 --> 00:18.200
So in the animal service class as you can see until Java 21, if you are not using this particular variable

00:18.440 --> 00:20.880
you still need to declare this variable.

00:20.920 --> 00:23.880
Otherwise the compiler will have an issue with that.

00:23.880 --> 00:27.680
But with the unnamed variables, we don't have to do this anymore.

00:27.680 --> 00:29.800
So I'm going to be putting it over here.

00:29.800 --> 00:34.120
And then I'm renaming this to be with unnamed variables.

00:34.440 --> 00:40.560
So what we can do now is basically we can actually add underscore and the code will not complain at

00:40.560 --> 00:41.280
all okay.

00:41.320 --> 00:43.520
So and then we can also use it guarded.

00:43.520 --> 00:44.720
That's missing over here.

00:44.720 --> 00:50.080
So in this case when name equal to equal to null then just return empty string.

00:50.120 --> 00:50.480
Right.

00:50.520 --> 00:52.760
And in the case of this you can see the compilation issue.

00:52.760 --> 00:53.080
Right.

00:53.080 --> 00:55.240
Hey you are just handling the null condition.

00:55.400 --> 00:58.440
If it's not null you need to still handle that condition also.

00:58.440 --> 01:03.540
So if you're going to be not null, then in that case we are going to be adding this over here.

01:03.580 --> 01:04.060
There you go.

01:04.060 --> 01:06.500
So this is the benefit of using a grid pattern.

01:06.540 --> 01:12.820
Here you can actually add this underscore which is the representation of unnamed variable where this

01:12.820 --> 01:15.180
variable is not going to be used at all.

01:15.180 --> 01:17.100
That's what the underscore is for.

01:17.140 --> 01:22.420
So now with the help of the coding assistant we are going to be writing the test for this one.

01:22.420 --> 01:24.540
So let's go ahead and look at some of the test.

01:24.980 --> 01:26.100
Let me move this to the right.

01:26.140 --> 01:26.620
There you go.

01:26.620 --> 01:30.220
So now I'm going to ask the assistant to write a test for me.

01:30.260 --> 01:35.380
So in this case we are actually going to be focusing on the animal service class.

01:35.700 --> 01:40.260
I'm going to ask it to please write a test for this function.

01:40.300 --> 01:44.060
I'm going to just highlight this one in the animal service test.

01:44.060 --> 01:49.340
And I'm also using the agent mode so that it can go ahead and identify what the classes and write the

01:49.340 --> 01:50.060
test for me.

01:50.620 --> 01:55.900
And in my experience, I'm pretty much using the agent mode wherever possible so that it has the ability

01:55.900 --> 01:59.620
to read the code base, understand the context, and make the changes for me.

01:59.620 --> 02:02.460
So in here I'm expecting it to write a test case over here.

02:02.500 --> 02:08.630
Press enter over here so it tells me that I'll help you write a test for retrieve name using guard pattern

02:08.630 --> 02:10.150
with unnamed variables function.

02:10.150 --> 02:12.230
So it's first analyzing this particular class.

02:12.990 --> 02:15.910
As you can see, it created a test for me.

02:15.910 --> 02:20.590
So it's identified like I see there is already a test present for this one.

02:20.590 --> 02:22.790
And it actually added all these scenarios.

02:22.830 --> 02:23.070
Right.

02:23.070 --> 02:29.790
So because in our case this guarded pattern have a different condition checks, we have a null if the

02:29.790 --> 02:31.790
name is null we handle it differently.

02:31.790 --> 02:34.830
If the name is not null we handle it differently.

02:34.830 --> 02:35.310
Right.

02:35.310 --> 02:37.950
So now it added all these things in this way.

02:37.950 --> 02:43.030
This looks like we have multiple assertions happening, but the coding looks very verbose.

02:43.070 --> 02:47.310
Actually the test case looks verbose, so I'm going to ask it to use this pattern.

02:47.310 --> 02:51.870
Hey, why don't you use use the add method source syntax.

02:52.150 --> 02:54.070
Let's see what the changes it's going to do.

02:54.070 --> 02:57.710
Because we already have a pattern over here right.

02:57.710 --> 03:03.190
So in this case it basically tried to use the same functionality that's been used by this particular

03:03.190 --> 03:03.830
test.

03:03.830 --> 03:06.410
And then now it's working as expected Okay.

03:06.450 --> 03:09.170
So now let's go ahead and run this particular test case.

03:11.450 --> 03:11.650
Okay.

03:11.690 --> 03:13.530
The test case is working as expected.

03:13.530 --> 03:16.650
But in my case I want the name to be null.

03:16.650 --> 03:18.730
If it's null, I'm returning an empty string.

03:18.730 --> 03:19.410
Right.

03:19.410 --> 03:23.250
So I want to make further changes to this particular test.

03:23.450 --> 03:26.850
I'm going to ask the agent to look for the change and make the change.

03:27.250 --> 03:30.530
The input to this function can have a null name.

03:30.650 --> 03:32.610
Can you add the check to this test?

03:32.610 --> 03:37.650
And one important thing to call out is that this input function is being used by the other test cases

03:37.650 --> 03:38.330
also.

03:38.330 --> 03:42.890
So what I'm going to ask it to do is do not reuse the input function.

03:42.890 --> 03:49.130
Please add a new one for this test so that the other tests are not going to be impacted.

03:49.130 --> 03:50.690
So the spelling mistakes is still okay.

03:50.730 --> 03:56.490
Actually the visibility the AA model has the ability to understand what you mean by reading through

03:56.490 --> 03:57.170
the whole sentence.

03:57.170 --> 03:58.290
So I'm going to press enter.

03:58.330 --> 04:00.610
Let's see what the changes it's going to make okay.

04:00.650 --> 04:04.170
So now it created the input with null name.

04:04.170 --> 04:05.810
But it's still the changes are going on.

04:05.810 --> 04:09.790
Don't think that hey it introduced a compilation issue, so it's still doing it.

04:09.790 --> 04:11.350
So it removed the old function.

04:11.510 --> 04:16.470
And it created a new function for you with the null values over here.

04:16.510 --> 04:16.870
Okay.

04:16.910 --> 04:21.710
And I'm also going to show you there is a change that it made but it's going to error out.

04:21.750 --> 04:23.590
I'll show you what I mean by that.

04:23.630 --> 04:25.150
So let's go ahead and execute this.

04:25.150 --> 04:28.790
So here for the cat the actual value expected is null.

04:28.830 --> 04:31.190
But in our case we have a explicit condition.

04:31.190 --> 04:32.870
Check if the name is null.

04:32.870 --> 04:35.070
We are returning the empty string right.

04:35.070 --> 04:37.510
So I'm going to be providing this over here.

04:37.510 --> 04:43.510
So I'm going to ask it like for the cat instance if the name is null we are returning an empty string.

04:43.510 --> 04:45.150
Please update the test accordingly.

04:45.150 --> 04:46.350
So give it one more shot.

04:46.350 --> 04:51.550
If it's continuously making changes which you don't expect it to do, that means you can jump in and

04:51.550 --> 04:53.470
manually make the change.

04:53.470 --> 04:57.830
So now what it did, was it basically identified, hey, I see the issue over here.

04:57.990 --> 05:00.750
And then it went ahead and made the change for us.

05:00.750 --> 05:04.470
So in the case of Cat, the expected value is going to be empty string.

05:04.470 --> 05:07.550
In the case of dog it's going to be null.

05:07.550 --> 05:09.800
So now let's go ahead and execute the test case.

05:10.040 --> 05:10.400
There you go.

05:10.440 --> 05:11.160
It's all looking good.

05:11.200 --> 05:12.280
I'm going to accept all.

05:12.280 --> 05:12.880
Accept all.

05:12.920 --> 05:13.360
There you go.

05:13.400 --> 05:19.640
So we have successfully created this test case completely written by the AI assistant.

05:19.640 --> 05:22.960
But you see the iterations that we had to go through, right?

05:23.000 --> 05:25.200
This is normally the practice I have seen.

05:25.640 --> 05:33.040
Basically, you need to guide the LLM or the AI assistant to write the code for you in a way that you

05:33.040 --> 05:34.600
expect the output to be.

05:34.600 --> 05:41.800
So in a nutshell, these a assistants are great, but it definitely requires your guidance in writing

05:41.800 --> 05:45.760
the code in the format or the structure or the way you want.

05:45.800 --> 05:49.400
So you basically give a an input, you get an output.

05:49.440 --> 05:54.560
If the output is not aligned to what you want, you give it a guidance on, hey, go ahead and make

05:54.560 --> 05:55.760
the changes accordingly.

05:56.000 --> 05:57.480
Then it will make the change for you.

05:57.480 --> 06:03.280
But overall it's a very good companion that can work with you 24 by seven and can also help you write

06:03.280 --> 06:04.800
the code in a way you want.

06:04.840 --> 06:09.680
So this is how to use the unnamed variables with pattern matching.

06:09.880 --> 06:11.160
This marks the end of this lecture.

06:11.160 --> 06:12.160
Thank you for watching.
