Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:03,740 --> 00:00:07,679
when I say that this video is probably
2
00:00:06,149 --> 00:00:09,300
the most important programming video
3
00:00:07,679 --> 00:00:10,859
you're ever going to watch it's partly
4
00:00:09,300 --> 00:00:12,809
because what I'm going to tell you is
5
00:00:10,859 --> 00:00:15,570
distinctly a minority position among
6
00:00:12,808 --> 00:00:16,589
programmers probably 5% or under
7
00:00:15,570 --> 00:00:18,539
programmers will tell you that
8
00:00:16,589 --> 00:00:20,698
definitively object-oriented programming
9
00:00:18,539 --> 00:00:22,500
is just not a good idea and in fact is
10
00:00:20,699 --> 00:00:23,699
going to lead you astray maybe you'll
11
00:00:22,500 --> 00:00:25,140
have another 20-30 percent of
12
00:00:23,699 --> 00:00:26,789
programmers who will hand in Hall and
13
00:00:25,140 --> 00:00:27,990
say that it has some virtues and some
14
00:00:26,789 --> 00:00:29,640
weaknesses and it might be better
15
00:00:27,989 --> 00:00:31,198
applied to some problems than others I'm
16
00:00:29,640 --> 00:00:33,179
not telling you that I'm telling you
17
00:00:31,199 --> 00:00:34,829
definitively no object-oriented
18
00:00:33,179 --> 00:00:38,189
programming doesn't fit any problem and
19
00:00:34,829 --> 00:00:39,630
you shouldn't take it seriously this is
20
00:00:38,189 --> 00:00:41,789
almost certainly not what you were told
21
00:00:39,630 --> 00:00:43,559
in school if you attended a programming
22
00:00:41,789 --> 00:00:45,030
course in the last 15 years or you read
23
00:00:43,558 --> 00:00:46,890
most educational materials about
24
00:00:45,030 --> 00:00:48,450
programming the the pervasive default
25
00:00:46,890 --> 00:00:49,890
assumption is just well object-oriented
26
00:00:48,450 --> 00:00:52,440
programming is the right way to go and
27
00:00:49,890 --> 00:00:53,969
it's just a settled matter so I
28
00:00:52,439 --> 00:00:55,229
reiterate this is probably going to be
29
00:00:53,969 --> 00:00:56,640
the most important video you watch about
30
00:00:55,229 --> 00:00:57,750
programming because it's going to tell
31
00:00:56,640 --> 00:01:00,259
you something you're not going to get
32
00:00:57,750 --> 00:01:02,340
from a vast majority of other sources
33
00:01:00,259 --> 00:01:03,929
first off I'm going to try and make
34
00:01:02,340 --> 00:01:06,180
clear exactly what I'm complaining about
35
00:01:03,929 --> 00:01:07,349
and what I'm not complaining about and
36
00:01:06,180 --> 00:01:08,850
then I'm going to try and explain well
37
00:01:07,349 --> 00:01:10,708
what is object oriented program really
38
00:01:08,849 --> 00:01:13,078
because if we don't nail that down it's
39
00:01:10,709 --> 00:01:14,368
almost impossible to criticize and then
40
00:01:13,078 --> 00:01:16,379
I'll try and account for it well if
41
00:01:14,368 --> 00:01:17,849
object or a mean isn't good why does it
42
00:01:16,379 --> 00:01:19,949
dominate the industry that's kind of an
43
00:01:17,849 --> 00:01:21,538
important question actually and then
44
00:01:19,950 --> 00:01:22,740
I'll actually get into well why does
45
00:01:21,539 --> 00:01:24,509
object coming toward me not work what's
46
00:01:22,739 --> 00:01:25,920
bad about it and then lastly if I'm
47
00:01:24,509 --> 00:01:27,629
telling you to not program in an
48
00:01:25,920 --> 00:01:29,159
object-oriented style than what you do
49
00:01:27,629 --> 00:01:30,778
instead what is the alternative
50
00:01:29,159 --> 00:01:34,409
it's called procedural programming but
51
00:01:30,778 --> 00:01:35,759
what does that look like exactly so what
52
00:01:34,409 --> 00:01:37,679
are the problems with object-oriented
53
00:01:35,759 --> 00:01:40,560
programming well first off the problem
54
00:01:37,679 --> 00:01:41,759
is really not classes per se that is I
55
00:01:40,560 --> 00:01:43,679
think it's actually possible to program
56
00:01:41,759 --> 00:01:46,138
occasionally with classes in a way
57
00:01:43,679 --> 00:01:48,149
that's fairly benign I don't think it's
58
00:01:46,138 --> 00:01:51,209
particularly beneficial but for
59
00:01:48,149 --> 00:01:53,129
aesthetic reasons it might seem more
60
00:01:51,209 --> 00:01:55,079
pleasing to have an explicit association
61
00:01:53,129 --> 00:01:57,269
between certain functions and certain
62
00:01:55,078 --> 00:01:59,009
dip types doing this pervasively though
63
00:01:57,269 --> 00:02:00,688
as I'll make clear is a really bad idea
64
00:01:59,009 --> 00:02:03,269
that's where everything goes wrong is
65
00:02:00,688 --> 00:02:05,038
when you try and shove every function of
66
00:02:03,269 --> 00:02:06,598
your code every behavior into an
67
00:02:05,039 --> 00:02:09,629
association with the datatype that leads
68
00:02:06,599 --> 00:02:10,979
to disaster secondly I don't think the
69
00:02:09,629 --> 00:02:13,348
problem with object-oriented programming
70
00:02:10,979 --> 00:02:14,939
is about performance I recommend you
71
00:02:13,348 --> 00:02:16,590
watch this talk by Mike Acton called
72
00:02:14,938 --> 00:02:17,878
data oriented design and
73
00:02:16,590 --> 00:02:19,680
plus he makes some very interesting
74
00:02:17,878 --> 00:02:21,090
points and provides some insight into
75
00:02:19,680 --> 00:02:23,250
that role the program in which most of
76
00:02:21,090 --> 00:02:25,378
us don't do but I think he over States
77
00:02:23,250 --> 00:02:26,639
his case fine there's a lot of software
78
00:02:25,378 --> 00:02:28,739
out there that should be written with
79
00:02:26,639 --> 00:02:30,299
much more regard for a performance but I
80
00:02:28,739 --> 00:02:33,150
think there's tons of software that just
81
00:02:30,300 --> 00:02:35,250
really doesn't apply you'll also hear
82
00:02:33,150 --> 00:02:37,500
complaints about excessive abstraction
83
00:02:35,250 --> 00:02:39,628
from generally the same people people
84
00:02:37,500 --> 00:02:41,340
like Mike Acton and again here I think
85
00:02:39,628 --> 00:02:43,590
they're overstating the case I think
86
00:02:41,340 --> 00:02:46,110
abstraction is actually a worthy goal in
87
00:02:43,590 --> 00:02:47,610
practice most abstractions aren't good
88
00:02:46,110 --> 00:02:50,130
it takes a long long time to develop
89
00:02:47,610 --> 00:02:51,690
good ones and as I'll explain a major
90
00:02:50,129 --> 00:02:53,639
problem with object-oriented as it does
91
00:02:51,689 --> 00:02:55,769
tend to produce abstractions that aren't
92
00:02:53,639 --> 00:02:59,098
any good that's the real problem not the
93
00:02:55,769 --> 00:03:00,569
idea of abstraction itself another
94
00:02:59,098 --> 00:03:02,639
interesting talk to watch is one by
95
00:03:00,568 --> 00:03:05,009
Abner coimbra called what programming is
96
00:03:02,639 --> 00:03:06,899
never about and the thing which he says
97
00:03:05,009 --> 00:03:09,899
programming is never about is code
98
00:03:06,900 --> 00:03:11,610
prettiness how code looks aesthetics his
99
00:03:09,900 --> 00:03:13,709
main point is that programmers typically
100
00:03:11,610 --> 00:03:15,420
focus too much on surface concerns about
101
00:03:13,709 --> 00:03:17,610
their code rather than stuff that really
102
00:03:15,419 --> 00:03:19,859
matters I think though he actually
103
00:03:17,610 --> 00:03:22,620
simply misstates his case or or rather
104
00:03:19,860 --> 00:03:24,780
his thesis doesn't really follow from
105
00:03:22,620 --> 00:03:26,789
his arguments which are generally valid
106
00:03:24,780 --> 00:03:28,310
I think when really pressed he would
107
00:03:26,789 --> 00:03:29,939
admit that elegance simplicity
108
00:03:28,310 --> 00:03:31,799
flexibility readability and
109
00:03:29,939 --> 00:03:33,150
maintainability structure all these
110
00:03:31,799 --> 00:03:34,980
things you might file under code
111
00:03:33,150 --> 00:03:37,409
aesthetics I think you admit actually do
112
00:03:34,979 --> 00:03:39,659
matter but I think the more accurate way
113
00:03:37,409 --> 00:03:41,579
to spend his point is that these surface
114
00:03:39,659 --> 00:03:44,219
level virtues of code are good things
115
00:03:41,579 --> 00:03:45,840
and actually important but object doing
116
00:03:44,219 --> 00:03:47,519
and programming and abstraction heavy
117
00:03:45,840 --> 00:03:48,900
programming in general fails to deliver
118
00:03:47,519 --> 00:03:51,359
them in fact it provides just the
119
00:03:48,900 --> 00:03:53,189
illusion of these things object-oriented
120
00:03:51,359 --> 00:03:54,599
programming is sold on the basis that it
121
00:03:53,189 --> 00:03:56,609
supposedly provides these things but
122
00:03:54,598 --> 00:04:00,839
particularly simplicity and elegance it
123
00:03:56,609 --> 00:04:02,280
actually makes things worse lastly be
124
00:04:00,840 --> 00:04:04,379
clear that I'm pushing procedural
125
00:04:02,280 --> 00:04:05,969
programming not necessarily functional
126
00:04:04,379 --> 00:04:07,560
programming which is a different thing
127
00:04:05,969 --> 00:04:09,389
as I will make clear in a moment I
128
00:04:07,560 --> 00:04:10,949
happen to think that functional
129
00:04:09,389 --> 00:04:12,930
programming actually is the future of
130
00:04:10,949 --> 00:04:14,729
higher level code I think it may
131
00:04:12,930 --> 00:04:16,858
actually be the default way we program
132
00:04:14,729 --> 00:04:17,459
at a higher level in ten years from now
133
00:04:16,858 --> 00:04:19,500
or something
134
00:04:17,459 --> 00:04:21,000
but there are serious efficiency
135
00:04:19,500 --> 00:04:22,259
problems that make functional
136
00:04:21,000 --> 00:04:24,000
programming not really viable in certain
137
00:04:22,259 --> 00:04:26,069
domains of programming and so my message
138
00:04:24,000 --> 00:04:28,228
is whether your code ends up functional
139
00:04:26,069 --> 00:04:29,079
or imperative that's a separate matter
140
00:04:28,228 --> 00:04:30,639
we're go
141
00:04:29,079 --> 00:04:34,269
allas your code should be procedural
142
00:04:30,639 --> 00:04:36,550
rather than object-oriented so it's a
143
00:04:34,269 --> 00:04:38,288
good time now to make clear exactly what
144
00:04:36,550 --> 00:04:39,250
are the competing paradigms of
145
00:04:38,288 --> 00:04:41,288
programming that we're really talking
146
00:04:39,250 --> 00:04:44,319
about there are four main possibilities
147
00:04:41,288 --> 00:04:46,598
your code confers to be both procedural
148
00:04:44,319 --> 00:04:48,460
and imperative procedural meaning that
149
00:04:46,598 --> 00:04:50,709
you have no explicit association between
150
00:04:48,459 --> 00:04:53,198
your datatypes and your functions your
151
00:04:50,709 --> 00:04:55,388
behaviors an imperative meaning that we
152
00:04:53,199 --> 00:04:57,460
just mutate state whenever we feel like
153
00:04:55,389 --> 00:05:00,069
it we don't have any special handling of
154
00:04:57,459 --> 00:05:01,810
shared State which can cause problems as
155
00:05:00,069 --> 00:05:04,180
your code gets larger and larger and
156
00:05:01,810 --> 00:05:05,949
more complex but in procedural and
157
00:05:04,180 --> 00:05:08,288
imperative programming we just cope with
158
00:05:05,949 --> 00:05:09,669
the problems as they arise and you can
159
00:05:08,288 --> 00:05:11,529
think of this style of programming as
160
00:05:09,668 --> 00:05:14,500
being basically the default it's the the
161
00:05:11,529 --> 00:05:16,059
obvious way to get work done so this is
162
00:05:14,500 --> 00:05:18,009
really how all programming was done in
163
00:05:16,060 --> 00:05:19,959
the early days of computers but then
164
00:05:18,009 --> 00:05:21,759
starting in the 60s as programs got more
165
00:05:19,959 --> 00:05:23,109
more complicated people began thinking
166
00:05:21,759 --> 00:05:25,569
about well how do we solve this problem
167
00:05:23,110 --> 00:05:27,848
of shared state because it really can
168
00:05:25,569 --> 00:05:29,468
get out of hand and so we got two major
169
00:05:27,848 --> 00:05:31,899
prescriptions on how to handle the
170
00:05:29,468 --> 00:05:33,699
problem one of these prescriptions says
171
00:05:31,899 --> 00:05:36,278
that our code should be procedural yet
172
00:05:33,699 --> 00:05:37,810
functional meaning that all or most of
173
00:05:36,278 --> 00:05:40,120
the functions that make up our code
174
00:05:37,810 --> 00:05:42,310
should be pure they should not deal with
175
00:05:40,120 --> 00:05:43,778
state and so programming in the style we
176
00:05:42,310 --> 00:05:45,668
would tackle the problem of shared state
177
00:05:43,778 --> 00:05:47,218
by minimizing state trying to get rid of
178
00:05:45,668 --> 00:05:49,359
as much of it as possible
179
00:05:47,218 --> 00:05:50,939
the other prescription people came up
180
00:05:49,360 --> 00:05:53,560
with said that our code should be
181
00:05:50,939 --> 00:05:55,209
object-oriented and imperative and the
182
00:05:53,560 --> 00:05:57,129
strategy here is that we simply
183
00:05:55,209 --> 00:05:58,989
segregate our state we take the state
184
00:05:57,129 --> 00:06:01,210
that makes up a program and instead of
185
00:05:58,990 --> 00:06:02,468
sharing it promiscuously we try and
186
00:06:01,209 --> 00:06:04,810
divide and conquer the problem we
187
00:06:02,468 --> 00:06:06,968
package it into these encapsulated units
188
00:06:04,810 --> 00:06:08,500
that we call objects and objects contain
189
00:06:06,968 --> 00:06:11,528
other objects and so forth and that's
190
00:06:08,500 --> 00:06:13,389
how we conquer the problem and these two
191
00:06:11,528 --> 00:06:15,158
prescriptions are actually orthogonal to
192
00:06:13,389 --> 00:06:16,629
each other we can do both the functional
193
00:06:15,158 --> 00:06:18,338
business to minimize the amount of state
194
00:06:16,629 --> 00:06:20,349
which our program deals with and then
195
00:06:18,338 --> 00:06:22,180
whatever state is left over we can then
196
00:06:20,348 --> 00:06:24,668
segregate into separate units of
197
00:06:22,180 --> 00:06:26,500
encapsulation and in fact I think this
198
00:06:24,668 --> 00:06:28,658
combination approach may actually be the
199
00:06:26,500 --> 00:06:30,370
ideal way to structure programs at least
200
00:06:28,658 --> 00:06:32,680
in terms of high-level code where we
201
00:06:30,370 --> 00:06:34,449
don't care so much about efficiency as
202
00:06:32,680 --> 00:06:37,389
I'll explain I think segregating state
203
00:06:34,449 --> 00:06:39,759
is actually a valid strategy up to a
204
00:06:37,389 --> 00:06:42,129
certain level of detail certain level of
205
00:06:39,759 --> 00:06:42,759
complexity and so if we first minimize
206
00:06:42,129 --> 00:06:44,439
the amount of
207
00:06:42,759 --> 00:06:46,990
a twitch our code deals with it then
208
00:06:44,439 --> 00:06:50,500
becomes a viable strategy to segregate
209
00:06:46,990 --> 00:06:52,689
the Romanian state you may have noticed
210
00:06:50,500 --> 00:06:54,250
in my definition of object-oriented I
211
00:06:52,689 --> 00:06:56,050
said nothing about inheritance and that
212
00:06:54,250 --> 00:06:57,699
is because inheritance is simply
213
00:06:56,050 --> 00:06:59,829
irrelevant no one defends it anymore
214
00:06:57,699 --> 00:07:00,699
even people who advocate for
215
00:06:59,829 --> 00:07:02,409
object-oriented
216
00:07:00,699 --> 00:07:04,060
well very very commonly these days tell
217
00:07:02,410 --> 00:07:05,920
you to be very very careful in using
218
00:07:04,060 --> 00:07:07,959
inheritance or maybe not to use it at
219
00:07:05,920 --> 00:07:09,040
all and so it's not really pertinent to
220
00:07:07,959 --> 00:07:10,899
any argument about whether
221
00:07:09,040 --> 00:07:13,569
object-oriented programming is good or
222
00:07:10,899 --> 00:07:14,859
bad for similar reasons I didn't say
223
00:07:13,569 --> 00:07:16,899
anything about polymorphism in my
224
00:07:14,860 --> 00:07:18,699
definition because polymorphism really
225
00:07:16,899 --> 00:07:20,439
good or bad isn't exclusive to
226
00:07:18,699 --> 00:07:22,209
object-oriented programming you can have
227
00:07:20,439 --> 00:07:24,069
procedural code that is polymorphic and
228
00:07:22,209 --> 00:07:25,539
in fact even more polymorphic than is
229
00:07:24,069 --> 00:07:27,819
typically available in most
230
00:07:25,540 --> 00:07:29,230
object-oriented languages so it's really
231
00:07:27,819 --> 00:07:30,550
not part of the discussion as far as I'm
232
00:07:29,230 --> 00:07:32,259
concerned when I complained about
233
00:07:30,550 --> 00:07:35,009
object-oriented programming I'm really
234
00:07:32,259 --> 00:07:37,539
complaining about one idea encapsulation
235
00:07:35,009 --> 00:07:39,459
encapsulation does not work or as I
236
00:07:37,540 --> 00:07:41,439
should qualify this encapsulation does
237
00:07:39,459 --> 00:07:43,060
not work at a fine-grained level which
238
00:07:41,439 --> 00:07:45,279
is the core of what object-oriented
239
00:07:43,060 --> 00:07:47,050
ideology prescribes that we need to take
240
00:07:45,279 --> 00:07:48,909
the state of our programs and divide and
241
00:07:47,050 --> 00:07:51,220
conquer that problem by chopping it up
242
00:07:48,910 --> 00:07:53,230
into tiny little pieces that is the
243
00:07:51,220 --> 00:07:58,060
nature of object-oriented code and it
244
00:07:53,230 --> 00:07:59,560
doesn't work it leads to madness before
245
00:07:58,060 --> 00:08:01,480
delving into why object-oriented
246
00:07:59,560 --> 00:08:03,060
programming doesn't work it is important
247
00:08:01,480 --> 00:08:04,990
to address this mystery of well if
248
00:08:03,060 --> 00:08:06,610
object-oriented isn't so great
249
00:08:04,990 --> 00:08:08,470
why does it now dominate the industry
250
00:08:06,610 --> 00:08:10,629
and why is it done so for almost last
251
00:08:08,470 --> 00:08:12,430
twenty years I've heard it sometimes
252
00:08:10,629 --> 00:08:14,379
suggested that well this was an
253
00:08:12,430 --> 00:08:16,629
imposition of management management
254
00:08:14,379 --> 00:08:18,310
wants interchangeable developers so that
255
00:08:16,629 --> 00:08:20,649
we can have a cookie cutter assembly
256
00:08:18,310 --> 00:08:22,089
line development process hence business
257
00:08:20,649 --> 00:08:23,979
types were really enthusiastic about
258
00:08:22,089 --> 00:08:25,919
object oriented programming promises
259
00:08:23,980 --> 00:08:28,540
about code reusability and
260
00:08:25,920 --> 00:08:30,310
compartmentalization it's a theory that
261
00:08:28,540 --> 00:08:31,870
sounds plausible to me but the main
262
00:08:30,310 --> 00:08:33,610
sticking point is that object-oriented
263
00:08:31,870 --> 00:08:35,500
programming doesn't actually deliver
264
00:08:33,610 --> 00:08:37,330
these promises you'd think people would
265
00:08:35,500 --> 00:08:39,360
have noticed sometime in the last 20
266
00:08:37,330 --> 00:08:41,770
years yet they seem not to have noticed
267
00:08:39,360 --> 00:08:43,210
I'm also skeptical of the idea that
268
00:08:41,769 --> 00:08:44,649
management actually really inserted
269
00:08:43,210 --> 00:08:46,750
themselves in these technical decisions
270
00:08:44,649 --> 00:08:47,939
that often I suppose once a
271
00:08:46,750 --> 00:08:50,679
object-oriented
272
00:08:47,940 --> 00:08:51,550
and that became the pervasive norm then
273
00:08:50,679 --> 00:08:53,529
yeah sure
274
00:08:51,549 --> 00:08:55,389
management would push towards doing what
275
00:08:53,529 --> 00:08:56,259
everyone else is doing so that they can
276
00:08:55,389 --> 00:08:58,899
draw from the
277
00:08:56,259 --> 00:09:00,519
larger talent pool but otherwise aside
278
00:08:58,899 --> 00:09:02,409
from pushing engineers to just go along
279
00:09:00,519 --> 00:09:04,059
with the legacy system and not rebuild
280
00:09:02,409 --> 00:09:05,740
everything I just don't think that many
281
00:09:04,059 --> 00:09:08,529
business managers really care that much
282
00:09:05,740 --> 00:09:09,820
about technical decisions I'm much more
283
00:09:08,529 --> 00:09:10,720
inclined to think that object-oriented
284
00:09:09,820 --> 00:09:12,550
programming is something that
285
00:09:10,720 --> 00:09:15,430
programmers did to themselves and the
286
00:09:12,549 --> 00:09:16,779
question is then well why I think a big
287
00:09:15,429 --> 00:09:19,479
part of the answer simply comes down to
288
00:09:16,779 --> 00:09:21,429
Java when it was first introduced in the
289
00:09:19,480 --> 00:09:23,560
mid-90s Java seemed like a welcome
290
00:09:21,429 --> 00:09:25,120
reprieve to many programmers compared to
291
00:09:23,559 --> 00:09:27,759
the alternatives Java seemed really
292
00:09:25,120 --> 00:09:29,169
simple for example on the PC this is
293
00:09:27,759 --> 00:09:31,389
what application development looked like
294
00:09:29,169 --> 00:09:33,159
you had to use the win32 api and see and
295
00:09:31,389 --> 00:09:34,269
not only that programmers have to
296
00:09:33,159 --> 00:09:36,100
concern themselves with memory
297
00:09:34,269 --> 00:09:38,230
management as you do and see but on top
298
00:09:36,100 --> 00:09:39,519
of that win32 just doesn't feel like the
299
00:09:38,230 --> 00:09:41,320
C that you would learn from books that's
300
00:09:39,519 --> 00:09:42,939
not what you would learn from knr it's
301
00:09:41,320 --> 00:09:45,370
not what you would learn in school it's
302
00:09:42,940 --> 00:09:48,160
all this excess and macro heavy stuff on
303
00:09:45,370 --> 00:09:49,929
top that is really mystifying even the
304
00:09:48,159 --> 00:09:51,309
tools you would use to write C programs
305
00:09:49,929 --> 00:09:53,019
on a Windows platform the the visual
306
00:09:51,309 --> 00:09:54,209
studio tools you know wouldn't be the
307
00:09:53,019 --> 00:09:55,929
same as what you would learn in
308
00:09:54,210 --> 00:09:57,190
university probably where you probably
309
00:09:55,929 --> 00:09:59,079
had a UNIX system and that's what you
310
00:09:57,190 --> 00:10:01,480
learned so it was already this platform
311
00:09:59,080 --> 00:10:02,680
with a quite high barrier to entry but
312
00:10:01,480 --> 00:10:04,659
then also in this period it was
313
00:10:02,679 --> 00:10:07,839
undergoing this ugly transition from win
314
00:10:04,659 --> 00:10:09,009
16 to win 32 and so you can begin to see
315
00:10:07,840 --> 00:10:11,200
why programmers were desperately looking
316
00:10:09,009 --> 00:10:12,730
for some way out the only real
317
00:10:11,200 --> 00:10:14,740
alternative at the time in PC
318
00:10:12,730 --> 00:10:16,300
programming was what Visual Basic but
319
00:10:14,740 --> 00:10:17,680
that was another effective Microsoft
320
00:10:16,299 --> 00:10:19,419
platform you're locking yourself in too
321
00:10:17,679 --> 00:10:21,189
and I suppose otherwise you might use
322
00:10:19,419 --> 00:10:23,049
peskow or Delfy but that platform had
323
00:10:21,190 --> 00:10:24,220
its own issues and so it shouldn't be
324
00:10:23,049 --> 00:10:25,779
too surprising that when Sun
325
00:10:24,220 --> 00:10:28,210
Microsystems came along and said here's
326
00:10:25,779 --> 00:10:30,759
this free thing that everyone can use
327
00:10:28,210 --> 00:10:33,070
across all platforms that got people's
328
00:10:30,759 --> 00:10:34,750
attention and Java had other things
329
00:10:33,070 --> 00:10:36,490
going for it it certainly seemed more
330
00:10:34,750 --> 00:10:38,230
accessible just in terms of like its
331
00:10:36,490 --> 00:10:40,269
naming conventions for example you look
332
00:10:38,230 --> 00:10:42,399
at the Java API s and you see things
333
00:10:40,269 --> 00:10:44,710
like file input stream which is not
334
00:10:42,399 --> 00:10:46,659
cryptic at all yes there are definitely
335
00:10:44,710 --> 00:10:49,210
issues and how abstracted many of the
336
00:10:46,659 --> 00:10:51,219
api's are and you know having to derive
337
00:10:49,210 --> 00:10:53,860
base classes to use the API as and all
338
00:10:51,220 --> 00:10:56,110
that nonsense but on first glance on
339
00:10:53,860 --> 00:10:58,240
surface inspection it certainly seems
340
00:10:56,110 --> 00:11:00,279
like a friendlier system it's not like
341
00:10:58,240 --> 00:11:01,870
UNIX where you have stuff like ioctl
342
00:11:00,279 --> 00:11:04,299
which is you're supposed to know is
343
00:11:01,870 --> 00:11:06,250
input/output control and other really
344
00:11:04,299 --> 00:11:08,500
horrible abbreviations and then when 32
345
00:11:06,250 --> 00:11:09,389
had the same thing you know LPC TST RS
346
00:11:08,500 --> 00:11:12,120
stands for
347
00:11:09,389 --> 00:11:14,490
Oh what's long pointer to a Const teach
348
00:11:12,120 --> 00:11:16,589
our string so even if you know what a t
349
00:11:14,490 --> 00:11:18,028
char is and a long pointer is you're
350
00:11:16,589 --> 00:11:19,980
stuck in this world where everything is
351
00:11:18,028 --> 00:11:21,360
cryptically abbreviated and it's just as
352
00:11:19,980 --> 00:11:22,230
goddamn puzzled that you have to figure
353
00:11:21,360 --> 00:11:24,329
out at every step
354
00:11:22,230 --> 00:11:25,800
Java came in and said no we don't
355
00:11:24,328 --> 00:11:27,838
necessarily have to program that way we
356
00:11:25,799 --> 00:11:29,490
can write real programs that don't have
357
00:11:27,839 --> 00:11:31,500
to be horribly cryptic in that way and
358
00:11:29,490 --> 00:11:32,940
then Java took things too far in the
359
00:11:31,500 --> 00:11:36,839
other direction but that's again we'll
360
00:11:32,940 --> 00:11:39,060
get to that Java also smartly had the C
361
00:11:36,839 --> 00:11:40,380
like syntax the curly brace syntax so
362
00:11:39,059 --> 00:11:42,359
superficially at least it seemed
363
00:11:40,379 --> 00:11:44,759
familiar to programmers from C and C++
364
00:11:42,360 --> 00:11:47,159
and it seemed like real programming it
365
00:11:44,759 --> 00:11:48,958
has curly braces after all and then the
366
00:11:47,159 --> 00:11:50,909
whole compilation to VM bike code
367
00:11:48,958 --> 00:11:52,409
business was again very alluring to
368
00:11:50,909 --> 00:11:54,269
programmers trying to escape their
369
00:11:52,409 --> 00:11:56,189
platform headaches and then Java also
370
00:11:54,269 --> 00:11:58,110
offered some very basic niceties like
371
00:11:56,190 --> 00:11:59,910
proper name spaces without header files
372
00:11:58,110 --> 00:12:01,589
for Christ's sake we still have to deal
373
00:11:59,909 --> 00:12:04,559
with header files to do our real
374
00:12:01,589 --> 00:12:05,820
programming in C and C++ at least 20
375
00:12:04,559 --> 00:12:07,979
years after we should have ditched them
376
00:12:05,820 --> 00:12:09,750
if for this one thing alone I think it's
377
00:12:07,980 --> 00:12:11,550
worth giving Java some credit it
378
00:12:09,750 --> 00:12:14,278
mainstream programming without header
379
00:12:11,549 --> 00:12:16,649
files and then of course also very
380
00:12:14,278 --> 00:12:17,909
alluring garbage collection I know some
381
00:12:16,649 --> 00:12:19,470
hard core low-level programmers out
382
00:12:17,909 --> 00:12:21,448
there will insist the garbage collection
383
00:12:19,470 --> 00:12:23,490
is never necessary it's never a good
384
00:12:21,448 --> 00:12:25,289
idea but whether or not that's the case
385
00:12:23,490 --> 00:12:27,899
it's really hard to argue with the
386
00:12:25,289 --> 00:12:29,490
appeal it shouldn't be surprising that
387
00:12:27,899 --> 00:12:31,289
the vast armies of people doing business
388
00:12:29,490 --> 00:12:34,528
crud applications wanted to stop
389
00:12:31,289 --> 00:12:36,299
thinking about memory management Java
390
00:12:34,528 --> 00:12:37,559
also mainstreamed exceptions as the
391
00:12:36,299 --> 00:12:39,328
primary way to handle errors and
392
00:12:37,559 --> 00:12:41,309
whatever problems this may have in
393
00:12:39,328 --> 00:12:43,169
practice I think it definitely seems
394
00:12:41,309 --> 00:12:45,239
appealing because the alternative is
395
00:12:43,169 --> 00:12:48,299
ugly the alternative is what we do in C
396
00:12:45,240 --> 00:12:50,669
and C++ of having to have an inbound err
397
00:12:48,299 --> 00:12:52,198
return value or likes you know saving to
398
00:12:50,669 --> 00:12:54,588
global and checking the global after
399
00:12:52,198 --> 00:12:56,458
everything you call it's not pretty
400
00:12:54,589 --> 00:12:59,040
going with a little return and that
401
00:12:56,458 --> 00:13:00,750
style probably the better way to go but
402
00:12:59,039 --> 00:13:02,250
that's not the solution Java came up
403
00:13:00,750 --> 00:13:05,039
with and so normalize this other thing
404
00:13:02,250 --> 00:13:06,629
that seemed better at the time I think
405
00:13:05,039 --> 00:13:09,059
some people also came to like the
406
00:13:06,629 --> 00:13:10,528
subject-verb-object nature of method
407
00:13:09,059 --> 00:13:12,659
calls over straight function calls
408
00:13:10,528 --> 00:13:14,909
because well this is just what we do in
409
00:13:12,659 --> 00:13:17,250
English its subject first then verb than
410
00:13:14,909 --> 00:13:17,730
object I myself don't find it all that
411
00:13:17,250 --> 00:13:19,889
appealing
412
00:13:17,730 --> 00:13:21,240
I prefer consistency and I think the
413
00:13:19,889 --> 00:13:23,129
distinction between subject and object
414
00:13:21,240 --> 00:13:24,509
in many many cases gets very very
415
00:13:23,129 --> 00:13:25,710
which is one of the problems with
416
00:13:24,509 --> 00:13:28,500
object-oriented programming as we'll get
417
00:13:25,710 --> 00:13:30,300
to but the style of syntax in Java led
418
00:13:28,500 --> 00:13:32,159
to this convenience people I think since
419
00:13:30,299 --> 00:13:34,620
then have become addicted to which is in
420
00:13:32,159 --> 00:13:36,509
their IDs it offers them for this data
421
00:13:34,620 --> 00:13:38,490
type what are my options what can I do
422
00:13:36,509 --> 00:13:39,629
with this thing it seems to enable
423
00:13:38,490 --> 00:13:41,820
Stella programming where you can just
424
00:13:39,629 --> 00:13:43,500
sort of browse you don't have to hold
425
00:13:41,820 --> 00:13:45,360
all the options in your head you just
426
00:13:43,500 --> 00:13:46,620
have a vague notion of way I'm going to
427
00:13:45,360 --> 00:13:48,149
take that thing and transform into this
428
00:13:46,620 --> 00:13:49,560
other thing I don't remember exactly
429
00:13:48,149 --> 00:13:51,870
what the methods called we'll just grope
430
00:13:49,559 --> 00:13:54,359
my way there using Auto completion in my
431
00:13:51,870 --> 00:13:55,799
IDE again there's really actually no
432
00:13:54,360 --> 00:13:57,269
reason you couldn't have the same style
433
00:13:55,799 --> 00:13:58,859
of convenience in a purely procedural
434
00:13:57,269 --> 00:14:01,379
language you just have an auto
435
00:13:58,860 --> 00:14:04,259
completion for given this first argument
436
00:14:01,379 --> 00:14:06,120
what functions take this type as its
437
00:14:04,259 --> 00:14:07,889
first argument and a being effectively
438
00:14:06,120 --> 00:14:10,230
the same thing really but because of
439
00:14:07,889 --> 00:14:12,029
quirks of history and syntax design this
440
00:14:10,230 --> 00:14:14,100
particular editing convenience has been
441
00:14:12,029 --> 00:14:15,449
implemented for languages like Java but
442
00:14:14,100 --> 00:14:17,550
generally not straight procedural
443
00:14:15,450 --> 00:14:19,530
languages and I think method
444
00:14:17,549 --> 00:14:21,719
auto-completion may actually largely
445
00:14:19,529 --> 00:14:24,179
explain why people sometimes claim that
446
00:14:21,720 --> 00:14:25,830
object oriented API is feel easier to
447
00:14:24,179 --> 00:14:27,689
use it's because you can largely
448
00:14:25,830 --> 00:14:31,020
autocomplete your way through most of
449
00:14:27,690 --> 00:14:32,940
the usage another thing Java is seem to
450
00:14:31,019 --> 00:14:34,919
have going for it is that back in the
451
00:14:32,940 --> 00:14:37,500
mid-90s this was the heyday of GUI
452
00:14:34,919 --> 00:14:40,949
programming and it seemed really logical
453
00:14:37,500 --> 00:14:43,259
to map components as we see them in a
454
00:14:40,950 --> 00:14:45,060
GUI window and classes in an
455
00:14:43,259 --> 00:14:47,669
object-oriented program that seemed like
456
00:14:45,059 --> 00:14:49,619
a very natural correspondence this was
457
00:14:47,669 --> 00:14:51,750
the most tangible version of the real
458
00:14:49,620 --> 00:14:54,179
world modeling which object-oriented
459
00:14:51,750 --> 00:14:56,460
promised at the time it seemed like a
460
00:14:54,179 --> 00:14:57,899
very plausible story and on top of that
461
00:14:56,460 --> 00:15:00,480
you have the virtue of Java Bean
462
00:14:57,899 --> 00:15:02,340
supposedly cross-platform with the Java
463
00:15:00,480 --> 00:15:03,990
swing API so you can write gooeys that
464
00:15:02,340 --> 00:15:05,639
will run on any system they'll look
465
00:15:03,990 --> 00:15:08,009
horribly ugly but at least hey they run
466
00:15:05,639 --> 00:15:10,439
on everything you could do so-called rad
467
00:15:08,009 --> 00:15:11,990
rapid application development of GUI
468
00:15:10,440 --> 00:15:14,190
applications like you do in Visual Basic
469
00:15:11,990 --> 00:15:18,090
except in Java you're not locked into
470
00:15:14,190 --> 00:15:19,830
Microsoft's platform so the funny thing
471
00:15:18,090 --> 00:15:21,330
to me about Java is that I think in an
472
00:15:19,830 --> 00:15:22,920
ultimate history it could have had
473
00:15:21,330 --> 00:15:24,480
virtually all the same success if not
474
00:15:22,919 --> 00:15:26,490
even more perhaps if it weren't
475
00:15:24,480 --> 00:15:27,990
object-oriented at all it could have
476
00:15:26,490 --> 00:15:29,879
just been a straight procedural language
477
00:15:27,990 --> 00:15:31,950
and would have had still a big long list
478
00:15:29,879 --> 00:15:33,750
of attractive selling points we could
479
00:15:31,950 --> 00:15:35,490
have had all the same portability the
480
00:15:33,750 --> 00:15:37,649
same garbage collection
481
00:15:35,490 --> 00:15:39,839
same exception handling and so forth
482
00:15:37,649 --> 00:15:41,490
down the line without any of the object
483
00:15:39,839 --> 00:15:43,050
oriented miss or at the very least
484
00:15:41,490 --> 00:15:44,250
without forcing everything into the mold
485
00:15:43,049 --> 00:15:45,719
of classes you could have it of a
486
00:15:44,250 --> 00:15:47,190
language like Python say where there are
487
00:15:45,720 --> 00:15:48,839
classes but also just straight
488
00:15:47,190 --> 00:15:52,200
procedural code if you want and they can
489
00:15:48,839 --> 00:15:54,120
live side by side just fine so this
490
00:15:52,200 --> 00:15:55,560
still is this question Java aside there
491
00:15:54,120 --> 00:15:57,179
seems to be some appeal to object
492
00:15:55,559 --> 00:15:59,729
oriented programming in itself inand
493
00:15:57,179 --> 00:16:01,679
what is that well I think very simply if
494
00:15:59,730 --> 00:16:02,909
you go back to the 60s and 70s as people
495
00:16:01,679 --> 00:16:04,379
were grappling with the problems of
496
00:16:02,909 --> 00:16:06,838
software systems getting larger and
497
00:16:04,379 --> 00:16:08,850
larger people tried to identify units of
498
00:16:06,839 --> 00:16:11,760
code abstraction that were larger than
499
00:16:08,850 --> 00:16:13,440
individual functions and data types it's
500
00:16:11,759 --> 00:16:15,028
natural to want to describe any complex
501
00:16:13,440 --> 00:16:16,500
system in terms of large scale
502
00:16:15,028 --> 00:16:18,509
components you know if you talk about
503
00:16:16,500 --> 00:16:20,309
human anatomy that you don't explain it
504
00:16:18,509 --> 00:16:22,200
first in terms of microbiology that
505
00:16:20,309 --> 00:16:23,669
would be nuts we first talked about very
506
00:16:22,200 --> 00:16:26,270
major organs like the brain and the
507
00:16:23,669 --> 00:16:28,889
heart and the kidneys and so forth as
508
00:16:26,269 --> 00:16:30,208
software gets larger and larger it felt
509
00:16:28,889 --> 00:16:31,980
like these units of code we were
510
00:16:30,208 --> 00:16:33,479
building out of the base materials these
511
00:16:31,980 --> 00:16:35,370
data structures and functions they
512
00:16:33,480 --> 00:16:38,490
became smaller and smaller relative to
513
00:16:35,370 --> 00:16:40,409
the whole sadly though the one general
514
00:16:38,490 --> 00:16:42,000
answer people could come up with of what
515
00:16:40,409 --> 00:16:43,708
is a unit of code abstraction bigger
516
00:16:42,000 --> 00:16:46,078
than a function and bigger than a data
517
00:16:43,708 --> 00:16:48,449
type is just simply a combination of the
518
00:16:46,078 --> 00:16:50,009
two and hence objects were born we took
519
00:16:48,450 --> 00:16:51,600
our functions in our data types and we
520
00:16:50,009 --> 00:16:53,730
associated them together into these
521
00:16:51,600 --> 00:16:55,740
larger units we want to think in terms
522
00:16:53,730 --> 00:16:57,060
of paragraphs rather than individual
523
00:16:55,740 --> 00:16:58,799
sentences and object-oriented
524
00:16:57,059 --> 00:17:02,069
programming seem to have an answer for
525
00:16:58,799 --> 00:17:03,599
how we could do that it's also very
526
00:17:02,070 --> 00:17:05,279
natural that as we build larger and
527
00:17:03,600 --> 00:17:07,980
larger systems and complex things as
528
00:17:05,279 --> 00:17:09,869
much as possible we want simple rules to
529
00:17:07,980 --> 00:17:11,939
guide us object-oriented programming
530
00:17:09,869 --> 00:17:14,250
seem to present a unit of abstraction
531
00:17:11,939 --> 00:17:16,170
and a set of guidelines whereby we can
532
00:17:14,250 --> 00:17:18,689
incrementally accrete larger and larger
533
00:17:16,170 --> 00:17:20,640
systems this line of thinking is what
534
00:17:18,689 --> 00:17:22,589
led us to patterns and then the so
535
00:17:20,640 --> 00:17:23,790
called solid principles and dependency
536
00:17:22,588 --> 00:17:25,558
injection and test-driven development
537
00:17:23,789 --> 00:17:27,149
and all this stuff which has
538
00:17:25,558 --> 00:17:28,950
subsequently been piled on by many
539
00:17:27,150 --> 00:17:30,420
people who insist that this is now the
540
00:17:28,950 --> 00:17:33,058
one true way to do object-oriented
541
00:17:30,420 --> 00:17:34,950
programming but to me all these best
542
00:17:33,058 --> 00:17:37,230
practices represent band-aids
543
00:17:34,950 --> 00:17:38,870
they are compensation for the fact that
544
00:17:37,230 --> 00:17:41,910
the original vision of object-oriented
545
00:17:38,869 --> 00:17:44,039
out and every few years there's a new
546
00:17:41,910 --> 00:17:45,870
ideology in town about how we actually
547
00:17:44,039 --> 00:17:46,740
do object and programming for real this
548
00:17:45,869 --> 00:17:49,229
time
549
00:17:46,740 --> 00:17:51,089
it's very easy to miss this dynamic I
550
00:17:49,230 --> 00:17:53,610
know I did for several years because I
551
00:17:51,089 --> 00:17:54,750
think within all of these addendums to
552
00:17:53,609 --> 00:17:56,908
object marry programming there there's
553
00:17:54,750 --> 00:17:59,519
lots of mystical speech dancing around
554
00:17:56,909 --> 00:18:01,830
genuine insights but it's not quite
555
00:17:59,519 --> 00:18:03,480
cohesive object when your programming
556
00:18:01,829 --> 00:18:05,308
feels like this circle which we've been
557
00:18:03,480 --> 00:18:10,169
trying to square for over a generation
558
00:18:05,308 --> 00:18:11,519
now finally let's talk about what's
559
00:18:10,169 --> 00:18:13,559
really wrong with object-oriented
560
00:18:11,519 --> 00:18:15,440
programming specifically encapsulation
561
00:18:13,558 --> 00:18:18,298
which is the linchpin of the whole thing
562
00:18:15,440 --> 00:18:20,730
so consider what is an object an object
563
00:18:18,298 --> 00:18:22,740
is this bundle of encapsulated state and
564
00:18:20,730 --> 00:18:24,298
we don't interact with the state of that
565
00:18:22,740 --> 00:18:25,769
object directly all interactions with
566
00:18:24,298 --> 00:18:27,389
that state from the outside world come
567
00:18:25,769 --> 00:18:29,490
in through messages messages to the
568
00:18:27,390 --> 00:18:31,110
object the object has a defined set of
569
00:18:29,490 --> 00:18:32,700
messages which it will receive called
570
00:18:31,109 --> 00:18:34,288
its public interface and so we have
571
00:18:32,700 --> 00:18:36,929
private information hidden behind the
572
00:18:34,288 --> 00:18:38,940
public interface when an object receives
573
00:18:36,929 --> 00:18:40,919
a message it may in turn send messages
574
00:18:38,940 --> 00:18:43,710
to other objects and so we can conceive
575
00:18:40,919 --> 00:18:45,419
of an object-oriented X all
576
00:18:43,710 --> 00:18:48,509
communicating with each other by sending
577
00:18:45,419 --> 00:18:50,429
messages many people today forget though
578
00:18:48,509 --> 00:18:52,319
that the original conception of a
579
00:18:50,429 --> 00:18:54,390
message is not exactly synonymous with
580
00:18:52,319 --> 00:18:56,308
just a method call yes in practice it
581
00:18:54,390 --> 00:18:58,559
means calling methods but a message
582
00:18:56,308 --> 00:19:00,178
strictly speaking sends only copies of
583
00:18:58,558 --> 00:19:02,308
state it doesn't send references a
584
00:19:00,179 --> 00:19:05,610
message sends and returns information
585
00:19:02,308 --> 00:19:07,440
about state not state itself and while
586
00:19:05,609 --> 00:19:09,658
wait a minute objects themselves are
587
00:19:07,440 --> 00:19:11,399
state and this has some interesting
588
00:19:09,659 --> 00:19:13,260
consequences it means that strictly
589
00:19:11,398 --> 00:19:16,319
speaking messages cannot pass around
590
00:19:13,259 --> 00:19:18,119
object references I've never seen a Java
591
00:19:16,319 --> 00:19:19,859
or C sharp code base that ever follow
592
00:19:18,119 --> 00:19:22,439
this rule perhaps some small talk
593
00:19:19,859 --> 00:19:24,808
programs have but in general this rule
594
00:19:22,440 --> 00:19:27,179
is not observed at all and probably for
595
00:19:24,808 --> 00:19:28,648
good reason as we'll discuss but anyway
596
00:19:27,179 --> 00:19:30,390
if we take the role seriously it means
597
00:19:28,648 --> 00:19:32,519
then for an object to send a message to
598
00:19:30,390 --> 00:19:34,230
another object the first object must
599
00:19:32,519 --> 00:19:35,970
hold a private reference to that other
600
00:19:34,230 --> 00:19:37,230
object because otherwise how is it going
601
00:19:35,970 --> 00:19:38,940
to talk to it to talk to knob jekt you
602
00:19:37,230 --> 00:19:40,200
have to have a reference to it and where
603
00:19:38,940 --> 00:19:42,149
is it object going to get a reference to
604
00:19:40,200 --> 00:19:44,669
another object if it can't get object
605
00:19:42,148 --> 00:19:46,408
references from messages the references
606
00:19:44,669 --> 00:19:48,390
which an object needs have to all be
607
00:19:46,409 --> 00:19:49,740
there at the object's inception they
608
00:19:48,390 --> 00:19:52,259
have to be there for the whole lifetime
609
00:19:49,740 --> 00:19:54,028
of the object and there's an even deeper
610
00:19:52,259 --> 00:19:56,190
consequence which is that if an object
611
00:19:54,028 --> 00:19:57,808
is sending messages to another that
612
00:19:56,190 --> 00:19:59,759
other object is part of the first
613
00:19:57,808 --> 00:20:00,509
object's private state and by the
614
00:19:59,759 --> 00:20:02,640
principle of account
615
00:20:00,509 --> 00:20:04,319
halation an object should be responsible
616
00:20:02,640 --> 00:20:06,690
for all the objects which it sends
617
00:20:04,319 --> 00:20:08,250
messages to this should be obvious if
618
00:20:06,690 --> 00:20:10,620
you consider that messages indirectly
619
00:20:08,250 --> 00:20:12,539
read and modify state when B sends a
620
00:20:10,619 --> 00:20:14,909
message to a here it's messing with the
621
00:20:12,539 --> 00:20:16,799
state of a indirectly shirt but is still
622
00:20:14,910 --> 00:20:18,240
messing with its state and so what
623
00:20:16,799 --> 00:20:19,649
happens when other objects come along
624
00:20:18,240 --> 00:20:21,870
and send messages to that same object
625
00:20:19,650 --> 00:20:24,000
what's happening here we have shared
626
00:20:21,869 --> 00:20:25,589
state it's hardly any different than if
627
00:20:24,000 --> 00:20:27,210
you had a single global variable being
628
00:20:25,589 --> 00:20:29,159
shared by say ten functions if you have
629
00:20:27,210 --> 00:20:30,900
an object receiving messages from ten
630
00:20:29,160 --> 00:20:32,310
other objects those objects are all
631
00:20:30,900 --> 00:20:33,780
effectively tied together because
632
00:20:32,309 --> 00:20:35,879
they're implicitly sharing this state
633
00:20:33,779 --> 00:20:37,769
sure the interactions with that state
634
00:20:35,880 --> 00:20:39,390
are indirect through public methods but
635
00:20:37,769 --> 00:20:41,400
those methods are providing very trivial
636
00:20:39,390 --> 00:20:43,050
kinds of coordination of the state you
637
00:20:41,400 --> 00:20:44,880
can impose rules through the access or
638
00:20:43,049 --> 00:20:46,289
methods like saying oh if you access
639
00:20:44,880 --> 00:20:47,550
this field it's a number well you can
640
00:20:46,289 --> 00:20:49,950
only increment that number you can't
641
00:20:47,549 --> 00:20:52,349
mutate it in any other way fine but it's
642
00:20:49,950 --> 00:20:54,180
a very trivial kind of protection the
643
00:20:52,349 --> 00:20:56,549
hard problems of shared state are much
644
00:20:54,180 --> 00:20:58,289
much deeper where in the system of ten
645
00:20:56,549 --> 00:20:59,970
objects all sharing the state is the
646
00:20:58,289 --> 00:21:01,710
real coordination and the answer is
647
00:20:59,970 --> 00:21:04,019
there isn't any as soon as you have
648
00:21:01,710 --> 00:21:07,380
objects being shared encapsulation just
649
00:21:04,019 --> 00:21:09,569
flies out the window so if we're taking
650
00:21:07,380 --> 00:21:11,460
encapsulation seriously the only real
651
00:21:09,569 --> 00:21:13,379
way to structure a program to structure
652
00:21:11,460 --> 00:21:14,910
our objects as a graph is not as a
653
00:21:13,380 --> 00:21:17,580
free-form graph but as a strict
654
00:21:14,910 --> 00:21:19,320
hierarchy at the top of our hierarchy we
655
00:21:17,579 --> 00:21:20,879
have an object representing effectively
656
00:21:19,319 --> 00:21:22,919
the whole program it's our god object
657
00:21:20,880 --> 00:21:24,870
and that has its direct children which
658
00:21:22,920 --> 00:21:26,610
represent the sub components and those
659
00:21:24,869 --> 00:21:28,829
children in turn have their own sub
660
00:21:26,609 --> 00:21:30,659
components and so on down the line and
661
00:21:28,829 --> 00:21:32,669
each object in the hierarchy is
662
00:21:30,660 --> 00:21:34,860
responsible for its direct children and
663
00:21:32,670 --> 00:21:37,500
the message is being passed strictly
664
00:21:34,859 --> 00:21:39,899
only ever go from parent to their direct
665
00:21:37,500 --> 00:21:41,220
child the god object here for example is
666
00:21:39,900 --> 00:21:42,720
not supposed to reach down to its
667
00:21:41,220 --> 00:21:43,860
grandchild it has to do all of its
668
00:21:42,720 --> 00:21:45,600
interactions with this grandchild
669
00:21:43,859 --> 00:21:47,490
indirectly through the grand child's
670
00:21:45,599 --> 00:21:48,899
parent otherwise who really is
671
00:21:47,490 --> 00:21:50,819
responsible for that object who is
672
00:21:48,900 --> 00:21:53,759
managing its State supposed to be the
673
00:21:50,819 --> 00:21:55,079
direct parent and so what happens when
674
00:21:53,759 --> 00:21:56,759
we have some sort of cross-cutting
675
00:21:55,079 --> 00:21:58,470
concern like down in the hierarchy it
676
00:21:56,759 --> 00:22:00,450
turns out oh wait there's some business
677
00:21:58,470 --> 00:22:01,950
that that object has with another object
678
00:22:00,450 --> 00:22:03,660
and a totally different branch of the
679
00:22:01,950 --> 00:22:05,700
hierarchy how do they talk to each other
680
00:22:03,660 --> 00:22:08,009
well not directly everything has to go
681
00:22:05,700 --> 00:22:10,110
through their common ancestor for a to
682
00:22:08,009 --> 00:22:12,029
send a message to be here it can't
683
00:22:10,109 --> 00:22:14,039
actually directly invoke any kind of
684
00:22:12,029 --> 00:22:14,339
method it has to mutate its own state
685
00:22:14,039 --> 00:22:16,048
and
686
00:22:14,339 --> 00:22:18,000
way and then information about that
687
00:22:16,048 --> 00:22:19,918
state that new intention of the object
688
00:22:18,000 --> 00:22:22,558
gets returned from a message sent from
689
00:22:19,919 --> 00:22:24,150
aids parent days parent intern same
690
00:22:22,558 --> 00:22:26,099
thing has to happen so it gets back up
691
00:22:24,150 --> 00:22:27,210
to the common ancestor and then only
692
00:22:26,099 --> 00:22:29,250
finally when we get to the common
693
00:22:27,210 --> 00:22:31,769
ancestor can that intent be realized as
694
00:22:29,250 --> 00:22:33,329
a series of message calls but not
695
00:22:31,769 --> 00:22:34,950
directly down to B it has to be
696
00:22:33,329 --> 00:22:37,259
bucket-brigade it down through the
697
00:22:34,950 --> 00:22:39,419
hierarchy that is how you handle
698
00:22:37,259 --> 00:22:43,200
cross-cutting concerns in a strict
699
00:22:39,419 --> 00:22:45,090
encapsulated hierarchy obviously no one
700
00:22:43,200 --> 00:22:46,649
writes programs this way or at least no
701
00:22:45,089 --> 00:22:48,839
one writes whole programs this way and
702
00:22:46,648 --> 00:22:51,689
for good reason is an absurd way to have
703
00:22:48,839 --> 00:22:53,038
to write your code now you might argue
704
00:22:51,690 --> 00:22:54,538
that people do follow these principles
705
00:22:53,038 --> 00:22:56,700
in practice they just do so
706
00:22:54,538 --> 00:22:58,648
inconsistently and perhaps there is some
707
00:22:56,700 --> 00:23:00,990
value in a code base where you apply
708
00:22:58,648 --> 00:23:02,908
these principles inconsistently perhaps
709
00:23:00,990 --> 00:23:05,159
half-assed encapsulation actually gets
710
00:23:02,909 --> 00:23:07,380
us something so imagine we have some
711
00:23:05,159 --> 00:23:09,240
sort of freeform graph of objects making
712
00:23:07,380 --> 00:23:10,919
up a program and we decide oh well
713
00:23:09,240 --> 00:23:12,028
there's this subsystem of objects that
714
00:23:10,919 --> 00:23:14,220
together should be their own
715
00:23:12,028 --> 00:23:15,808
self-contained encapsulated hierarchy of
716
00:23:14,220 --> 00:23:18,149
objects and so we're going to refactor
717
00:23:15,808 --> 00:23:19,648
our code well very often what that means
718
00:23:18,148 --> 00:23:21,808
is not only do we have to do a lot of
719
00:23:19,648 --> 00:23:23,969
complicated rethinking of the structure
720
00:23:21,808 --> 00:23:25,859
of the relationships here of what calls
721
00:23:23,970 --> 00:23:27,480
what on the other objects we very
722
00:23:25,859 --> 00:23:29,038
typically have to introduce more objects
723
00:23:27,480 --> 00:23:30,269
like say here to represent this whole
724
00:23:29,038 --> 00:23:32,398
new subsystem we probably have to
725
00:23:30,269 --> 00:23:35,099
introduce some new sub god object you
726
00:23:32,398 --> 00:23:37,229
know some ruler of this subsystem now
727
00:23:35,099 --> 00:23:39,028
all interactions with the subsystem have
728
00:23:37,230 --> 00:23:42,210
to be rican sexualized as going through
729
00:23:39,028 --> 00:23:43,919
this minor deity so say we successfully
730
00:23:42,210 --> 00:23:45,509
do this refractory and now while our
731
00:23:43,919 --> 00:23:47,490
code doesn't follow the principles of
732
00:23:45,509 --> 00:23:50,069
encapsulation perfectly it's doing so in
733
00:23:47,490 --> 00:23:52,319
a half consistent way and maybe there's
734
00:23:50,069 --> 00:23:54,269
some benefit there well I think what
735
00:23:52,319 --> 00:23:56,278
tends to happen is subsequently we
736
00:23:54,269 --> 00:23:58,558
decide oh wait we need some new
737
00:23:56,278 --> 00:24:01,380
interaction between elements of this
738
00:23:58,558 --> 00:24:02,819
encapsulated subsystem and instead of
739
00:24:01,380 --> 00:24:04,830
having to do the hard work of figuring
740
00:24:02,819 --> 00:24:07,109
out how exactly it all gets coordinated
741
00:24:04,829 --> 00:24:08,939
from the roots of that subsystem the
742
00:24:07,109 --> 00:24:11,369
temptation is to just handle the
743
00:24:08,940 --> 00:24:13,259
business directly but if we want to do
744
00:24:11,369 --> 00:24:14,759
the proper thing we have two options
745
00:24:13,259 --> 00:24:16,440
that maybe it turns out that that stuff
746
00:24:14,759 --> 00:24:17,759
external to the subsystem actually just
747
00:24:16,440 --> 00:24:19,230
needs to get integrated into that
748
00:24:17,759 --> 00:24:21,690
subsystem and so it comes under the
749
00:24:19,230 --> 00:24:23,339
purview of the subsystems root but
750
00:24:21,690 --> 00:24:24,840
otherwise we now have two subsystems
751
00:24:23,339 --> 00:24:26,699
that need to coordinate and who's going
752
00:24:24,839 --> 00:24:28,199
to do the coordination well now we need
753
00:24:26,700 --> 00:24:29,880
a new subsystem god
754
00:24:28,200 --> 00:24:32,669
responsible for the collective business
755
00:24:29,880 --> 00:24:34,528
of these two subsystems and now all
756
00:24:32,669 --> 00:24:36,269
interactions of these two subsystems
757
00:24:34,528 --> 00:24:37,798
have to go through this root object but
758
00:24:36,269 --> 00:24:40,109
also all interactions with the outside
759
00:24:37,798 --> 00:24:42,720
world and these two subsystems have to
760
00:24:40,109 --> 00:24:44,189
go through this new root object so as
761
00:24:42,720 --> 00:24:45,839
you can see chances are really good that
762
00:24:44,190 --> 00:24:47,639
what you would actually do is say
763
00:24:45,839 --> 00:24:49,109
it and just do this you would just reach
764
00:24:47,638 --> 00:24:50,490
in and have the objects directly
765
00:24:49,109 --> 00:24:52,619
interact with each other whether they
766
00:24:50,490 --> 00:24:54,149
should properly do so or not and now
767
00:24:52,619 --> 00:24:56,308
where is the encapsulation what's the
768
00:24:54,148 --> 00:24:58,739
point whether you follow the rules
769
00:24:56,308 --> 00:25:00,658
strictly or loosely you're in a bad
770
00:24:58,740 --> 00:25:02,669
place if you follow the rule strictly
771
00:25:00,659 --> 00:25:11,490
most things you do end up at being very
772
00:25:02,669 --> 00:25:12,929
unobtrusive these entities tends to be
773
00:25:11,490 --> 00:25:14,788
very abstract and nebulous but
774
00:25:12,929 --> 00:25:16,620
alternatively if you follow the rules
775
00:25:14,788 --> 00:25:19,470
loosely what are you even getting why
776
00:25:16,619 --> 00:25:20,908
are you bothering what is the point when
777
00:25:19,470 --> 00:25:22,528
I look at your object-oriented code base
778
00:25:20,909 --> 00:25:24,450
what I'm going to encounter is either
779
00:25:22,528 --> 00:25:26,429
this over-engineered giant tower of
780
00:25:24,450 --> 00:25:28,649
abstractions or I'm going to be looking
781
00:25:26,429 --> 00:25:30,750
at this inconsistently architected pile
782
00:25:28,648 --> 00:25:32,729
of objects that are all probably tangled
783
00:25:30,750 --> 00:25:34,259
together like Christmas lights you'll
784
00:25:32,730 --> 00:25:35,909
have all these objects giving you a warm
785
00:25:34,259 --> 00:25:37,200
fuzzy feeling of encapsulation
786
00:25:35,909 --> 00:25:41,460
but you're not going to have any real
787
00:25:37,200 --> 00:25:42,870
encapsulation of any significance what
788
00:25:41,460 --> 00:25:44,909
people tend to create when they design
789
00:25:42,869 --> 00:25:46,648
object-oriented programs are overly
790
00:25:44,909 --> 00:25:48,750
architected buildings where the walls
791
00:25:46,648 --> 00:25:50,038
have been prematurely erected before we
792
00:25:48,750 --> 00:25:51,720
have really figured out what the needs
793
00:25:50,038 --> 00:25:53,429
of the floor plan are and so what
794
00:25:51,720 --> 00:25:55,139
happens is down the line turns out oh
795
00:25:53,429 --> 00:25:56,700
wait we need to get from this room over
796
00:25:55,138 --> 00:25:58,798
here to that room over there but oh wait
797
00:25:56,700 --> 00:26:00,090
we've erected barriers in between so we
798
00:25:58,798 --> 00:26:01,798
end up busting a bunch of holes through
799
00:26:00,089 --> 00:26:03,449
all the walls like the kool-aid guy and
800
00:26:01,798 --> 00:26:05,069
the resulting pattern is really not
801
00:26:03,450 --> 00:26:06,990
organized at all it's just Swiss cheese
802
00:26:05,069 --> 00:26:09,210
we thought we were being disciplined
803
00:26:06,990 --> 00:26:10,649
neatly modularizing all the state but
804
00:26:09,210 --> 00:26:12,149
then the requirements changed or we just
805
00:26:10,648 --> 00:26:14,268
didn't anticipate certain details of the
806
00:26:12,148 --> 00:26:16,469
implementation and we end up with a mess
807
00:26:14,269 --> 00:26:18,120
the lesson we should take from this is
808
00:26:16,470 --> 00:26:20,250
to be very careful about erecting
809
00:26:18,119 --> 00:26:21,509
barriers about imposing structure it's
810
00:26:20,250 --> 00:26:23,339
actually better to start out with a
811
00:26:21,509 --> 00:26:24,990
free-form absence of structure rather
812
00:26:23,339 --> 00:26:26,668
than impose a structure that will likely
813
00:26:24,990 --> 00:26:28,740
turn out to not really fit our problem
814
00:26:26,669 --> 00:26:30,090
bad structure that doesn't really fit
815
00:26:28,740 --> 00:26:31,230
our problem not only makes it harder to
816
00:26:30,089 --> 00:26:33,359
implement the code in the first place
817
00:26:31,230 --> 00:26:34,919
that hinders change and it confuses
818
00:26:33,359 --> 00:26:36,719
anyone who looks at our code because
819
00:26:34,919 --> 00:26:38,879
it's implying one thing but then what's
820
00:26:36,720 --> 00:26:40,200
really going on is another in the
821
00:26:38,878 --> 00:26:41,459
object-oriented world we have to think
822
00:26:40,200 --> 00:26:43,380
about all these graphs we have
823
00:26:41,460 --> 00:26:44,759
think about an inheritance hierarchy we
824
00:26:43,380 --> 00:26:46,320
have to think about a composition graph
825
00:26:44,759 --> 00:26:47,548
we have to think about data flows
826
00:26:46,319 --> 00:26:49,558
between the object and also we're
827
00:26:47,548 --> 00:26:51,210
thinking about a coal graph the
828
00:26:49,558 --> 00:26:53,548
liberating thing about procedural code
829
00:26:51,210 --> 00:26:55,019
is there's just the coal graph we also
830
00:26:53,548 --> 00:26:57,298
of course do have to think about how our
831
00:26:55,019 --> 00:26:58,528
data is structured and how our data gets
832
00:26:57,298 --> 00:27:00,269
transformed throughout the course of the
833
00:26:58,528 --> 00:27:01,919
program but the beauty of procedural
834
00:27:00,269 --> 00:27:03,980
code is that we can think about that
835
00:27:01,919 --> 00:27:05,820
totally independent of any notion of
836
00:27:03,980 --> 00:27:07,589
responsibilities when I'm looking at my
837
00:27:05,819 --> 00:27:09,720
data I can think just about my data and
838
00:27:07,589 --> 00:27:11,158
when I'm looking at my functions I'm not
839
00:27:09,720 --> 00:27:13,710
thinking about all these self-imposed
840
00:27:11,159 --> 00:27:15,870
barriers I'm not constantly trying to
841
00:27:13,710 --> 00:27:17,610
group and modularize everything into
842
00:27:15,869 --> 00:27:20,099
these small units of so-called single
843
00:27:17,609 --> 00:27:21,869
responsibilities when I sit down to
844
00:27:20,099 --> 00:27:23,849
write object-oriented code I always have
845
00:27:21,869 --> 00:27:25,798
to play this game I have this mental
846
00:27:23,849 --> 00:27:27,389
list of the obvious data types which my
847
00:27:25,798 --> 00:27:28,950
code will deal with and have the
848
00:27:27,390 --> 00:27:30,899
separate mental list of all the imagined
849
00:27:28,950 --> 00:27:33,028
behaviors I want in my program although
850
00:27:30,898 --> 00:27:35,459
the functionality I imagine it to have
851
00:27:33,028 --> 00:27:37,440
and then what object-oriented ideology
852
00:27:35,460 --> 00:27:39,630
demands is that I take all my behaviors
853
00:27:37,440 --> 00:27:41,940
and I somehow associate each one with
854
00:27:39,630 --> 00:27:43,679
one of my data types inevitably what
855
00:27:41,940 --> 00:27:45,210
this means in any non-trivial program is
856
00:27:43,679 --> 00:27:47,159
I'm actually going to have to introduce
857
00:27:45,210 --> 00:27:49,110
all sorts of additional data types
858
00:27:47,159 --> 00:27:50,700
just to be these containers for certain
859
00:27:49,109 --> 00:27:52,829
behaviors which otherwise don't
860
00:27:50,700 --> 00:27:54,269
naturally fit with any of my obvious
861
00:27:52,829 --> 00:27:55,859
data types the data types I knew I
862
00:27:54,269 --> 00:27:56,759
actually wanted because they represent
863
00:27:55,859 --> 00:27:58,918
actual data I need
864
00:27:56,759 --> 00:28:00,359
in fact as programs get larger and
865
00:27:58,919 --> 00:28:02,700
larger in object-oriented code it tends
866
00:28:00,359 --> 00:28:04,648
to be that these unobvious unnatural
867
00:28:02,700 --> 00:28:06,450
data types tend to actually predominate
868
00:28:04,648 --> 00:28:08,158
you end up with majority of so-called
869
00:28:06,450 --> 00:28:09,808
data types which really aren't there
870
00:28:08,159 --> 00:28:11,730
because they're representing data they
871
00:28:09,808 --> 00:28:14,089
exist simply as a text to conform to
872
00:28:11,730 --> 00:28:16,288
this ideology about code modular ization
873
00:28:14,089 --> 00:28:18,298
very quickly we end up in what Steve
874
00:28:16,288 --> 00:28:20,038
Yeager called the kingdom of nouns where
875
00:28:18,298 --> 00:28:21,808
every aspect of our program has to be
876
00:28:20,038 --> 00:28:24,089
rican Sepp Shu alized as not just mere
877
00:28:21,808 --> 00:28:25,589
standalone verbs you know functions you
878
00:28:24,089 --> 00:28:28,500
have to be Rican sexualised as nouns
879
00:28:25,589 --> 00:28:30,359
things that represent a set of behaviors
880
00:28:28,500 --> 00:28:31,919
and so what we get in our
881
00:28:30,359 --> 00:28:34,319
object-oriented code bases are all these
882
00:28:31,919 --> 00:28:36,840
service classes and manager classes and
883
00:28:34,319 --> 00:28:40,079
other what I call Dewar classes these
884
00:28:36,839 --> 00:28:41,428
very nebulous and abstract entities even
885
00:28:40,079 --> 00:28:43,168
when dealing with data types and
886
00:28:41,429 --> 00:28:45,450
behaviors that are relatively concrete
887
00:28:43,169 --> 00:28:47,520
which have fairly visible connections to
888
00:28:45,450 --> 00:28:49,470
the functionality apparent to actual
889
00:28:47,519 --> 00:28:51,148
uses of the program even here the
890
00:28:49,470 --> 00:28:52,889
matchmaking game constantly presents us
891
00:28:51,148 --> 00:28:55,109
with these obnoxious philosophical
892
00:28:52,888 --> 00:28:55,379
dilemmas in object-oriented analysis
893
00:28:55,109 --> 00:28:56,909
into
894
00:28:55,380 --> 00:28:59,040
design we constantly have to ask ourself
895
00:28:56,910 --> 00:29:01,529
stupid questions like should a message
896
00:28:59,039 --> 00:29:03,480
send itself because maybe instead we
897
00:29:01,529 --> 00:29:05,849
should have some sender object which
898
00:29:03,480 --> 00:29:07,349
sends messages or wait a minute maybe
899
00:29:05,849 --> 00:29:10,049
there should be a receiver object which
900
00:29:07,349 --> 00:29:13,109
receives messages or a connection object
901
00:29:10,049 --> 00:29:15,180
which transmits messages so very quickly
902
00:29:13,109 --> 00:29:16,949
the real world model in which object
903
00:29:15,180 --> 00:29:18,750
oriented programming promises becomes a
904
00:29:16,950 --> 00:29:22,230
fool's game where there aren't any real
905
00:29:18,750 --> 00:29:23,880
good answers in my experience object
906
00:29:22,230 --> 00:29:26,640
oriented analysis and design very
907
00:29:23,880 --> 00:29:28,770
quickly becomes analysis paralysis if
908
00:29:26,640 --> 00:29:30,000
you take the ideology seriously as I did
909
00:29:28,769 --> 00:29:31,769
you're going to waste a lot of time
910
00:29:30,000 --> 00:29:33,630
hemming and hawing about to
911
00:29:31,769 --> 00:29:35,849
conceptualize these elements of your
912
00:29:33,630 --> 00:29:37,800
program object-oriented programming is
913
00:29:35,849 --> 00:29:39,480
generally sold to students on the basis
914
00:29:37,799 --> 00:29:42,240
of these trivial examples that neatly
915
00:29:39,480 --> 00:29:43,980
model real-world taxonomy but what we
916
00:29:42,240 --> 00:29:45,960
get in practice from object-oriented
917
00:29:43,980 --> 00:29:48,299
analysis and design is a lot of very
918
00:29:45,960 --> 00:29:51,809
abstract excess structure with no
919
00:29:48,299 --> 00:29:53,460
obvious real-world analogues note here
920
00:29:51,809 --> 00:29:55,950
that programmers have their own peculiar
921
00:29:53,460 --> 00:29:57,630
definition of abstract when programmers
922
00:29:55,950 --> 00:29:59,850
talk about abstraction they're generally
923
00:29:57,630 --> 00:30:02,250
talking about simplified interface over
924
00:29:59,849 --> 00:30:03,959
complex inner workings what's odd about
925
00:30:02,250 --> 00:30:06,509
this is that a more general usage
926
00:30:03,960 --> 00:30:08,279
abstract as a connotation of being hard
927
00:30:06,509 --> 00:30:10,079
to understand something which is
928
00:30:08,279 --> 00:30:12,329
abstract has no resemblance to the
929
00:30:10,079 --> 00:30:14,099
things of common daily life and it turns
930
00:30:12,329 --> 00:30:16,829
out that most things which programs do
931
00:30:14,099 --> 00:30:18,719
are abstract in this sense and so it
932
00:30:16,829 --> 00:30:20,759
shouldn't be surprising that we have
933
00:30:18,720 --> 00:30:22,710
great difficulty conceptualizing the
934
00:30:20,759 --> 00:30:24,299
components of a typical program in terms
935
00:30:22,710 --> 00:30:26,009
of neatly self-contained modules
936
00:30:24,299 --> 00:30:28,889
particularly modules which have any
937
00:30:26,009 --> 00:30:31,019
real-world analogue when we pollute our
938
00:30:28,890 --> 00:30:32,940
code with generic entities like managers
939
00:30:31,019 --> 00:30:34,349
and factories and services we're not
940
00:30:32,940 --> 00:30:35,700
really making anything easier to
941
00:30:34,349 --> 00:30:38,069
understand we're just putting a happy
942
00:30:35,700 --> 00:30:40,080
face on the underlying abstract business
943
00:30:38,069 --> 00:30:42,059
and for every excess layer of
944
00:30:40,079 --> 00:30:44,849
abstraction or getting more abstract
945
00:30:42,059 --> 00:30:46,679
NISS in attempting to neatly modular
946
00:30:44,849 --> 00:30:48,299
eyes and label every little fiddly bit
947
00:30:46,680 --> 00:30:50,009
that our program does we're actually
948
00:30:48,299 --> 00:30:52,710
just making our program harder to
949
00:30:50,009 --> 00:30:54,150
understand something that happens all
950
00:30:52,710 --> 00:30:56,549
the time when I look at object-oriented
951
00:30:54,150 --> 00:30:58,620
code bases is that I'll try and find the
952
00:30:56,549 --> 00:31:01,169
parts in code that correspond to some
953
00:30:58,619 --> 00:31:03,029
user visible functionality but trying to
954
00:31:01,170 --> 00:31:04,800
find the functionality going by clues
955
00:31:03,029 --> 00:31:06,589
from the names of classes and the names
956
00:31:04,799 --> 00:31:09,329
of methods tends to be very misleading
957
00:31:06,589 --> 00:31:11,730
very typically my expectation that
958
00:31:09,329 --> 00:31:13,649
shanell DX would be in the class named X
959
00:31:11,730 --> 00:31:15,360
turns out to be wrong because the
960
00:31:13,650 --> 00:31:17,220
abstract nature of what we typically do
961
00:31:15,359 --> 00:31:18,689
in programs generally necessitates that
962
00:31:17,220 --> 00:31:20,009
functionality is not going to be
963
00:31:18,690 --> 00:31:23,220
self-contained it's not going to neatly
964
00:31:20,009 --> 00:31:24,990
fit into one neat module and so the
965
00:31:23,220 --> 00:31:26,910
class which is called X will very
966
00:31:24,990 --> 00:31:28,170
superficially relate to X but then all
967
00:31:26,910 --> 00:31:30,690
the real work is done elsewhere
968
00:31:28,170 --> 00:31:32,220
scattered throughout the code this makes
969
00:31:30,690 --> 00:31:34,500
me question what is the value of having
970
00:31:32,220 --> 00:31:36,839
a class called X if it doesn't really
971
00:31:34,500 --> 00:31:38,579
contain all the business of X what this
972
00:31:36,839 --> 00:31:40,649
class X really represents is actually
973
00:31:38,579 --> 00:31:42,449
misleading code structure and how is
974
00:31:40,650 --> 00:31:45,030
that helpful how is that conducive to
975
00:31:42,450 --> 00:31:46,650
understanding of your code base the
976
00:31:45,029 --> 00:31:47,910
other reason I have this problem reading
977
00:31:46,650 --> 00:31:50,130
code bases and trying to track down
978
00:31:47,910 --> 00:31:52,019
where functionality actually lives is
979
00:31:50,130 --> 00:31:54,360
because object-oriented design tends to
980
00:31:52,019 --> 00:31:56,220
fracture functionality in our code it
981
00:31:54,359 --> 00:31:58,829
tends to take what otherwise could be
982
00:31:56,220 --> 00:32:00,480
relatively self-contained code and split
983
00:31:58,829 --> 00:32:02,789
it up into many separate methods across
984
00:32:00,480 --> 00:32:05,250
many separate classes typically often in
985
00:32:02,789 --> 00:32:07,559
many separate files for God's sake this
986
00:32:05,250 --> 00:32:09,930
fracturing is accepted because of an
987
00:32:07,559 --> 00:32:12,329
ideology about encapsulation and this
988
00:32:09,930 --> 00:32:14,580
notion of classes and methods properly
989
00:32:12,329 --> 00:32:17,129
having so-called single responsibilities
990
00:32:14,579 --> 00:32:19,230
and our certainly valid arguments for
991
00:32:17,130 --> 00:32:21,510
that idea certainly it is much easier to
992
00:32:19,230 --> 00:32:23,099
get a small short function correct and
993
00:32:21,509 --> 00:32:25,379
to get a large sprawling function
994
00:32:23,099 --> 00:32:26,969
correct but the important question is
995
00:32:25,380 --> 00:32:28,650
that in splitting our code under too
996
00:32:26,970 --> 00:32:30,329
many little small methods and too many
997
00:32:28,650 --> 00:32:31,860
separate classes are we actually
998
00:32:30,329 --> 00:32:33,629
decreasing the total complexity of our
999
00:32:31,859 --> 00:32:35,399
program or just displacing the
1000
00:32:33,630 --> 00:32:37,620
complexity just merely spreading it
1001
00:32:35,400 --> 00:32:39,509
around in either case there is this
1002
00:32:37,619 --> 00:32:41,429
attendant trade-off we're making whereby
1003
00:32:39,509 --> 00:32:42,420
splitting up larger units of code into
1004
00:32:41,430 --> 00:32:44,009
many smaller ones
1005
00:32:42,420 --> 00:32:46,380
we're greatly increasing is the
1006
00:32:44,009 --> 00:32:47,670
so-called surface area of our code where
1007
00:32:46,380 --> 00:32:49,260
I come along and I look at your code
1008
00:32:47,670 --> 00:32:51,029
base and try and get a foothold and
1009
00:32:49,259 --> 00:32:52,740
everything split up into these tiny
1010
00:32:51,029 --> 00:32:54,960
little units these tiny little packets
1011
00:32:52,740 --> 00:32:56,579
of code reading this kind of code often
1012
00:32:54,960 --> 00:32:57,900
feels frustrating in the same way it can
1013
00:32:56,579 --> 00:32:59,279
be frustrating to eat a bunch of little
1014
00:32:57,900 --> 00:33:00,990
candies that are all individually
1015
00:32:59,279 --> 00:33:02,759
wrapped and when all your methods are
1016
00:33:00,990 --> 00:33:04,440
really really short you end up having to
1017
00:33:02,759 --> 00:33:06,779
jump all around the code to find any
1018
00:33:04,440 --> 00:33:08,549
line of logic a lot of business that
1019
00:33:06,779 --> 00:33:10,710
otherwise could be neatly sequentially
1020
00:33:08,549 --> 00:33:12,720
expressed in longer methods gets
1021
00:33:10,710 --> 00:33:14,160
artificially split up so it feels like
1022
00:33:12,720 --> 00:33:15,779
you've taken a neatly sorted deck of
1023
00:33:14,160 --> 00:33:19,130
cards and thrown them into the air so
1024
00:33:15,779 --> 00:33:19,129
you can play 52 card pickup
1025
00:33:20,589 --> 00:33:23,319
so if you're not going to be writing
1026
00:33:21,700 --> 00:33:24,519
object-oriented code what are you going
1027
00:33:23,319 --> 00:33:26,230
to be doing instead you're going to be
1028
00:33:24,519 --> 00:33:28,450
writing procedural code but what does
1029
00:33:26,230 --> 00:33:29,679
that look like well as I mentioned at
1030
00:33:28,450 --> 00:33:31,659
the beginning this doesn't necessarily
1031
00:33:29,679 --> 00:33:33,429
mean you need to avoid classes entirely
1032
00:33:31,659 --> 00:33:35,380
if you have a language like Python or
1033
00:33:33,429 --> 00:33:37,360
C++ where you have both straight
1034
00:33:35,380 --> 00:33:38,830
functions and also classes there are
1035
00:33:37,359 --> 00:33:41,259
some cases where the association between
1036
00:33:38,829 --> 00:33:43,269
your data types and certain functions is
1037
00:33:41,259 --> 00:33:45,129
really really strong that it fits some
1038
00:33:43,269 --> 00:33:47,079
organizational purposes to just
1039
00:33:45,130 --> 00:33:48,640
explicitly associate them together but
1040
00:33:47,079 --> 00:33:50,829
making those functions methods of that
1041
00:33:48,640 --> 00:33:52,960
type the most obvious example would be
1042
00:33:50,829 --> 00:33:56,349
ADT so abstract data types things like
1043
00:33:52,960 --> 00:33:57,788
queues and lists and so forth the key
1044
00:33:56,349 --> 00:33:58,808
thing to keep in mind however is at the
1045
00:33:57,788 --> 00:34:00,490
moment you start hemming and hawing
1046
00:33:58,808 --> 00:34:02,678
about whether this particular function
1047
00:34:00,490 --> 00:34:04,450
really has a primary association with
1048
00:34:02,679 --> 00:34:05,679
that data type that should be the moment
1049
00:34:04,450 --> 00:34:07,659
you say screw it will make it just a
1050
00:34:05,679 --> 00:34:09,639
plain function because it turns out that
1051
00:34:07,659 --> 00:34:11,470
most things we do in code tend to be
1052
00:34:09,639 --> 00:34:13,539
cross-cutting concerns they don't have
1053
00:34:11,469 --> 00:34:15,428
necessarily special obvious
1054
00:34:13,539 --> 00:34:16,659
relationships with particular data types
1055
00:34:15,429 --> 00:34:18,250
they might concern more than one data
1056
00:34:16,659 --> 00:34:19,480
type and that's why you should generally
1057
00:34:18,250 --> 00:34:21,099
prefer functions so you don't have to
1058
00:34:19,480 --> 00:34:25,300
play this silly game of matchmaking
1059
00:34:21,099 --> 00:34:26,830
functions two data types so we're going
1060
00:34:25,300 --> 00:34:28,839
to be writing our code primarily out of
1061
00:34:26,829 --> 00:34:30,639
plain functions and we're not going to
1062
00:34:28,838 --> 00:34:32,588
attempt to encapsulate the state of our
1063
00:34:30,639 --> 00:34:35,349
program at a fine-grained level because
1064
00:34:32,588 --> 00:34:36,730
it doesn't work however shared state is
1065
00:34:35,349 --> 00:34:39,099
still a problem and if we're not careful
1066
00:34:36,730 --> 00:34:40,960
it can get out of hand we can't totally
1067
00:34:39,099 --> 00:34:42,879
solve the problem unless we do a pure
1068
00:34:40,960 --> 00:34:44,349
functional programming but short of that
1069
00:34:42,878 --> 00:34:47,618
there are broad guidelines we can follow
1070
00:34:44,349 --> 00:34:49,780
to mitigate the problem first off when
1071
00:34:47,619 --> 00:34:51,460
in doubt parameterize this means that
1072
00:34:49,780 --> 00:34:52,869
rather than passing data to functions
1073
00:34:51,460 --> 00:34:55,030
through global variables you should
1074
00:34:52,869 --> 00:34:56,500
instead make that data and explicit
1075
00:34:55,030 --> 00:34:58,780
parameter of the function so it has to
1076
00:34:56,500 --> 00:35:00,309
get explicitly passed in as much as
1077
00:34:58,780 --> 00:35:02,980
possible we want data access in our
1078
00:35:00,309 --> 00:35:04,719
program to flow through the Col graph so
1079
00:35:02,980 --> 00:35:06,219
any time you're tempted to pass data to
1080
00:35:04,719 --> 00:35:07,868
a function through a global because it
1081
00:35:06,219 --> 00:35:09,579
seems more efficient or maybe just more
1082
00:35:07,869 --> 00:35:12,700
convenient you should give that a strong
1083
00:35:09,579 --> 00:35:13,989
reconsideration secondly whatever
1084
00:35:12,699 --> 00:35:15,759
Global's you do end up with in your
1085
00:35:13,989 --> 00:35:17,229
program it can be slightly helpful to
1086
00:35:15,760 --> 00:35:19,089
group them logically into data types
1087
00:35:17,230 --> 00:35:20,740
even if this means you effectively have
1088
00:35:19,088 --> 00:35:22,210
a data type with one instance in your
1089
00:35:20,739 --> 00:35:23,319
whole program this little trick can
1090
00:35:22,210 --> 00:35:25,389
often make your code seem just a little
1091
00:35:23,320 --> 00:35:27,190
bit more organized in a sense you're
1092
00:35:25,389 --> 00:35:29,409
just using data types of way to create
1093
00:35:27,190 --> 00:35:30,700
tiny little sub namespaces but if you do
1094
00:35:29,409 --> 00:35:32,618
a good job logically grouping your
1095
00:35:30,699 --> 00:35:33,819
Global's this way as a side benefit this
1096
00:35:32,619 --> 00:35:35,380
can complement rule number
1097
00:35:33,820 --> 00:35:37,930
because now you can more conveniently
1098
00:35:35,380 --> 00:35:39,460
pass this global state to functions by a
1099
00:35:37,929 --> 00:35:41,199
bundling your data together in two types
1100
00:35:39,460 --> 00:35:42,789
you typically cut down on the number of
1101
00:35:41,199 --> 00:35:44,710
parameters which functions have to take
1102
00:35:42,789 --> 00:35:48,090
though do be careful there is an art to
1103
00:35:44,710 --> 00:35:48,090
how you logically group things together
1104
00:35:48,150 --> 00:35:52,180
the third guideline is to
1105
00:35:49,989 --> 00:35:53,919
opportunistically favor pure functions
1106
00:35:52,179 --> 00:35:55,389
even if you're not explicitly working in
1107
00:35:53,920 --> 00:35:57,099
a functional style or working in a
1108
00:35:55,389 --> 00:35:58,900
functional language if you see an
1109
00:35:57,099 --> 00:36:00,429
opportunity to make a function pure it's
1110
00:35:58,900 --> 00:36:03,039
generally good strategy to take that
1111
00:36:00,429 --> 00:36:04,690
opportunity again pure functions tend to
1112
00:36:03,039 --> 00:36:06,039
come in at efficiency cost but the
1113
00:36:04,690 --> 00:36:07,240
brilliant thing about pure functions is
1114
00:36:06,039 --> 00:36:09,639
that they're the only truly
1115
00:36:07,239 --> 00:36:11,108
self-contained unit of code when I'm
1116
00:36:09,639 --> 00:36:12,579
reading and writing a pure function I
1117
00:36:11,108 --> 00:36:14,108
don't have to think about anything else
1118
00:36:12,579 --> 00:36:16,569
I can just consider that function
1119
00:36:14,108 --> 00:36:18,279
entirely unto itself therefore they tend
1120
00:36:16,570 --> 00:36:22,539
to be easier to understand and to make
1121
00:36:18,280 --> 00:36:24,040
correct the fourth guideline is that we
1122
00:36:22,539 --> 00:36:26,170
actually should try to encapsulate our
1123
00:36:24,039 --> 00:36:28,210
code only in a very loose general sense
1124
00:36:26,170 --> 00:36:29,710
at the level of namespaces packages
1125
00:36:28,210 --> 00:36:32,170
modules whatever your language has so
1126
00:36:29,710 --> 00:36:33,909
when I'm working in go Lang for example
1127
00:36:32,170 --> 00:36:35,590
I think of each package is having its
1128
00:36:33,909 --> 00:36:38,259
own private state and then a public
1129
00:36:35,590 --> 00:36:39,970
interface I find that encapsulation at
1130
00:36:38,260 --> 00:36:41,500
this coarse grain level tends to work
1131
00:36:39,969 --> 00:36:43,569
because you're typically dealing with
1132
00:36:41,500 --> 00:36:45,130
much larger units of code than the
1133
00:36:43,570 --> 00:36:47,440
supposedly ideal classes of
1134
00:36:45,130 --> 00:36:48,880
object-oriented programming the typical
1135
00:36:47,440 --> 00:36:50,590
golang program is going to have not that
1136
00:36:48,880 --> 00:36:52,480
many packages maybe like ten at the high
1137
00:36:50,590 --> 00:36:54,130
end and structuring I'm your handful of
1138
00:36:52,480 --> 00:36:57,010
elements into hierarchy of encapsulation
1139
00:36:54,130 --> 00:36:58,900
is reasonably doable when it turns out
1140
00:36:57,010 --> 00:37:00,220
during development that oh wait I have
1141
00:36:58,900 --> 00:37:01,690
some cross-cutting concern of my
1142
00:37:00,219 --> 00:37:03,519
packages and so we're going to violate
1143
00:37:01,690 --> 00:37:05,710
this perfect hierarchy of encapsulation
1144
00:37:03,519 --> 00:37:07,150
again it's not such a big deal because
1145
00:37:05,710 --> 00:37:09,699
you're dealing with a relatively small
1146
00:37:07,150 --> 00:37:11,349
group of objects all the basic problems
1147
00:37:09,699 --> 00:37:13,239
of encapsulation are still there is just
1148
00:37:11,349 --> 00:37:16,750
that the coarse-grained macro level the
1149
00:37:13,239 --> 00:37:18,250
problems are reasonably manageable the
1150
00:37:16,750 --> 00:37:20,619
last guideline is that you shouldn't be
1151
00:37:18,250 --> 00:37:21,670
scared of long functions for a long time
1152
00:37:20,619 --> 00:37:23,829
now program and students have been
1153
00:37:21,670 --> 00:37:25,750
advised to when in doubt chop their code
1154
00:37:23,829 --> 00:37:27,608
into smaller and smaller functions but
1155
00:37:25,750 --> 00:37:29,650
doing this has significant costs there
1156
00:37:27,608 --> 00:37:31,480
are trade-offs it turns out that most
1157
00:37:29,650 --> 00:37:33,309
programs have these key sections where
1158
00:37:31,480 --> 00:37:35,409
most of what the code is doing is a long
1159
00:37:33,309 --> 00:37:37,029
laundry list of stuff and what we're
1160
00:37:35,409 --> 00:37:38,679
told to do in these scenarios is write
1161
00:37:37,030 --> 00:37:40,180
functions like this where all the
1162
00:37:38,679 --> 00:37:42,190
business has been extracted out to
1163
00:37:40,179 --> 00:37:44,108
separate functions the problem with
1164
00:37:42,190 --> 00:37:47,019
doing this pervasively is that what was
1165
00:37:44,108 --> 00:37:47,289
naturally a logical sequence of code and
1166
00:37:47,019 --> 00:37:49,480
was
1167
00:37:47,289 --> 00:37:51,460
otherwise written in sequence top to
1168
00:37:49,480 --> 00:37:54,070
bottom is now spread and out of order
1169
00:37:51,460 --> 00:37:55,690
throughout your codebase obviously in
1170
00:37:54,070 --> 00:37:57,250
cases where the business extracted to a
1171
00:37:55,690 --> 00:37:59,230
separate function is something that you
1172
00:37:57,250 --> 00:38:01,119
want to call in multiple places that's a
1173
00:37:59,230 --> 00:38:02,440
very good reason to have a function but
1174
00:38:01,119 --> 00:38:04,269
if all these functions were just called
1175
00:38:02,440 --> 00:38:05,769
in this one place I would generally
1176
00:38:04,269 --> 00:38:07,570
prefer looking at code where the
1177
00:38:05,769 --> 00:38:09,670
business of those functions is just done
1178
00:38:07,570 --> 00:38:11,890
in line and if you want high-level
1179
00:38:09,670 --> 00:38:14,110
documentation of what's going on in my
1180
00:38:11,889 --> 00:38:16,119
func here then you just put what I would
1181
00:38:14,110 --> 00:38:18,579
call a section comment denoting what
1182
00:38:16,119 --> 00:38:20,079
each section of the code does in this
1183
00:38:18,579 --> 00:38:22,119
arrangement the sequence of the business
1184
00:38:20,079 --> 00:38:23,349
is totally clear and when I'm browsing
1185
00:38:22,119 --> 00:38:24,969
the whole codebase when I'm looking
1186
00:38:23,349 --> 00:38:26,170
outside this function there's less
1187
00:38:24,969 --> 00:38:27,639
clutter because there are now fewer
1188
00:38:26,170 --> 00:38:29,260
functions have to look at and wonder
1189
00:38:27,639 --> 00:38:31,449
well hey where is that cold I wonder
1190
00:38:29,260 --> 00:38:33,160
what that thing does it also has
1191
00:38:31,449 --> 00:38:35,259
advantage of letting us avoid having to
1192
00:38:33,159 --> 00:38:36,549
named functions naming stuff is really
1193
00:38:35,260 --> 00:38:38,650
important in code but is really really
1194
00:38:36,550 --> 00:38:40,120
hard to do well and in general I find it
1195
00:38:38,650 --> 00:38:42,280
preferable if we can avoid naming
1196
00:38:40,119 --> 00:38:43,299
entities as much as possible in this
1197
00:38:42,280 --> 00:38:44,740
arrangement we don't have to think hard
1198
00:38:43,300 --> 00:38:46,510
about what to call these functions we
1199
00:38:44,739 --> 00:38:48,279
can just have a comment line and have a
1200
00:38:46,510 --> 00:38:50,020
full English sentence which generally is
1201
00:38:48,280 --> 00:38:53,590
better at conveying accurate meaning and
1202
00:38:50,019 --> 00:38:55,179
also is simply easier to write if for
1203
00:38:53,590 --> 00:38:56,800
whatever reason it doesn't seem adequate
1204
00:38:55,179 --> 00:38:58,659
to simply comment a section rather than
1205
00:38:56,800 --> 00:39:00,730
extract it to a separate function the
1206
00:38:58,659 --> 00:39:02,440
next best thing is to make it a private
1207
00:39:00,730 --> 00:39:04,570
function a nested function such that
1208
00:39:02,440 --> 00:39:06,490
it's clear this function is not called
1209
00:39:04,570 --> 00:39:08,710
anywhere else it's only cold within this
1210
00:39:06,489 --> 00:39:10,000
function in this arrangement I as a
1211
00:39:08,710 --> 00:39:11,559
reader of your code coming from the
1212
00:39:10,000 --> 00:39:13,510
outside I'm still presented with a
1213
00:39:11,559 --> 00:39:14,769
smaller surface area fewer entities in
1214
00:39:13,510 --> 00:39:17,770
the code and so it's just easier for me
1215
00:39:14,769 --> 00:39:19,059
to get a foothold now when you do write
1216
00:39:17,769 --> 00:39:21,130
functions which are hundreds if not
1217
00:39:19,059 --> 00:39:22,449
thousands of lines long you still should
1218
00:39:21,130 --> 00:39:24,220
keep in mind general guidelines about
1219
00:39:22,449 --> 00:39:25,899
code readability basic things like not
1220
00:39:24,219 --> 00:39:27,730
straying too far from the left margin
1221
00:39:25,900 --> 00:39:29,079
for too far or for too long you know you
1222
00:39:27,730 --> 00:39:31,000
don't want to have code that's indented
1223
00:39:29,079 --> 00:39:32,259
in eight levels because it gets really
1224
00:39:31,000 --> 00:39:33,670
obnoxious to scrolling up and down code
1225
00:39:32,260 --> 00:39:35,950
if you have to scroll over for one thing
1226
00:39:33,670 --> 00:39:37,720
and also it tends to just imply there's
1227
00:39:35,949 --> 00:39:40,449
a lot of busy logic in this part of code
1228
00:39:37,719 --> 00:39:41,739
and it gets confusing so likewise you
1229
00:39:40,449 --> 00:39:43,210
also need to look out for parts of
1230
00:39:41,739 --> 00:39:45,309
functions where the logics is getting
1231
00:39:43,210 --> 00:39:46,900
too complex the first thing to do is of
1232
00:39:45,309 --> 00:39:48,279
course to try and simplify your logic
1233
00:39:46,900 --> 00:39:49,960
but failing that there are going to
1234
00:39:48,280 --> 00:39:51,250
cases where hey we should just split
1235
00:39:49,960 --> 00:39:52,510
this off into a separate function so
1236
00:39:51,250 --> 00:39:55,869
it's more neatly self-contained
1237
00:39:52,510 --> 00:39:57,550
complexity the other concern with longer
1238
00:39:55,869 --> 00:39:59,380
functions is that as your function gets
1239
00:39:57,550 --> 00:40:00,970
longer and longer you tend to accrue
1240
00:39:59,380 --> 00:40:02,710
more and more local variables
1241
00:40:00,969 --> 00:40:04,029
so what you want to do is hopefully a
1242
00:40:02,710 --> 00:40:05,530
language allows us you want to try and
1243
00:40:04,030 --> 00:40:07,240
constrain the scope of the local
1244
00:40:05,530 --> 00:40:08,530
variables so that they don't exist for
1245
00:40:07,239 --> 00:40:10,809
the full duration of the function but
1246
00:40:08,530 --> 00:40:12,220
rather for subsections this way either
1247
00:40:10,809 --> 00:40:13,838
reader of your code when they scan up
1248
00:40:12,219 --> 00:40:15,219
and down the function I don't have to
1249
00:40:13,838 --> 00:40:17,588
think about all the variables for the
1250
00:40:15,219 --> 00:40:18,608
whole duration of the function the way
1251
00:40:17,588 --> 00:40:20,108
this is done the most curly brace
1252
00:40:18,608 --> 00:40:22,480
languages is you can just introduce a
1253
00:40:20,108 --> 00:40:24,250
new sub scope with curly braces so here
1254
00:40:22,480 --> 00:40:26,679
for example this integer X variable only
1255
00:40:24,250 --> 00:40:28,358
exists within these curly braces when
1256
00:40:26,679 --> 00:40:29,949
you have subsections of a function which
1257
00:40:28,358 --> 00:40:31,960
you are commenting it's generally a good
1258
00:40:29,949 --> 00:40:34,059
idea to when in doubt enclose them in
1259
00:40:31,960 --> 00:40:35,740
curly braces this gives readers of the
1260
00:40:34,059 --> 00:40:38,108
function and assurance that variables
1261
00:40:35,739 --> 00:40:40,029
from the preceding sections don't fall
1262
00:40:38,108 --> 00:40:41,348
through to the following sections and so
1263
00:40:40,030 --> 00:40:44,160
in later sections we don't have to think
1264
00:40:41,349 --> 00:40:46,720
about the variables that were used above
1265
00:40:44,159 --> 00:40:49,029
where possible the even better thing to
1266
00:40:46,719 --> 00:40:50,919
do is to enclose these local scopes in
1267
00:40:49,030 --> 00:40:53,140
their own anonymous function that's then
1268
00:40:50,920 --> 00:40:54,789
just immediately called an advantage
1269
00:40:53,139 --> 00:40:56,828
here within the nested function is that
1270
00:40:54,789 --> 00:40:58,179
it's not just its own sub scope but also
1271
00:40:56,829 --> 00:41:00,250
within this anonymous function it's
1272
00:40:58,179 --> 00:41:01,719
guaranteed then any return is not going
1273
00:41:00,250 --> 00:41:03,880
to return out of the enclosing function
1274
00:41:01,719 --> 00:41:05,559
it'll return just out of this enclosed
1275
00:41:03,880 --> 00:41:07,059
function and so we have a stronger
1276
00:41:05,559 --> 00:41:09,068
guarantee that the logic of this
1277
00:41:07,059 --> 00:41:11,920
subsection is self-contained from the
1278
00:41:09,068 --> 00:41:12,909
enclosing function unfortunately what I
1279
00:41:11,920 --> 00:41:14,619
often really want
1280
00:41:12,909 --> 00:41:16,088
when creating subsections of longer
1281
00:41:14,619 --> 00:41:18,220
functions is a feature that doesn't
1282
00:41:16,088 --> 00:41:19,539
exist in any language I know of it's an
1283
00:41:18,219 --> 00:41:21,189
idea I've only seen in one other place
1284
00:41:19,539 --> 00:41:23,019
it was Jonathan Blow and this talks
1285
00:41:21,190 --> 00:41:24,849
about his argument language these making
1286
00:41:23,019 --> 00:41:27,219
and the idea is that we want something
1287
00:41:24,849 --> 00:41:30,220
like an anonymous function which doesn't
1288
00:41:27,219 --> 00:41:32,139
see anything of its enclosing scope the
1289
00:41:30,219 --> 00:41:34,299
virtue of extracting a section of code
1290
00:41:32,139 --> 00:41:35,588
out to a truly separate function is that
1291
00:41:34,300 --> 00:41:37,030
everything that comes into the function
1292
00:41:35,588 --> 00:41:39,549
has to be explicitly passed to a
1293
00:41:37,030 --> 00:41:41,560
parameter it would be great if we could
1294
00:41:39,550 --> 00:41:43,720
write inline anonymous functions with
1295
00:41:41,559 --> 00:41:45,279
the same virtue specifically what I
1296
00:41:43,719 --> 00:41:47,500
would propose is imagine we had a
1297
00:41:45,280 --> 00:41:49,780
reserved word use that introduces a
1298
00:41:47,500 --> 00:41:51,818
block and in the header of the eu's we
1299
00:41:49,780 --> 00:41:53,890
list variables from the enclosing scope
1300
00:41:51,818 --> 00:41:55,838
which we want to be accessible in this
1301
00:41:53,889 --> 00:41:57,429
block but otherwise anything from the
1302
00:41:55,838 --> 00:41:59,500
enclosing scope would not be visible
1303
00:41:57,429 --> 00:42:01,419
these listed variables however would
1304
00:41:59,500 --> 00:42:03,730
really actually be copies of those
1305
00:42:01,420 --> 00:42:05,500
variables so if you assign to X or Y
1306
00:42:03,730 --> 00:42:07,599
here in the scope you're assigning to x
1307
00:42:05,500 --> 00:42:09,670
and y the local variables of this use
1308
00:42:07,599 --> 00:42:11,650
block not to X and y of the enclosing
1309
00:42:09,670 --> 00:42:13,358
scope which is the effect you get with a
1310
00:42:11,650 --> 00:42:14,349
truly separate function right you assign
1311
00:42:13,358 --> 00:42:15,969
to the parameters of
1312
00:42:14,349 --> 00:42:17,230
function you're not modifying what was
1313
00:42:15,969 --> 00:42:18,969
passed to the function you're just
1314
00:42:17,230 --> 00:42:20,940
modifying those local variables that's
1315
00:42:18,969 --> 00:42:23,109
the same thing we want in this use block
1316
00:42:20,940 --> 00:42:25,450
furthermore a use block should itself
1317
00:42:23,110 --> 00:42:27,250
return values so you use return inside
1318
00:42:25,449 --> 00:42:28,779
the use block and it doesn't return from
1319
00:42:27,250 --> 00:42:30,340
the enclosing function it returns from
1320
00:42:28,780 --> 00:42:32,650
the use itself that uses an expression
1321
00:42:30,340 --> 00:42:34,740
and so we can return values from this
1322
00:42:32,650 --> 00:42:36,849
use and assignment to this variable a so
1323
00:42:34,739 --> 00:42:39,069
in effect we'd have this block of code
1324
00:42:36,849 --> 00:42:41,559
which is as neatly self-contained as a
1325
00:42:39,070 --> 00:42:43,630
separate function however it is written
1326
00:42:41,559 --> 00:42:45,250
in line and so it's very very clear that
1327
00:42:43,630 --> 00:42:47,260
oh this is a piece of code that's only
1328
00:42:45,250 --> 00:42:49,360
used in this one place you don't have to
1329
00:42:47,260 --> 00:42:50,380
go look for it elsewhere and also we
1330
00:42:49,360 --> 00:42:52,240
don't even have to give it a name
1331
00:42:50,380 --> 00:42:53,920
instead we can just put a section
1332
00:42:52,239 --> 00:42:55,419
comment header before the use block and
1333
00:42:53,920 --> 00:42:56,920
that is generally much better for
1334
00:42:55,420 --> 00:42:59,680
containing the actual intent of this
1335
00:42:56,920 --> 00:43:01,420
block of code if later down the line we
1336
00:42:59,679 --> 00:43:02,769
decide that this block of code actually
1337
00:43:01,420 --> 00:43:04,720
should be extracted to tone proper
1338
00:43:02,769 --> 00:43:06,340
function that's a very easy thing to do
1339
00:43:04,719 --> 00:43:08,139
you can have an editor convenience that
1340
00:43:06,340 --> 00:43:09,579
does that for you automatically it's
1341
00:43:08,139 --> 00:43:11,109
already clear what the parameters and
1342
00:43:09,579 --> 00:43:12,519
the arguments should be all the
1343
00:43:11,110 --> 00:43:15,789
programmer would have to do is provide a
1344
00:43:12,519 --> 00:43:16,900
name for the new function so anyway it
1345
00:43:15,789 --> 00:43:18,309
would be nice if language has had this
1346
00:43:16,900 --> 00:43:20,320
feature unfortunately I don't know of
1347
00:43:18,309 --> 00:43:22,090
anyone that does but regardless you
1348
00:43:20,320 --> 00:43:23,920
shouldn't be so scared of long functions
1349
00:43:22,090 --> 00:43:30,460
they actually have their place in most
1350
00:43:23,920 --> 00:43:32,710
code bases at the very
1351
00:43:30,460 --> 00:43:34,389
I hope I can get you to try procedural
1352
00:43:32,710 --> 00:43:35,650
programming it doesn't really matter
1353
00:43:34,389 --> 00:43:37,358
what language you're in if you're in
1354
00:43:35,650 --> 00:43:38,829
Java or C sharp you can write the
1355
00:43:37,358 --> 00:43:40,750
procedural code you can break the rules
1356
00:43:38,829 --> 00:43:42,730
but if you've ever felt any of the
1357
00:43:40,750 --> 00:43:44,530
paralysis that I felt attempting to do
1358
00:43:42,730 --> 00:43:46,358
object oriented programming properly to
1359
00:43:44,530 --> 00:43:47,950
square the circle I think you'll find
1360
00:43:46,358 --> 00:43:49,659
abandoning all those ideas and just
1361
00:43:47,949 --> 00:43:52,179
reverting the procedural code to be a
1362
00:43:49,659 --> 00:43:53,348
liberating experience I can tell you
1363
00:43:52,179 --> 00:43:54,819
from personal experience that having
1364
00:43:53,349 --> 00:43:56,349
read these books that you don't need to
1365
00:43:54,820 --> 00:43:57,460
read them they don't have answers
1366
00:43:56,349 --> 00:43:59,080
they're not going to square the circle
1367
00:43:57,460 --> 00:44:00,639
and you're going to waste productive
1368
00:43:59,079 --> 00:44:02,679
years of your life trying to live up to
1369
00:44:00,639 --> 00:44:04,118
their ideals I'm not saying there's
1370
00:44:02,679 --> 00:44:05,829
nothing to these ideas there are bits
1371
00:44:04,119 --> 00:44:07,210
and pieces that have value test-driven
1372
00:44:05,829 --> 00:44:08,619
development for example has some
1373
00:44:07,210 --> 00:44:10,720
interesting ideas there's value in
1374
00:44:08,619 --> 00:44:12,849
testing but that's part of the problem
1375
00:44:10,719 --> 00:44:15,098
is that kernels of good ideas have been
1376
00:44:12,849 --> 00:44:16,359
taken to holistic extremes in a way that
1377
00:44:15,099 --> 00:44:18,039
I think has been disastrous for the
1378
00:44:16,358 --> 00:44:20,348
industry and certainly for programming
1379
00:44:18,039 --> 00:44:22,150
education there are very few solid
1380
00:44:20,349 --> 00:44:24,369
holistic answers about how we should
1381
00:44:22,150 --> 00:44:27,809
write code we'd all be better off if we
1382
00:44:24,369 --> 00:44:27,809
stopped chasing that Chamara
1383
00:44:33,059 --> 00:44:35,119
you
106165
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.