1
00:00:02,150 --> 00:00:06,160
<v Instructor>Now when working with classes and objects,</v>

2
00:00:06,160 --> 00:00:09,240
there are a couple of important rules

3
00:00:09,240 --> 00:00:11,810
and principles out there which you might have heard

4
00:00:11,810 --> 00:00:13,740
in the past already.

5
00:00:13,740 --> 00:00:18,450
And all of these rules and principles and laws exist

6
00:00:18,450 --> 00:00:22,350
to help you write more maintainable code.

7
00:00:22,350 --> 00:00:24,160
That is the core idea.

8
00:00:24,160 --> 00:00:27,380
Now, of course, as I already mentioned at the beginning

9
00:00:27,380 --> 00:00:30,990
of this course section, there is a relation

10
00:00:30,990 --> 00:00:34,690
between maintainable and extensible code and clean code,

11
00:00:34,690 --> 00:00:37,540
which primarily focuses on readability

12
00:00:37,540 --> 00:00:41,530
but therefore is also related to maintainability.

13
00:00:41,530 --> 00:00:45,690
There is a connection for sure, but there are some patterns

14
00:00:45,690 --> 00:00:49,130
and principles which are more important for a clean code

15
00:00:49,130 --> 00:00:52,930
which have direct impact on the readability of code

16
00:00:52,930 --> 00:00:56,480
whereas there are other patterns and principles which have

17
00:00:56,480 --> 00:01:00,230
less of an impact there, but which instead really help you

18
00:01:00,230 --> 00:01:03,340
with building large scale extensible,

19
00:01:03,340 --> 00:01:07,150
maintainable applications where a lot of developers

20
00:01:07,150 --> 00:01:10,620
can keep a project up and running over a long time

21
00:01:10,620 --> 00:01:14,180
and where it's easy to add new functionalities ends on

22
00:01:14,180 --> 00:01:17,940
and that is done only loosely related to clean code.

23
00:01:17,940 --> 00:01:21,390
Now, of course, here I will focus on the principles and laws

24
00:01:21,390 --> 00:01:24,810
which have a bigger impact on the cleanness of your code

25
00:01:24,810 --> 00:01:28,720
and for that, we will start with the law of demeter.

26
00:01:28,720 --> 00:01:31,070
Now, what is the law of demeter?

27
00:01:31,070 --> 00:01:33,980
It is related to code like this.

28
00:01:33,980 --> 00:01:38,220
Here in this code which could be in some method

29
00:01:38,220 --> 00:01:42,270
of some class, we are accessing the customer property

30
00:01:42,270 --> 00:01:43,490
of the class.

31
00:01:43,490 --> 00:01:47,980
On that property, the last purchase property, as it seems

32
00:01:47,980 --> 00:01:50,640
and on that last purchase property

33
00:01:50,640 --> 00:01:52,450
which seems to be another object,

34
00:01:52,450 --> 00:01:55,070
we access the date property.

35
00:01:55,070 --> 00:01:58,450
You should avoid code like this.

36
00:01:58,450 --> 00:02:03,260
Drilling deeply into other objects is something

37
00:02:03,260 --> 00:02:06,440
which is discouraged by the law of demeter.

38
00:02:06,440 --> 00:02:09,020
Now, why is it discouraged?

39
00:02:09,020 --> 00:02:11,810
The law of demeter is in the end about the principle

40
00:02:11,810 --> 00:02:16,000
of least knowledge, which means your class shouldn't rely

41
00:02:16,000 --> 00:02:21,000
too much on the internals of strangers of other objects

42
00:02:21,560 --> 00:02:24,810
you are interacting with in your code.

43
00:02:24,810 --> 00:02:28,430
In this example, a customer would be a class property,

44
00:02:28,430 --> 00:02:32,450
so interacting with it is fine and accessing last purchase

45
00:02:32,450 --> 00:02:37,060
is fine but last purchase now is kind of a stranger.

46
00:02:37,060 --> 00:02:40,220
We access it indirectly through customer,

47
00:02:40,220 --> 00:02:42,910
and we shouldn't drill deeper into that.

48
00:02:42,910 --> 00:02:44,690
Now, why shouldn't we do that?

49
00:02:44,690 --> 00:02:47,040
Well, because whenever something changes

50
00:02:47,040 --> 00:02:50,290
about the internal structure of last purchase,

51
00:02:50,290 --> 00:02:54,570
all our code in this place where we have this line of code

52
00:02:54,570 --> 00:02:56,513
needs to change as well.

53
00:02:57,523 --> 00:03:00,730
And that then makes our code harder to extend and maintain

54
00:03:00,730 --> 00:03:04,930
because simple changes can result in a lot of changes

55
00:03:04,930 --> 00:03:07,253
in a lot of places of your application.

56
00:03:08,603 --> 00:03:10,930
And this law is closely related to our concept

57
00:03:10,930 --> 00:03:14,030
of writing clean code, because for one,

58
00:03:14,030 --> 00:03:18,044
when writing clean code, we shouldn't repeat ourselves

59
00:03:18,044 --> 00:03:21,770
and we often might do that if we have statements like this.

60
00:03:21,770 --> 00:03:25,210
And in addition, statements like this are a bit harder

61
00:03:25,210 --> 00:03:29,230
to read, analyzing such lines of code simply takes longer

62
00:03:29,230 --> 00:03:31,563
than short, simple statements.

63
00:03:32,975 --> 00:03:35,060
Of course, this isn't too difficult of a statement,

64
00:03:35,060 --> 00:03:37,363
but it is also just a brief example.

65
00:03:38,720 --> 00:03:41,330
So the law of demeter, which we should therefore

66
00:03:41,330 --> 00:03:45,020
also follow to achieve clean code in the end states

67
00:03:45,020 --> 00:03:50,020
that code in a method may only access the direct internals

68
00:03:50,160 --> 00:03:53,460
which would be properties and methods of the object

69
00:03:53,460 --> 00:03:54,503
it belongs to.

70
00:03:55,406 --> 00:03:57,960
So you can access any property and method of the class

71
00:03:57,960 --> 00:04:00,973
of which this method in which you run the code is part of.

72
00:04:01,956 --> 00:04:04,140
You can access any objects, which are stored in properties

73
00:04:04,140 --> 00:04:05,833
of that object.

74
00:04:05,833 --> 00:04:07,860
So in this case, accessing customer is fine

75
00:04:07,860 --> 00:04:11,840
because it is a property of the class this code belongs to,

76
00:04:11,840 --> 00:04:14,220
as you can tell by the this keyword

77
00:04:14,220 --> 00:04:18,430
and accessing last purchase is of course also fine

78
00:04:18,430 --> 00:04:22,023
because that directly is part of that customer property.

79
00:04:23,356 --> 00:04:25,280
You may also use any objects which you receive

80
00:04:25,280 --> 00:04:28,410
as method parameters, so it's not just about properties

81
00:04:28,410 --> 00:04:30,490
of the class, like customer,

82
00:04:30,490 --> 00:04:33,500
but if customer would be received as a parameter,

83
00:04:33,500 --> 00:04:36,920
we would also be allowed to access data in it

84
00:04:36,920 --> 00:04:41,020
and of course, we also are allowed to interact with objects

85
00:04:41,020 --> 00:04:43,160
which are created in the method.

86
00:04:43,160 --> 00:04:45,630
So if we would instantiate another class,

87
00:04:45,630 --> 00:04:47,950
we can of course, also work with the API

88
00:04:47,950 --> 00:04:50,250
and properties of that class.

89
00:04:50,250 --> 00:04:53,510
That's the idea behind the law of demeter.

90
00:04:53,510 --> 00:04:57,823
And here's this example from the slide now in a

91
00:04:57,823 --> 00:04:59,710
real dummy class.

92
00:04:59,710 --> 00:05:03,170
I'm accessing date on last purchase on customer,

93
00:05:03,170 --> 00:05:07,540
which is a property and only this part here would be okay,

94
00:05:07,540 --> 00:05:11,570
because with this part, I'm directly working on the property

95
00:05:11,570 --> 00:05:15,303
and the data in the property which is part of this class.

96
00:05:16,939 --> 00:05:20,060
Date is not directly data of the customer property,

97
00:05:20,060 --> 00:05:22,070
it's part of some other data,

98
00:05:22,070 --> 00:05:24,070
which is stored in the customer property

99
00:05:24,961 --> 00:05:27,020
and therefore we have only indirect access to it,

100
00:05:27,020 --> 00:05:30,300
which is not allowed by the law of demeter.

101
00:05:31,837 --> 00:05:34,403
Now here, in this example, we are, for example

102
00:05:34,403 --> 00:05:37,310
getting this date to then call the deliver purchases

103
00:05:37,310 --> 00:05:38,533
by date method.

104
00:05:39,618 --> 00:05:40,770
And the obvious question now is

105
00:05:40,770 --> 00:05:43,273
how could we clean this code up?

106
00:05:44,141 --> 00:05:46,991
How could we change it such that we do follow

107
00:05:46,991 --> 00:05:48,670
the law of demeter?

108
00:05:48,670 --> 00:05:52,200
Well, one first simple solution could be

109
00:05:52,200 --> 00:05:56,491
that we simply go to our customer class

110
00:05:56,491 --> 00:05:58,420
so to our customer object and the class defining

111
00:05:58,420 --> 00:06:02,593
that object structure and we add a new method in there.

112
00:06:03,731 --> 00:06:05,210
Instead of this code, which for the moment

113
00:06:05,210 --> 00:06:08,370
I'm commenting out so that we still have it for reference,

114
00:06:08,370 --> 00:06:10,240
instead of this code, it would be better

115
00:06:10,240 --> 00:06:12,440
if our customer class would have

116
00:06:12,440 --> 00:06:15,853
a get last purchase date method.

117
00:06:17,655 --> 00:06:21,352
This code would be allowed because we're directly accessing

118
00:06:21,352 --> 00:06:23,480
a method in our customer property

119
00:06:23,480 --> 00:06:26,660
and now this customer object simply needs such a method

120
00:06:26,660 --> 00:06:30,280
which then internally accesses last purchase date.

121
00:06:30,280 --> 00:06:33,130
And there, this would be okay because last purchase

122
00:06:34,063 --> 00:06:37,130
of course will be a direct property or some data stored

123
00:06:37,130 --> 00:06:38,560
in the customer object,

124
00:06:38,560 --> 00:06:42,590
so then accessing date on that direct last purchase property

125
00:06:42,590 --> 00:06:46,680
would also be fine inside of that customer class.

126
00:06:46,680 --> 00:06:50,280
So this is how we could adjust our code

127
00:06:50,280 --> 00:06:52,289
in the customer class

128
00:06:52,289 --> 00:06:55,520
and then also in the delivery job class to not violate

129
00:06:55,520 --> 00:06:57,410
the law of demeter anymore.

130
00:06:57,410 --> 00:07:00,400
However, this is not a great solution

131
00:07:00,400 --> 00:07:04,560
because now we basically force our code to follow

132
00:07:04,560 --> 00:07:07,790
the law of demeter, but we still have some problems

133
00:07:07,790 --> 00:07:10,530
and we didn't really end up with that much

134
00:07:10,530 --> 00:07:12,160
of a cleaner code.

135
00:07:12,160 --> 00:07:14,360
Yes, this code is a bit easier to read now,

136
00:07:14,360 --> 00:07:17,170
so that is cleaner because it's a clear instruction

137
00:07:17,170 --> 00:07:20,560
which requires no interpretation, but of course we now just

138
00:07:20,560 --> 00:07:23,623
outsourced the problem into customer.

139
00:07:23,623 --> 00:07:25,330
And whilst we technically are not violating

140
00:07:25,330 --> 00:07:28,590
the law of demeter anymore, you'll of course quickly end up

141
00:07:28,590 --> 00:07:30,330
with a lot of helper methods,

142
00:07:30,330 --> 00:07:34,160
like get last purchase date here adjust to follow

143
00:07:34,160 --> 00:07:36,383
the law of demeter on paper.

144
00:07:37,350 --> 00:07:41,600
The better solution for solving problems like this

145
00:07:41,600 --> 00:07:45,015
and the solution, which really makes this clean,

146
00:07:45,015 --> 00:07:46,890
which is why I wanna cover this concept is that

147
00:07:46,890 --> 00:07:50,073
you should tell, don't ask in your code.

148
00:07:51,190 --> 00:07:53,770
Don't ask for information to then use it

149
00:07:53,770 --> 00:07:58,180
but instead, simply tell other classes what to do,

150
00:07:58,180 --> 00:08:00,360
if that is possible.

151
00:08:00,360 --> 00:08:04,680
For example here, instead of getting that date

152
00:08:04,680 --> 00:08:07,170
now with this helper method, just to then forward it

153
00:08:07,170 --> 00:08:11,640
to deliver purchase by date, the better solution would be

154
00:08:11,640 --> 00:08:15,683
to work on the warehouse instead of the customer class.

155
00:08:17,120 --> 00:08:22,120
So that here on this warehouse, we, for example, can call

156
00:08:24,320 --> 00:08:29,320
deliver purchase method instead of purchases by date

157
00:08:31,600 --> 00:08:35,280
and here we pass this customer last purchase

158
00:08:36,350 --> 00:08:40,110
and then it would be the job of the deliver purchase method

159
00:08:40,110 --> 00:08:42,940
to extract the data from that last purchase

160
00:08:42,940 --> 00:08:45,030
which it receives, which is required

161
00:08:45,030 --> 00:08:46,653
to deliver this purchase.

162
00:08:47,670 --> 00:08:50,470
Now we're not violating the law of demeter

163
00:08:50,470 --> 00:08:52,860
because we're only accessing the customer property

164
00:08:52,860 --> 00:08:55,540
and some data which is directly stored in that

165
00:08:55,540 --> 00:09:00,540
and in deliver purchase, we just get a purchase object

166
00:09:00,540 --> 00:09:04,480
which we also can use there to extract any data we want

167
00:09:04,480 --> 00:09:07,070
because you can work with objects

168
00:09:07,070 --> 00:09:09,430
and the data directly stored in them

169
00:09:09,430 --> 00:09:12,613
if they are received as method parameters.

170
00:09:14,265 --> 00:09:17,870
And this is now how you really would wanna solve this.

171
00:09:17,870 --> 00:09:22,140
You wanna tell your code what to do and not ask

172
00:09:22,140 --> 00:09:25,595
and that's all the why I wanted to cover this principle

173
00:09:25,595 --> 00:09:29,650
or this law here because this certainly is clean code.

174
00:09:29,650 --> 00:09:34,310
We don't have to interpret why we want that date

175
00:09:34,310 --> 00:09:36,510
and what we could be doing with it.

176
00:09:36,510 --> 00:09:41,510
Instead we, in the end, got rid of two lines of code,

177
00:09:41,634 --> 00:09:44,210
which we required before, which we had to read

178
00:09:44,210 --> 00:09:47,440
and we just got one line of code, which is 100% clear

179
00:09:47,440 --> 00:09:49,050
about what it does.

180
00:09:49,050 --> 00:09:52,240
That's the law of demeter and how you should follow it

181
00:09:52,240 --> 00:09:55,833
and that's why it matters for writing clean code.

182
00:09:56,690 --> 00:10:01,030
And as a side note and why I also wanted to include

183
00:10:01,030 --> 00:10:04,530
this in the course, which is of course not just about

184
00:10:04,530 --> 00:10:07,350
object-oriented programming and classes,

185
00:10:07,350 --> 00:10:12,350
the law of demeter actually also can be applied to code

186
00:10:12,360 --> 00:10:15,870
that uses just functions and no classes.

187
00:10:15,870 --> 00:10:18,030
I mean, of course you can't apply it like this,

188
00:10:18,030 --> 00:10:21,790
you can't apply the official law with all its definitions,

189
00:10:21,790 --> 00:10:24,060
but you can apply its logic.

190
00:10:24,060 --> 00:10:28,420
If you write code with just functions without any classes

191
00:10:28,420 --> 00:10:31,630
because you are writing code in a procedural way

192
00:10:31,630 --> 00:10:34,840
or because you use functional programming,

193
00:10:34,840 --> 00:10:37,570
if you do either of these two things,

194
00:10:37,570 --> 00:10:41,230
then you still wanna ensure that inside of your functions,

195
00:10:41,230 --> 00:10:44,980
you don't dig too deeply into received data

196
00:10:44,980 --> 00:10:49,200
unless there is kind of a data container only.

197
00:10:49,200 --> 00:10:53,110
And you also wanna tell, not ask,

198
00:10:53,110 --> 00:10:55,250
at least if it makes sense.

199
00:10:55,250 --> 00:10:59,430
You should always ask yourself whether getting some data

200
00:10:59,430 --> 00:11:02,100
to then do something with it in the next step

201
00:11:02,100 --> 00:11:05,020
is the best possible way right now

202
00:11:05,020 --> 00:11:09,150
or if you maybe just instead should tell some other part

203
00:11:09,150 --> 00:11:11,780
of your application what to do next.

204
00:11:11,780 --> 00:11:14,850
This is always something you can consider

205
00:11:14,850 --> 00:11:18,510
because telling instead of asking is always super easy

206
00:11:18,510 --> 00:11:22,930
to understand and automatically leads to shorter code

207
00:11:22,930 --> 00:11:26,680
at least in the part where you are telling.

208
00:11:26,680 --> 00:11:30,100
So the law of demeter is super important especially

209
00:11:30,100 --> 00:11:32,460
when working with classes and objects

210
00:11:32,460 --> 00:11:36,520
but also if you think about the general idea behind it

211
00:11:36,520 --> 00:11:38,093
in other kinds of code.

