about summary refs log tree commit diff
path: root/usth/ICT2.7/P4L5 Software Refactoring Subtitles/14 - Extract Method - lang_en_vs4.srt
blob: 88769e0f62180f173baf9aca211fb5e7e7716f69 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
1
00:00:00,060 --> 00:00:02,406
The next re-factoring which is also the last one that we'll

2
00:00:02,406 --> 00:00:05,640
see, extract method is one of the most commonly used re-factoring. As

3
00:00:05,640 --> 00:00:09,080
it is applicable in many, many situations. The starting point is

4
00:00:09,080 --> 00:00:12,720
a method that is too long and contains cohesive code fragments, that

5
00:00:12,720 --> 00:00:16,180
really serve a single very specific purpose. So we start from

6
00:00:16,180 --> 00:00:19,270
a cohesive code fragment in a large method. What we can do

7
00:00:19,270 --> 00:00:22,370
in this case, is to create a method using that code fragment.

8
00:00:22,370 --> 00:00:25,620
And then replacing the code fragment with a call to that method.

9
00:00:25,620 --> 00:00:28,390
Let's look at this with an example. Here over this method called

10
00:00:28,390 --> 00:00:31,070
print owing, and what it does, imagine that it does a lot

11
00:00:31,070 --> 00:00:33,850
of operations here that I'm just not listing, and then it's got

12
00:00:33,850 --> 00:00:37,620
a set of print statements. That are just printing a lot of details

13
00:00:37,620 --> 00:00:41,280
about the owing information. And then again, a lot of code after

14
00:00:41,280 --> 00:00:44,270
that. So what I could do in this case to simplify. The

15
00:00:44,270 --> 00:00:47,940
method is to transform this set of statements. They are cohesive in

16
00:00:47,940 --> 00:00:51,120
the sense that they do just one thing, they just print these details

17
00:00:51,120 --> 00:00:53,890
into a method, and then I had, replace the statements with a

18
00:00:53,890 --> 00:00:56,740
call to that method. Which is actually something similar to what we did

19
00:00:56,740 --> 00:00:59,900
as part of some the previous re-factoring's. Here I'm showing the result.

20
00:00:59,900 --> 00:01:02,770
So here is the method that I extracted. As you can see. It

21
00:01:02,770 --> 00:01:05,790
contains the code that was previously here. I give you the meaningful

22
00:01:05,790 --> 00:01:09,080
name, I called it printDetails so it's clear what it does. And now

23
00:01:09,080 --> 00:01:12,820
the print owning method is simpler. Because I still have the remaining code

24
00:01:12,820 --> 00:01:17,200
the one I didn't touch. But now this potentially long list of details.

25
00:01:17,200 --> 00:01:20,560
Of prints, of details is not replaced by a single method code.

26
00:01:20,560 --> 00:01:23,550
So a gain similar to the previous refactorings that we saw. If

27
00:01:23,550 --> 00:01:26,370
we just look at the printing method, it's very easy to figure

28
00:01:26,370 --> 00:01:29,490
out what this part does. Oh, print some details. And once more I

29
00:01:29,490 --> 00:01:33,060
really want to stress this. If you don't care about how, this

30
00:01:33,060 --> 00:01:36,350
is implemented and knowing that this print some details is enough. Then

31
00:01:36,350 --> 00:01:38,980
you're done. You don't need to understand anything more. It's clear, it's

32
00:01:38,980 --> 00:01:42,430
self explanatory. And if you'll need to look at what print details does,

33
00:01:42,430 --> 00:01:44,390
you just go and look at print details. And you look at

34
00:01:44,390 --> 00:01:47,280
it in isolation. So it's easier to understand what this does without having

35
00:01:47,280 --> 00:01:49,640
to think the rest of the code. So once more the color we

36
00:01:49,640 --> 00:01:52,740
factor in is just to improve your design, made the code more readable

37
00:01:52,740 --> 00:01:56,020
Make the code more maintainable. And also keep in mind all of these,

38
00:01:56,020 --> 00:01:59,080
are kind of small examples. You also always have to think about the

39
00:01:59,080 --> 00:02:02,560
effect that this can have on larger codebases. It can really improve a

40
00:02:02,560 --> 00:02:05,280
lot. The understandabililty, and maintainability of

41
00:02:05,280 --> 00:02:07,220
your code. So in general, it's design.