1
00:00:02,150 --> 00:00:02,983
<v Instructor>Let's continue</v>

2
00:00:02,983 --> 00:00:05,770
with these important principles, though,

3
00:00:05,770 --> 00:00:07,980
as mentioned before, we already covered

4
00:00:07,980 --> 00:00:09,910
the two most important ones

5
00:00:09,910 --> 00:00:12,490
when it comes to writing clean code.

6
00:00:12,490 --> 00:00:14,450
Nonetheless, let's next have a look

7
00:00:14,450 --> 00:00:18,010
at the interface segregation principle.

8
00:00:18,010 --> 00:00:20,610
What is this principle about?

9
00:00:20,610 --> 00:00:23,890
The interface segregation principles says

10
00:00:23,890 --> 00:00:28,260
that many client-specific interfaces are better

11
00:00:28,260 --> 00:00:30,950
than one general purpose interface.

12
00:00:30,950 --> 00:00:34,450
Now, again, as always, what does this mean?

13
00:00:34,450 --> 00:00:36,740
I got another example for you.

14
00:00:36,740 --> 00:00:38,200
I got a class SQLDatabase

15
00:00:39,070 --> 00:00:43,260
which implements an interface called Database.

16
00:00:43,260 --> 00:00:45,790
Interfaces are a common thing

17
00:00:45,790 --> 00:00:48,380
in object-oriented programming, of course.

18
00:00:48,380 --> 00:00:51,710
Interfaces are contracts which force classes

19
00:00:51,710 --> 00:00:53,820
to implement certain behaviors.

20
00:00:53,820 --> 00:00:56,660
In this case, we are forced to add a connect

21
00:00:56,660 --> 00:01:01,360
and store data method if we implement this interface.

22
00:01:01,360 --> 00:01:06,200
And the SQLDatabase does implement this interface.

23
00:01:06,200 --> 00:01:08,850
Now, everything looks all right here,

24
00:01:08,850 --> 00:01:12,467
but now let's say we don't just have the SQLDatabase,

25
00:01:13,650 --> 00:01:17,440
but instead, we also have is InMemoryDatabase,

26
00:01:17,440 --> 00:01:19,740
which, of course, also is a database,

27
00:01:19,740 --> 00:01:21,800
and therefore, it also makes sense

28
00:01:21,800 --> 00:01:24,573
that we implemented the Database interface.

29
00:01:25,700 --> 00:01:29,810
Hence, we are forced to add a connect and store data method.

30
00:01:29,810 --> 00:01:32,860
And now the problem is that it might make sense

31
00:01:32,860 --> 00:01:34,660
to have a stored data method

32
00:01:34,660 --> 00:01:38,060
because no matter if we are storing data in a SQLDatabase

33
00:01:38,060 --> 00:01:41,780
or in memory, we want to be able to store data

34
00:01:41,780 --> 00:01:43,180
at the end of the day,

35
00:01:43,180 --> 00:01:48,110
but connect, that does not necessarily make sense here.

36
00:01:48,110 --> 00:01:51,630
And InMemoryDatabase, which uses the local memory

37
00:01:51,630 --> 00:01:53,000
for storing data,

38
00:01:53,000 --> 00:01:56,113
doesn't really need to connect anywhere.

39
00:01:57,000 --> 00:02:00,530
So in the end, we have the wrong interface here.

40
00:02:00,530 --> 00:02:02,440
And if that kind of reminds you

41
00:02:02,440 --> 00:02:05,350
of the Liskov substitution principle we had a look at

42
00:02:05,350 --> 00:02:09,960
in the last lecture, that would actually not be a surprise

43
00:02:09,960 --> 00:02:11,610
because here we have the problem

44
00:02:11,610 --> 00:02:14,300
with modeling our data incorrectly

45
00:02:14,300 --> 00:02:17,240
and using the wrong base class in the end.

46
00:02:17,240 --> 00:02:20,500
Now with the interface segregation principle,

47
00:02:20,500 --> 00:02:23,450
we have the problem of having a wrong interface,

48
00:02:23,450 --> 00:02:26,070
which we force onto multiple classes,

49
00:02:26,070 --> 00:02:30,480
even if these classes are not using inheritance at all.

50
00:02:30,480 --> 00:02:33,440
The problem is that our database interface

51
00:02:33,440 --> 00:02:36,530
is a general purpose interface here.

52
00:02:36,530 --> 00:02:39,730
It's tries to cover too many use cases,

53
00:02:39,730 --> 00:02:42,793
connecting and storing data, to be precise.

54
00:02:43,690 --> 00:02:47,060
And we should avoid general purpose interfaces

55
00:02:47,060 --> 00:02:50,140
when following the interface segregation principle.

56
00:02:50,140 --> 00:02:51,610
Instead, we should have multiple,

57
00:02:51,610 --> 00:02:54,653
smaller clients-specific interfaces.

58
00:02:55,950 --> 00:02:59,560
For example, here we could have a database interface

59
00:02:59,560 --> 00:03:01,310
if we wanna keep that name.

60
00:03:01,310 --> 00:03:03,730
But in addition, we could have an interface

61
00:03:03,730 --> 00:03:05,593
which is called RemoteDatabase.

62
00:03:07,630 --> 00:03:11,410
And the connect method, forcing classes

63
00:03:11,410 --> 00:03:14,283
to have a connect method would go into RemoteDatabase.

64
00:03:16,178 --> 00:03:19,950
Then the SQLDatabase class could implement Database

65
00:03:19,950 --> 00:03:24,610
and RemoteDatabase, which in TypeScript, is done like this,

66
00:03:24,610 --> 00:03:27,870
simply by separating interfaces by a comma,

67
00:03:27,870 --> 00:03:31,260
and the InMemoryDatabase only implements

68
00:03:31,260 --> 00:03:35,503
the Database interface and not the RemoteDatabase interface.

69
00:03:36,420 --> 00:03:38,280
Hence, we can delete connect here.

70
00:03:38,280 --> 00:03:40,820
We're not forced to implement it anymore,

71
00:03:40,820 --> 00:03:42,530
and hence, we now have code

72
00:03:42,530 --> 00:03:46,193
which does follow the interface segregation principle.

73
00:03:47,300 --> 00:03:50,680
Now just as with the Liskov substitution principle,

74
00:03:50,680 --> 00:03:53,980
obviously the interface segregation principle

75
00:03:53,980 --> 00:03:55,680
is an important principle

76
00:03:55,680 --> 00:03:59,270
when it comes to writing maintainable and extensible code,

77
00:03:59,270 --> 00:04:01,920
when it comes to writing clean code.

78
00:04:01,920 --> 00:04:04,640
Where it, of course, matters that humans

79
00:04:04,640 --> 00:04:06,260
can read the code quite well,

80
00:04:06,260 --> 00:04:08,380
and that it's easy to understand,

81
00:04:08,380 --> 00:04:11,220
this rule doesn't help us too much.

82
00:04:11,220 --> 00:04:14,260
It also doesn't hurt, and it certainly helps a bit,

83
00:04:14,260 --> 00:04:16,243
but it's not super important.

