In this video we're going to try to condense down our ads user marker method an ad company marker into
one single method instead.
So we're gonna first try one approach that is going to look like it's gonna work out pretty well but
it's going to have a distinct downside.
Well then finally refactor into the final good approach that we're going to stick with for this application.
And just about all the future applications we work on as well.
So to get started I'm going to find ad company marker right here and I'm just gonna come in out the
entire thing.
We're going to eventually delete this but I want to just leave the code around for right now just we
can refer back to it if we need to refer to any of the code inside there.
It's not going to focus on AD user marker instead to make this method a little bit more generic and
indicate that it will work for both a company and a user.
I'm going to first change the name to be only ad marker like so.
So here's approach number one to solving our problem.
I'm going to change the argument type right here to say that we can pass in an instance of a user or
a company.
Remember that is what that little pipe operator means.
And again that is the character right above the enter key on your keyboard.
You really have to hit shift and then like backslash to get that thing.
Now we might want to update the argument name as well really quickly.
So I got to change the argument name to something like mapping all to indicate that this is something
that could be mapped or like placed on the map as a marker and maybe that argument name does make a
lot of sense but I think it will work out for now.
OK.
So let's talk about exactly what this or operator does right here.
We said previously we took a quick glance at this we just said this means or mapping all can be or this
argument can be either an instance of a user or a company.
But what does that really do.
It's a quick diagram back over here all right.
So in this diagram we've got an instance of a company in an instance of a user and then I listed out
all the different properties that a company has and all the different properties that a user has along
with their respective types.
Anytime that we use and or operator on an argument here's what is happening behind the scenes on that
argument that we get access to inside of this method typescript is going to do a little powering down
operation to limit the number of properties that we can actually reference on maps Bill.
So anytime we use this or operator typescript is going to take a look at the two different types and
it's gonna say you can only do your first two properties on that argument if they exist in both of these
different types so typescript is going to look at company it's going to look at the property name company
name right here.
Remember that's one of the properties that company has typescript is then going to say OK.
Does user have a company name.
Nope it sure doesn't.
So we are not allowed to refer to company name off of this argument right here.
Typescript is then going to look at catch phrase does user have a catch phrase.
Nope.
OK we are not allowed to refer to that and finally it's going to go onto location and say OK does user
have a location.
Yep it does.
And is it it's type a object that has allowed to much longitude that are both numbers.
Yep sure is.
OK.
We are allowed to refer to location on that argument typescript is then going to take a look at the
user type and iterate through all of this things properties as well.
It's going to say okay.
Does company have a name.
No it doesn't.
Okay.
We are not allowed to refer to that property.
Does company have a location.
Yep it does.
Okay we're allowed to refer to that.
So the result of this or operate a right here is that typescript is going to limit the number of properties
that we can refer to on this magical thing.
And you can actually see that in action very easily by running out mapping all map of all sorry.
I got one of those these MacBook Pros that is like double typing characters right now.
Anyways if you then write out capital and in a dot it will list out all the different properties that
you are safe to refer to and so you'll notice that the only property we can refer to here is location
because that's the only commonality between user and company so that's why we are allowed to refer to
mapping all dot location dot lap down here and the same thing for the longitude as well.
But if we had to refer to say a user's name or a company's company name we would not be allowed to.
All right so this actually kind of works out pretty well for us right.
Because I don't think we really care about is a common property between user and company.
We only care about location dot lat and location dot longitude.
So this definitely does work out for us.
However I would suggest that there is still a big downside to this approach.
This code works but it's not perfect and here's why let's start to think about what would happen at
some point in time in the future.
Here's my diagram.
Here we go.
So let's think about what would happen in the future if we started to have other things that we want
to map on our map or show as a marker on the map.
Right now we just have user and company.
But what if we also had something like I don't know would be something else we want to show on a map.
How about like how about like a customization of a company maybe like a car lot you know a place that
sells cars and so we would make a new class called car lot.
Maybe we also want to show something that's like maybe a park so maybe we have a new thing called Park
dot.
Yes we've got to have a class called Park and so on for every one of these new things that we want to
show on our map.
We would have to add on an additional import statement into mapped out to us.
So I'd have to import Park I would have to import car lot and so on and for every one of these additional
things we might want to map.
We're gonna have to add in an additional or a case to our method.
So ad marker capital can be a user or a company or a park or a car lot or whatever else and I think
you get the idea.
Here so every time we want to expand this repertoire of things we can map.
We're gonna have to do a lot of adding on here and if there's eventually like 30 things we want to map
well we would have a really huge method signature.
So this is not really a great approach right now number one because it's not super scalable and number
two because it's introducing a very tight coupling between our map class and every other class we have
in our application that we want to show on the map.
It's like I said this is an solution.
It is a solution it would work but I still don't think this is the best way of going.
So let's take one more quick pause.
We can come back next video and finally go to the real solution for this problem.
So see you in just a minute.