In this video we're going to start to clean up our set color function as I mentioned in the last video.
There's definitely a little bit more appropriate syntax so we can use here to decide exactly what color
we're trying to work on.
So we could refactor this instead of having a series of IF statements we could instead use a switch
statement that would look like this.
I could instead say of instead the if statement right here I could say switch over color and then open
up a curly brace and notice how I've still got the closing Curly race from our if statement we had just
a moment ago right there.
So then inside of here we could add in a case where color is equal to the string red.
So if color is equal to red we can run this entire f statement right here so I'll tab that in like so
and then for this to be a proper switch statement or a proper case.
We always make sure we terminate the case with the return at the very end.
So we don't fall through to any other case statements or in other cases that we might have inside of
here so I'm going to say this really quickly and do another quick test just to make sure everything
still works and yet it still looks good to me.
Now even with this change having the nested IF statement here is still a little bit challenging to read.
So there is yet another way that we could refactor this.
This is a totally optional refactor but it definitely would increase the legibility of what's going
on here.
So I could say I could use something called a ternary expression a Turner expression is going to essentially
check one statement and then if that statement evaluates to a truth you value it will run some immediate
statement like in this case return.
Otherwise it will run some other statement like set red.
So essentially ternary is going to do exactly what we have with our if statement right here already.
But it's just going to kind of condense down this logic so to do so I would write out red plus change
greater than fifty five or read plus change less than zero.
And here's where the ternary comes in we'll put in a question mark like so the question mark means that
this is going to be the expression we want to run.
If all this evaluates to a truth we value unfortunately we cannot return directly from a ternary statement.
So instead we're just going to evaluate this expression to know like so that essentially means don't
do anything.
So if this is true right here if we are going to be outside outside of our bounds of red then don't
do anything at all otherwise.
So I'll put a call in there and that means if this is all SAE then do this.
And in this case I want to update red by calling set red with red let's change now we can clean up that
entire if statement.
We're down to just one line of code.
Now you can definitely argue that this is kind of hard to read right here especially if you're not familiar
with ternary expressions.
But once you start to get a little bit more familiar with them.
Well you're going to be very comfortable kind of taking a glance at this and understanding what's going
on.
So I'm going to again save this.
We'll do another quick test now if you see any error inside of your simulator right now.
I can just guarantee you that you've got a typo somewhere inside of this line.
So do double check this line of code double check the entire switch statement because if you're seeing
an error that's only popping up right now.
You definitely have a typo right here OK.
So I'll do another quick test.
I'll increase red Yep decrease red.
Yep.
Still works.
Very good.
Now we can easily essentially copy paste this statement right here and handle our other two colors of
blue and green as well.
So going to copy the case red paste it once and twice and I'm going to go through it and make sure I
change all the color references inside of here.
So first I'll update the string to be green all then do red red.
Set red and red.
So I have to do four more changes here.
Make sure you do four changes on this line right here.
My first one there's one there's two I'll do red right here inside of the parentheses.
There's three.
And then finally I will update set red to set Green.
There's four.
So make sure you do for updates as well.
Now we can finish up with the last case.
So I'll update.
Case Red I'll change that string right there we want to handle a case of blue.
Here and now I'll update this line again.
Once again we'll have four separate changes.
So here's one here is to here is three and there's four.
I updated the setter as well so once again if you have an error at this point going forward chances
are you made a typo somewhere inside of here.
So I beg you please triple check please make sure that K screen has no mention of red whatsoever and
make sure that case Blue has no mention of red whatsoever as well.
The last thing we will do here.
Technically we don't have to do this but usually by convention in a switch statement we add in kind
of a default case at the very bottom.
We say default like so.
So if our color that we passed in did not match red green or blue we'll instead run this case right
here.
In which case we'll just return and say don't do anything okay.
So now our set color function can handle all these different types of changes now that we've updated
set color.
We can go back down to all of our callbacks down here and we can update set blue and set Green to use
set color instead so we'll start off with blue first.
I'll change this to set color and set color again make sure you update both those then as a first argument
will pass in blue as a string and as a second argument will pass in for the first one positive color
increment and for the second one will do negative 1 times color increment all right.
So once again make sure you've got set color set.
Color blue and blue add strings and then positive color increment and negative color increment now will
repeat that same process on set green.
So we'll do a set color on both those string of green and then our second arguments will add in those
commas and I'll do negative 1 times color increment and the first will be positive 1 All right and that's
pretty much it.
So once again I got a bigger because the changes we're making here you've made a lot of video courses
over time and the changes we just made it's extremely likely likely that you might have just accidentally
made a typo somewhere inside of here.
So if your application is not working after this then I can guarantee you you got a typo somewhere inside
of here or somewhere up inside that switch statement.
So please double check your code make sure you don't have any typos inside of here.
It's going to save this I'll flip back over and we can once again do a test.
It's all increased red.
Yep definitely works.
Increase blue.
Yep definitely works and increased green.
Yep definitely works as well.
All right so that's pretty much it.
So we've now condensed down all of our state logic we don't have any additional callbacks or any additional
hard to read functions going on inside of here.
We have only calls to set color and then inside of set color.
We've got some well kind of challenging to read stuff but I'd say overall once you kind of get used
to the syntax you can definitely understand what's going on.
All right.
So now we've fixed all this stuff up.
Let's take a quick pause right here.
There is technically one more little optimization we could make to this function so we'll take another
pause when we come back the next video.
We're going to take a look at one last way that we can fix all this stuff up just a little bit more
than it is already.
So I'll see you in just a minute.