1
00:00:06,800 --> 00:00:07,310
Hello.

2
00:00:08,330 --> 00:00:11,870
In this video, we're going to find the answer to the question for.

3
00:00:12,800 --> 00:00:14,600
What is the spaghetti code?

4
00:00:15,170 --> 00:00:22,880
Spaghetti code is a pejorative term used to describe cold that is messy, tangled and hard to maintain.

5
00:00:23,630 --> 00:00:27,980
You can imagine such code as a bowl of spaghetti with all pasta.

6
00:00:27,980 --> 00:00:35,330
Ferraz being untangles together, making it very hard to say where one starts and word you ever erns.

7
00:00:35,960 --> 00:00:40,040
It would be hard to follow a single friend without moving the others.

8
00:00:40,550 --> 00:00:47,360
And it is as hard to make any change in the spaghetti code without affecting many other areas of code.

9
00:00:48,020 --> 00:00:51,920
There are plenty of issues that make cold spaghetti code.

10
00:00:52,160 --> 00:00:54,200
So let's just list a couple of them.

11
00:00:54,770 --> 00:01:03,860
Huge grasses and mustard's multiple responsibilities in one class using the go to cute, but naming

12
00:01:04,490 --> 00:01:07,430
magic numbers and cold repetitions.

13
00:01:08,240 --> 00:01:12,200
You can learn more about those topics in the form of lectures.

14
00:01:13,430 --> 00:01:17,750
Let's see some spaghetti code and then let's try to refactor it.

15
00:01:18,470 --> 00:01:22,070
Imagine you were told to implement such an interface.

16
00:01:24,030 --> 00:01:30,510
The class implementing such an interface should calculate the roots of a quadratic function that is

17
00:01:30,510 --> 00:01:33,630
defined by a user via console interaction.

18
00:01:34,230 --> 00:01:39,120
This interface is not very good since the calculate method returns void.

19
00:01:39,600 --> 00:01:45,420
It forces the implementer to mix reading from the console with actual words calculation.

20
00:01:46,050 --> 00:01:50,040
But let's just leave it as it is for the purpose of the exercise.

21
00:01:50,970 --> 00:01:53,460
Before we start, just a quick reminder.

22
00:01:54,210 --> 00:02:01,080
The quadratic function looks like this, and this is how you calculate the roots of a quadratic function.

23
00:02:02,760 --> 00:02:08,760
Now let's see spaghetti code that actually implements the interface we defined before.

24
00:02:09,450 --> 00:02:12,240
Brace yourself as this is going to be ugly.

25
00:02:15,040 --> 00:02:15,730
All right.

26
00:02:16,960 --> 00:02:19,480
It may take a while to read it all.

27
00:02:20,020 --> 00:02:24,340
So I recommend checking out the repository I linked to this course.

28
00:02:24,940 --> 00:02:27,040
Let's just take a quick look at this graph.

29
00:02:27,550 --> 00:02:29,410
First of all, it's quite big.

30
00:02:30,340 --> 00:02:33,670
This file is over 100 lines long.

31
00:02:34,480 --> 00:02:37,450
The plenty of things here that are just terrible.

32
00:02:38,230 --> 00:02:44,470
Not only is this court hard to read and understand, but it is also a breeding ground for bugs.

33
00:02:45,250 --> 00:02:51,310
Also, if we need to make other changes in this code, it would be laborious and error prone.

34
00:02:52,090 --> 00:02:56,200
Let me point out some of the most outstanding problems in this code.

35
00:02:56,710 --> 00:03:00,220
First of all, one class is doing everything.

36
00:03:00,910 --> 00:03:02,590
There is a lot of bad naming.

37
00:03:02,740 --> 00:03:08,050
For example, this variable also there are unnecessary comments.

38
00:03:08,560 --> 00:03:14,800
Instead of this comment, we should simply rename discoverable to is finished and remove the comment.

39
00:03:15,490 --> 00:03:17,740
There are a lot of cold repetitions.

40
00:03:18,940 --> 00:03:25,510
For example, this code is repeated three times for each of the A, B and C variables.

41
00:03:26,080 --> 00:03:32,740
Also, there are even more unnecessary comments since the code itself says that we are reading a here.

42
00:03:33,250 --> 00:03:34,720
No need to add a comment.

43
00:03:37,660 --> 00:03:44,680
The formatting is inconsistent and just about, for example, in this part of the code, those bracers

44
00:03:44,680 --> 00:03:52,540
are a new line here, but in the same line here, also, there is a lot of redundant calculations.

45
00:03:53,080 --> 00:03:57,910
Like, for example, here we're trying to parse string to double twice.

46
00:03:59,170 --> 00:03:59,790
All right.

47
00:03:59,830 --> 00:04:04,390
I think it might be an interesting exercise for you to try to refactor this code.

48
00:04:05,080 --> 00:04:06,730
Let me show you how I did it.

49
00:04:09,530 --> 00:04:13,940
So this is the main course, the one that actually implements the interface.

50
00:04:14,720 --> 00:04:17,510
As you can see, this class is much smaller now.

51
00:04:18,440 --> 00:04:24,260
I've moved the operations of reading from the council to our separate class called Council Reader.

52
00:04:25,190 --> 00:04:32,030
I've also created Muffie Utilities class that only deals with the actual calculation of the square function

53
00:04:32,030 --> 00:04:32,510
routes.

54
00:04:33,110 --> 00:04:38,960
Since this calculation produces a result that may have zero one or two numbers within.

55
00:04:39,290 --> 00:04:45,020
I also created a special class quadratic function roots to represent such a result.

56
00:04:45,770 --> 00:04:47,150
Let's see those classes.

57
00:04:47,630 --> 00:04:49,370
First, the console reader.

58
00:04:53,440 --> 00:04:58,870
As you can see, the only purpose of this class is to provide functions for reading from the console.

59
00:04:59,320 --> 00:05:06,610
I created a metal trick double that given a variable name, asks the user to enter this variable and

60
00:05:06,610 --> 00:05:08,770
then tries to pass it to Darbo.

61
00:05:09,280 --> 00:05:17,770
I've also added a similar method, but for reading books, it's just asking to type Y or N. and passes

62
00:05:17,770 --> 00:05:19,330
it to true or false.

63
00:05:20,320 --> 00:05:22,720
Now let's see the Matthew 286.

64
00:05:27,020 --> 00:05:29,120
As you can see, this class is really simple.

65
00:05:29,690 --> 00:05:36,140
The only thing it does is a pure mathematical calculation, given a quadratic function formula.

66
00:05:36,230 --> 00:05:38,270
It calculates its function rules.

67
00:05:38,720 --> 00:05:42,020
As we said, there can be zero one or two routes.

68
00:05:42,560 --> 00:05:45,350
I created a special type to represent that.

69
00:05:46,250 --> 00:05:46,940
Let's see it.

70
00:05:50,480 --> 00:05:58,910
This glass can hold zero one or two results within it exposes the first root and second root properties,

71
00:05:59,270 --> 00:06:02,780
which throw exceptions if an invalid operation executed.

72
00:06:03,380 --> 00:06:09,170
For example, if the programmer tries to redo the second root of a result, that only has one root.

73
00:06:09,830 --> 00:06:15,320
Of course, we also have wayang properties to check how many roots there are.

74
00:06:16,160 --> 00:06:18,140
And here we have the constructors.

75
00:06:18,890 --> 00:06:20,690
As you can see, this is pretty simple.

76
00:06:22,100 --> 00:06:23,630
Let's summarize what happened.

77
00:06:24,380 --> 00:06:28,550
The code got divided into smaller, more cohesive classes.

78
00:06:29,150 --> 00:06:31,000
They are now called repetitions now.

79
00:06:31,540 --> 00:06:32,780
Naming is better.

80
00:06:33,020 --> 00:06:35,030
And the code style is consistent.

81
00:06:35,720 --> 00:06:38,990
No comments are added since no comments are needed.

82
00:06:39,530 --> 00:06:41,570
The code speaks for itself.

83
00:06:42,680 --> 00:06:43,940
Is this code perfect?

84
00:06:44,270 --> 00:06:45,500
Of course not.

85
00:06:45,860 --> 00:06:47,090
No code is perfect.

86
00:06:47,210 --> 00:06:48,050
That's for sure.

87
00:06:48,860 --> 00:06:53,840
I see a couple of things that could be improved, which I skipped for brevity sake.

88
00:06:54,800 --> 00:06:58,700
First, the console reader should not be a static class.

89
00:06:59,120 --> 00:07:03,500
It should rather implement an interface like a user input reader.

90
00:07:04,250 --> 00:07:10,700
This way, we could easily charge the implementation to, for example, reading user input from a firm

91
00:07:10,700 --> 00:07:12,200
in a desktop application.

92
00:07:12,830 --> 00:07:19,130
Also, this class has a very specific implementation that would probably have to be generalized.

93
00:07:19,400 --> 00:07:22,700
One more muttons for reading various types would be added.

94
00:07:24,080 --> 00:07:28,160
Also, writing to the console should not be done with console runtime.

95
00:07:28,910 --> 00:07:32,480
This way, this class is starting coupled with the console class.

96
00:07:33,140 --> 00:07:36,860
What if we decide to change the way of communicating with the user?

97
00:07:37,610 --> 00:07:42,260
This should be, again, some interface like a user message writer.

98
00:07:43,310 --> 00:07:48,320
You may argue that this code is actually longer than it was before refactoring.

99
00:07:48,840 --> 00:07:49,820
Is it about Fik?

100
00:07:50,300 --> 00:07:51,410
No, it isn't.

101
00:07:52,130 --> 00:07:57,920
Called refactoring naturally leads to creating more classis methods and data structures.

102
00:07:58,580 --> 00:08:05,600
Also, please note that I added a brand new entity for representing the roots of the quadratic function.

103
00:08:06,200 --> 00:08:12,950
It allowed me to have clean, well named and simple to use operations on the not so simple result of

104
00:08:12,950 --> 00:08:17,300
quadratic function roots calculation in a real life project.

105
00:08:17,480 --> 00:08:23,150
We could have thousands of places where quadratic functions would be used, and having a well-defined

106
00:08:23,150 --> 00:08:26,150
class to represent its result would be critical.

107
00:08:26,870 --> 00:08:35,180
Lock-Out is not an issue tangoed messy and readable and complicated code is don't be afraid to make

108
00:08:35,180 --> 00:08:37,550
your code longer during the refactoring.

109
00:08:38,120 --> 00:08:43,640
You will still spend less time working on this code than you would spend working on the code.

110
00:08:43,640 --> 00:08:44,600
That is shorter.

111
00:08:44,630 --> 00:08:45,470
But Agler.

112
00:08:46,400 --> 00:08:48,020
All right, let's summarize.

113
00:08:48,590 --> 00:08:56,000
The spaghetti code is a pejorative term used to describe code that is messy, tangled and hard to maintain.

114
00:08:56,600 --> 00:09:03,320
There are actually more pasta related metaphors used to describe, but gold and you might be asked about

115
00:09:03,320 --> 00:09:04,700
them during the interview.

116
00:09:05,330 --> 00:09:08,420
For example, what is the ravioli code?

117
00:09:08,930 --> 00:09:15,530
The ravioli code is a term that describes code, that contains classes that are easy to understand in

118
00:09:15,530 --> 00:09:16,400
isolation.

119
00:09:16,730 --> 00:09:22,640
But interactions between them and the project as a whole are not or might be.

120
00:09:22,670 --> 00:09:24,440
What is the lasagna code?

121
00:09:25,430 --> 00:09:32,150
The lasagna code is a code whose layers are complicated and so are the interactions between them.

122
00:09:32,690 --> 00:09:37,100
Making a change in one of their heavily affects other layers.

123
00:09:38,360 --> 00:09:38,960
All right.

124
00:09:39,650 --> 00:09:41,390
I hope I didn't make you hungry.

125
00:09:41,900 --> 00:09:44,910
Thanks for watching this video and see you in the next one.

126
00:09:44,930 --> 00:09:48,080
When we start, the important topic of the design putters.

