Hi everyone.
Welcome back.
In this lecture we will code and learn about how unnamed variables can work alongside with pattern matching.
So in this case let's open the record match over here.
And let's go to the animal service class.
So in the animal service class as you can see until Java 21, if you are not using this particular variable
you still need to declare this variable.
Otherwise the compiler will have an issue with that.
But with the unnamed variables, we don't have to do this anymore.
So I'm going to be putting it over here.
And then I'm renaming this to be with unnamed variables.
So what we can do now is basically we can actually add underscore and the code will not complain at
all okay.
So and then we can also use it guarded.
That's missing over here.
So in this case when name equal to equal to null then just return empty string.
Right.
And in the case of this you can see the compilation issue.
Right.
Hey you are just handling the null condition.
If it's not null you need to still handle that condition also.
So if you're going to be not null, then in that case we are going to be adding this over here.
There you go.
So this is the benefit of using a grid pattern.
Here you can actually add this underscore which is the representation of unnamed variable where this
variable is not going to be used at all.
That's what the underscore is for.
So now with the help of the coding assistant we are going to be writing the test for this one.
So let's go ahead and look at some of the test.
Let me move this to the right.
There you go.
So now I'm going to ask the assistant to write a test for me.
So in this case we are actually going to be focusing on the animal service class.
I'm going to ask it to please write a test for this function.
I'm going to just highlight this one in the animal service test.
And I'm also using the agent mode so that it can go ahead and identify what the classes and write the
test for me.
And in my experience, I'm pretty much using the agent mode wherever possible so that it has the ability
to read the code base, understand the context, and make the changes for me.
So in here I'm expecting it to write a test case over here.
Press enter over here so it tells me that I'll help you write a test for retrieve name using guard pattern
with unnamed variables function.
So it's first analyzing this particular class.
As you can see, it created a test for me.
So it's identified like I see there is already a test present for this one.
And it actually added all these scenarios.
Right.
So because in our case this guarded pattern have a different condition checks, we have a null if the
name is null we handle it differently.
If the name is not null we handle it differently.
Right.
So now it added all these things in this way.
This looks like we have multiple assertions happening, but the coding looks very verbose.
Actually the test case looks verbose, so I'm going to ask it to use this pattern.
Hey, why don't you use use the add method source syntax.
Let's see what the changes it's going to do.
Because we already have a pattern over here right.
So in this case it basically tried to use the same functionality that's been used by this particular
test.
And then now it's working as expected Okay.
So now let's go ahead and run this particular test case.
Okay.
The test case is working as expected.
But in my case I want the name to be null.
If it's null, I'm returning an empty string.
Right.
So I want to make further changes to this particular test.
I'm going to ask the agent to look for the change and make the change.
The input to this function can have a null name.
Can you add the check to this test?
And one important thing to call out is that this input function is being used by the other test cases
also.
So what I'm going to ask it to do is do not reuse the input function.
Please add a new one for this test so that the other tests are not going to be impacted.
So the spelling mistakes is still okay.
Actually the visibility the AA model has the ability to understand what you mean by reading through
the whole sentence.
So I'm going to press enter.
Let's see what the changes it's going to make okay.
So now it created the input with null name.
But it's still the changes are going on.
Don't think that hey it introduced a compilation issue, so it's still doing it.
So it removed the old function.
And it created a new function for you with the null values over here.
Okay.
And I'm also going to show you there is a change that it made but it's going to error out.
I'll show you what I mean by that.
So let's go ahead and execute this.
So here for the cat the actual value expected is null.
But in our case we have a explicit condition.
Check if the name is null.
We are returning the empty string right.
So I'm going to be providing this over here.
So I'm going to ask it like for the cat instance if the name is null we are returning an empty string.
Please update the test accordingly.
So give it one more shot.
If it's continuously making changes which you don't expect it to do, that means you can jump in and
manually make the change.
So now what it did, was it basically identified, hey, I see the issue over here.
And then it went ahead and made the change for us.
So in the case of Cat, the expected value is going to be empty string.
In the case of dog it's going to be null.
So now let's go ahead and execute the test case.
There you go.
It's all looking good.
I'm going to accept all.
Accept all.
There you go.
So we have successfully created this test case completely written by the AI assistant.
But you see the iterations that we had to go through, right?
This is normally the practice I have seen.
Basically, you need to guide the LLM or the AI assistant to write the code for you in a way that you
expect the output to be.
So in a nutshell, these a assistants are great, but it definitely requires your guidance in writing
the code in the format or the structure or the way you want.
So you basically give a an input, you get an output.
If the output is not aligned to what you want, you give it a guidance on, hey, go ahead and make
the changes accordingly.
Then it will make the change for you.
But overall it's a very good companion that can work with you 24 by seven and can also help you write
the code in a way you want.
So this is how to use the unnamed variables with pattern matching.
This marks the end of this lecture.
Thank you for watching.