Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:04,490 --> 00:00:10,970
So what is this law of Demeter or as I'm choosing to call it, it's the guideline, it's a meat because
2
00:00:10,970 --> 00:00:14,990
I don't think it's something that we want to adhere to, necessarily to strictly.
3
00:00:14,990 --> 00:00:19,690
It needs a little bit of caution when applying it now.
4
00:00:19,700 --> 00:00:26,060
Typically, it's referred to as the Dot's rule or the multiple Dot's rule or the two dots rule.
5
00:00:26,300 --> 00:00:34,340
And the idea is that if you see a line like this, you're almost certainly in violation of the law.
6
00:00:35,180 --> 00:00:37,160
And let's have a look at this.
7
00:00:37,160 --> 00:00:39,290
Why is this a bad thing?
8
00:00:39,650 --> 00:00:41,660
Because we've got a class.
9
00:00:41,660 --> 00:00:44,360
We're in some class that has an enemy variable.
10
00:00:44,360 --> 00:00:47,660
Maybe that was something passed in as an argument to a function.
11
00:00:48,350 --> 00:00:51,010
So we have a dependency on enemy in this class.
12
00:00:51,020 --> 00:00:51,650
That's fine.
13
00:00:51,650 --> 00:00:53,900
We know about the interface of enemy.
14
00:00:53,900 --> 00:00:57,140
So doing against current target on enemy should be fine.
15
00:00:57,260 --> 00:01:03,200
That's a publicly exposed variable where a depending upon its public interface that is OK, however,
16
00:01:03,200 --> 00:01:03,860
was not okay.
17
00:01:03,860 --> 00:01:04,060
Is it?
18
00:01:04,069 --> 00:01:06,980
Then this target is of some other type.
19
00:01:06,980 --> 00:01:08,750
Maybe it's a player, for example.
20
00:01:08,750 --> 00:01:12,700
Maybe it's a character and we are now calling a function on that.
21
00:01:12,710 --> 00:01:15,170
So now we depending upon that class as well.
22
00:01:15,170 --> 00:01:18,200
And then what's more, we've done another bad thing.
23
00:01:18,260 --> 00:01:19,310
This would have been bad enough.
24
00:01:19,550 --> 00:01:26,150
We're going one step further and we're saying, well, apart from depending upon the enemy, the enemy's
25
00:01:26,150 --> 00:01:32,060
target class and the health now with depending on the health component as well to do damage to it.
26
00:01:32,510 --> 00:01:37,610
Basically, this line is bad because it means that we're depending on every link of this chain.
27
00:01:38,180 --> 00:01:43,640
If anything in this chain changes, then we have got ourselves a problem now.
28
00:01:43,640 --> 00:01:46,760
This wouldn't be so much of a problem if everything in this chain was the same.
29
00:01:46,760 --> 00:01:50,630
Sometimes in some languages, like Ruby, you'll do this a lot with strings.
30
00:01:50,630 --> 00:01:55,910
You'll have chains of string commands, a chain of array commands that each chain member of the changes
31
00:01:55,910 --> 00:01:59,960
returns another array or another string, so you're not actually increasing the number of classes you're
32
00:01:59,960 --> 00:02:00,710
depending upon.
33
00:02:01,160 --> 00:02:02,990
But this isn't the case here.
34
00:02:03,200 --> 00:02:06,170
Here we are, depending on more and more classes.
35
00:02:06,170 --> 00:02:13,190
And that's basically the reason that we have the law of diameter because we don't want to or the law
36
00:02:13,190 --> 00:02:14,990
of Demeter, as some people pronounce it differently.
37
00:02:15,170 --> 00:02:16,910
That's how I pronounce it, and it might be wrong.
38
00:02:17,510 --> 00:02:23,570
Anyway, the reason we don't want to do this is we don't want to add more class dependencies, and this
39
00:02:23,570 --> 00:02:28,430
is an easy way of accidentally adding more class dependencies and not realizing it.
40
00:02:29,300 --> 00:02:36,500
It's not the best thing, and often what we want to do is make the class we're talking to have the responsibility
41
00:02:36,500 --> 00:02:38,690
for doing more of this.
42
00:02:39,020 --> 00:02:45,110
So exposing something higher level from the enemy might be a solution, such as doing damage to the
43
00:02:45,110 --> 00:02:45,920
current target.
44
00:02:46,550 --> 00:02:50,440
So that means that the enemy will have to have more code written into it.
45
00:02:50,450 --> 00:02:54,680
Maybe the current target and the health component also will need to have more code written into them
46
00:02:55,280 --> 00:02:57,500
to avoid these multiple steps.
47
00:02:57,950 --> 00:03:04,070
But that does not mean that if we made some sort of wholesale change to the way that the target works,
48
00:03:04,370 --> 00:03:10,220
you're not going to have to go and change a whole bunch of other files that are doing lookups crazy
49
00:03:10,220 --> 00:03:11,360
look ups like this?
50
00:03:12,380 --> 00:03:20,690
So here's a challenge for you to see if you can identify what is wrong with the code on this slide.
51
00:03:21,230 --> 00:03:22,850
So what's the situation here?
52
00:03:22,850 --> 00:03:25,460
We've got ourselves a play set up.
53
00:03:25,460 --> 00:03:26,550
A has got a gun.
54
00:03:26,630 --> 00:03:29,630
The gun has got ammo and the ammo class has got a number of rounds.
55
00:03:29,630 --> 00:03:34,100
And for simplicity's sake, I've made these variables public, although you could imagine that they
56
00:03:34,100 --> 00:03:36,080
were private with getters and so on and so forth.
57
00:03:36,080 --> 00:03:39,110
But for simplicity's sake, everything's public in this hierarchy.
58
00:03:40,040 --> 00:03:46,100
Then over on the left, we've got some UI to represent the player and represent the gun and the ammo,
59
00:03:46,760 --> 00:03:49,030
and this works fairly straightforwardly.
60
00:03:49,040 --> 00:03:53,630
We've got a player UI which has some gun UI in it.
61
00:03:54,170 --> 00:03:58,900
And when we redraw the player UI, we get some information about the play.
62
00:03:58,910 --> 00:04:04,430
You can see we're getting reference to the player we are printing its health in lieu of actually updating
63
00:04:04,430 --> 00:04:04,940
some UI.
64
00:04:04,940 --> 00:04:12,230
We are printing the health and then we are calling redraw on the gun UI and the Gun UI similarly gets
65
00:04:12,230 --> 00:04:15,230
some information from the player and prints the rounds left.
66
00:04:15,590 --> 00:04:20,959
What looks wrong in this slide pulseaudio and see if you can figure it out and if you can figure out
67
00:04:20,959 --> 00:04:24,080
what's wrong, a bonus is to try and fix it.
68
00:04:25,510 --> 00:04:26,350
OK, welcome back.
69
00:04:26,380 --> 00:04:32,950
So if you've had a go at this, then you will be saying, well, this line here looks a bit fishy.
70
00:04:32,950 --> 00:04:39,070
That seems to violate the law of Dempster because our gun UI has to have a dependency on player, has
71
00:04:39,070 --> 00:04:44,500
to have a dependency on gun, has to have a dependency on ammo and it has to have a dependency on rounds
72
00:04:44,500 --> 00:04:45,010
left.
73
00:04:45,810 --> 00:04:47,320
It doesn't seem ideal.
74
00:04:47,320 --> 00:04:50,440
So what could we do to improve this situation?
75
00:04:51,010 --> 00:04:57,660
Well, it's not always as straightforward as saying, Well, let's just have the player have a get round
76
00:04:57,670 --> 00:04:58,390
left method.
77
00:04:58,390 --> 00:05:01,270
Maybe that's the wrong level of abstraction for the player.
78
00:05:01,600 --> 00:05:08,860
I think here the first place we're going wrong is by having the Gun UI get hold of the player for itself.
79
00:05:09,160 --> 00:05:13,090
The Gun UI doesn't really have to be linked to a player at all.
80
00:05:13,300 --> 00:05:17,800
It could just be passed a gun or given a gun from somewhere else.
81
00:05:18,220 --> 00:05:20,780
So let's see if we can't do a better job of this.
82
00:05:20,800 --> 00:05:24,370
So the first thing I said was over in the Gun UI.
83
00:05:24,490 --> 00:05:30,640
I think we want to not worry about getting hold of the player here in the gun UI.
84
00:05:31,120 --> 00:05:34,330
In fact, I don't even think we should know about the player at all.
85
00:05:34,330 --> 00:05:35,080
It's a gun UI.
86
00:05:35,080 --> 00:05:36,550
It's not a player gun UI.
87
00:05:37,300 --> 00:05:43,810
So let's try and reduce the number of dependencies here by passing in the actual gun type here that
88
00:05:43,810 --> 00:05:46,420
we want to redraw that we want to render.
89
00:05:46,840 --> 00:05:50,680
So now what we can do is we've simplified matters a little bit.
90
00:05:50,680 --> 00:05:55,000
We've got none of this dealing with the player or figuring out how to get hold of the player.
91
00:05:55,420 --> 00:05:59,870
And instead, we are just getting hold of the gun or ammo that rounds left.
92
00:05:59,890 --> 00:06:02,650
This is still not great, but we'll come back to that in a second.
93
00:06:03,340 --> 00:06:09,790
The player UI now would be the one responsible for when it calls can UI and does a you end up redraw
94
00:06:09,790 --> 00:06:10,240
UI.
95
00:06:10,240 --> 00:06:13,000
It would pass in the player dart gun.
96
00:06:13,000 --> 00:06:18,790
And that's fine because it has a dependency on player, has an unfortunate dependency that I don't quite
97
00:06:18,790 --> 00:06:24,340
like, but we can probably not get around at this level, which is that it knows how to find player
98
00:06:24,340 --> 00:06:24,910
as well.
99
00:06:24,970 --> 00:06:32,680
That's not ideal, but it's probably OK, and it can use obviously the public interface for player.
100
00:06:32,680 --> 00:06:37,750
It can get its health, it can get its gun and redraw the gun using the gun UI, which it's aware of.
101
00:06:37,750 --> 00:06:40,780
So this is no longer so bad.
102
00:06:40,780 --> 00:06:44,560
It's not violating the law as badly, but there is still a gun.
103
00:06:44,840 --> 00:06:45,970
Ammo dealt rounds left.
104
00:06:45,970 --> 00:06:52,390
So we've got an issue here that the Gun UI knows a little bit about the internals of the gun and how
105
00:06:52,570 --> 00:06:55,540
it storing the rounds left of ammo.
106
00:06:55,540 --> 00:07:00,550
And if we were to change how that works, it's going to affect the gun UI.
107
00:07:01,300 --> 00:07:07,450
So this is where we do the kind of simple change of just adding another in here in the gun where we
108
00:07:07,450 --> 00:07:15,370
might do something like public, which returns float an inch, which would be something like get rounds
109
00:07:15,370 --> 00:07:24,010
left on here and we could just go ahead and from here, return the ammo dot rounds left.
110
00:07:24,250 --> 00:07:29,740
And now you can see the gun had already a dependency on ammo, so it's fine for it to be getting the
111
00:07:29,740 --> 00:07:30,970
rounds left from ammo.
112
00:07:31,060 --> 00:07:36,280
It's using its public interface of its direct dependencies, not dependencies of dependencies.
113
00:07:36,520 --> 00:07:38,040
And that's the point of the law that matter.
114
00:07:38,650 --> 00:07:42,490
So now we can fix that up over in the Gun UI.
115
00:07:42,760 --> 00:07:51,370
So instead of the Gun UI, knowing about the ammo class, it just as a gun darts get rounds left, and
116
00:07:51,760 --> 00:07:55,570
that means that the Gun UI only has a dependency on gun.
117
00:07:56,290 --> 00:08:03,880
The Play UI only has a dependency on player and not any of its descendant dependencies, and the gun
118
00:08:03,880 --> 00:08:06,370
only has a dependency on its ammo and so on.
119
00:08:06,370 --> 00:08:11,260
So we've really reduced those dependencies down to the direct dependencies that are absolutely necessary
120
00:08:11,260 --> 00:08:12,970
by using the law of diameter.
121
00:08:13,360 --> 00:08:17,770
So hopefully now you'll be more confident in understanding if you ever come across a lot of damage or
122
00:08:17,770 --> 00:08:20,650
demetre and don't know what it meant in the past.
123
00:08:20,650 --> 00:08:27,400
Now you've got an idea and can see how it can help you to write better, more maintainable code with
124
00:08:27,400 --> 00:08:28,300
fewer dependencies.
125
00:08:28,720 --> 00:08:32,200
So this brings us to the close of our introductory section.
126
00:08:32,200 --> 00:08:36,260
Let's review our coding components before we dive in to design patterns.
127
00:08:36,580 --> 00:08:42,039
Number one, the code shall read like English number two, thou shalt take time to name things.
128
00:08:42,039 --> 00:08:44,830
Well, we've seen how important and hard that is.
129
00:08:45,400 --> 00:08:51,310
Thy classes shall do one thing also known as the single responsibility principle, and this is about
130
00:08:51,310 --> 00:08:52,840
consistent levels of abstraction.
131
00:08:53,260 --> 00:08:55,450
Thou shalt use as little state as possible.
132
00:08:55,450 --> 00:08:57,070
State causes bugs.
133
00:08:57,280 --> 00:08:59,560
Let's not include as much as we can.
134
00:08:59,980 --> 00:09:03,040
Thou shalt not refactor before the third repetition.
135
00:09:03,040 --> 00:09:07,360
Premature refactoring leads to complication, not simplification.
136
00:09:08,020 --> 00:09:12,730
Thou shalt not use a static and global state state is bad static and global state.
137
00:09:13,120 --> 00:09:20,590
So my classes shall be highly cohesive and they classes shall be loosely coupled, meaning that they
138
00:09:20,590 --> 00:09:24,430
communicate a lot within themselves and less outside of the.
139
00:09:24,570 --> 00:09:30,240
Sells Nashat use composition over inheritance because it's more flexible and generally more reusable.
140
00:09:30,630 --> 00:09:35,790
And thou shalt follow the Law of Death Matter to not include dependencies that we don't need.
141
00:09:36,180 --> 00:09:40,470
I look forward to seeing you in the next section where we'll get cracking with design patterns.
14509
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.