1
00:00:00,490 --> 00:00:08,530
All right, so the second lesson is about simplifying your code and trying to always go on the simple

2
00:00:08,530 --> 00:00:13,000
route and also trying to avoid reinventing the wheel.

3
00:00:13,240 --> 00:00:14,460
This is very important.

4
00:00:14,830 --> 00:00:20,680
So we are hearing the restaurant's activity, as you can see, and we want to refactor something in

5
00:00:20,680 --> 00:00:26,800
the filter restaurant's method that we just extracted in the last class in the last video.

6
00:00:27,370 --> 00:00:27,900
All right.

7
00:00:27,910 --> 00:00:32,200
So if we look at this map, there is something that I really don't like.

8
00:00:32,200 --> 00:00:38,730
And I think that you don't like it either when the filtering of the restaurant is is actually done.

9
00:00:38,740 --> 00:00:45,720
As you can see here, there's the calculation of the distance between the user and the app in the apps,

10
00:00:45,760 --> 00:00:47,050
restaurants, of course.

11
00:00:47,380 --> 00:00:54,520
And you have all of this crazy logic with calculating, considering the radius of the earth and all

12
00:00:54,520 --> 00:00:54,910
that.

13
00:00:55,220 --> 00:00:58,080
And I guess that the logic is fine, right?

14
00:00:58,090 --> 00:00:59,410
I mean, it works.

15
00:00:59,560 --> 00:01:03,790
The only problem is that we don't really want to have this kind of logic.

16
00:01:03,790 --> 00:01:09,010
It seems that we are reinventing the wheel and we can just try to use something more simpler, something

17
00:01:09,010 --> 00:01:10,590
that comes out of the box.

18
00:01:10,960 --> 00:01:18,380
So if we go to Google and we just type in calculate distance between two locations on Android, get

19
00:01:18,390 --> 00:01:19,240
to these results.

20
00:01:19,240 --> 00:01:25,990
And I think that this one, if we look going here, we can see that we have here a pretty similar approach

21
00:01:25,990 --> 00:01:27,360
to what we have so far.

22
00:01:27,370 --> 00:01:28,330
It's pretty long.

23
00:01:28,840 --> 00:01:35,200
And as I said, calculating the distance between two points seems to be a task that has obviously been

24
00:01:35,200 --> 00:01:37,090
approached by somebody else until now.

25
00:01:37,300 --> 00:01:41,870
So there's no need for you to reinvent the wheel and create a custom implementation.

26
00:01:42,160 --> 00:01:44,650
You can always use something that is already done.

27
00:01:44,980 --> 00:01:50,480
And on the second answer here, I should the first answer there is something really cool.

28
00:01:50,480 --> 00:01:56,410
And this is actually somebody saying this, that there's no need to reinvent the wheel for such calculation

29
00:01:56,410 --> 00:01:57,910
between two locations.

30
00:01:58,240 --> 00:02:02,440
And there's an out of the box method here called location.

31
00:02:02,440 --> 00:02:05,560
The distance between that does exactly this.

32
00:02:05,980 --> 00:02:07,720
And it's pretty simple.

33
00:02:07,720 --> 00:02:09,190
It has three lines or something.

34
00:02:09,190 --> 00:02:10,390
Let's copy paste this.

35
00:02:10,510 --> 00:02:10,900
Right.

36
00:02:12,170 --> 00:02:18,800
And let's go back and delete all of this crazy code that we have, actually delete this part here and

37
00:02:19,100 --> 00:02:23,870
let's leave the log for now and delete this as well.

38
00:02:23,880 --> 00:02:25,000
We don't really need it.

39
00:02:25,340 --> 00:02:26,750
And let's go here.

40
00:02:27,590 --> 00:02:35,900
Let's paste what we have just gotten from the code and asking me that maybe he could convert it to.

41
00:02:36,530 --> 00:02:37,820
That would be very kind of you.

42
00:02:37,820 --> 00:02:38,670
Andrei's to you.

43
00:02:39,140 --> 00:02:40,100
Thanks for doing this.

44
00:02:40,280 --> 00:02:42,950
And it has already done this for me.

45
00:02:43,310 --> 00:02:50,120
It obviously asked me to import this, and I'm going to do that because I want to do this and look how

46
00:02:50,120 --> 00:02:50,870
cool this is.

47
00:02:50,870 --> 00:02:53,840
Also the return type here is not really what we want.

48
00:02:54,080 --> 00:02:57,250
So let's call this vault distance here.

49
00:02:58,400 --> 00:02:58,970
Sorry.

50
00:02:59,850 --> 00:03:03,150
Well, let's call this what this does and.

51
00:03:06,410 --> 00:03:08,990
Like this, all right.

52
00:03:09,830 --> 00:03:16,190
Actually, not distance, let's call this distance result here, and this is a result here, is going

53
00:03:16,190 --> 00:03:21,500
to take the actual result for from this distance between and I guess that we will receive this in meters.

54
00:03:21,510 --> 00:03:29,900
So let's divide it by one thousand just to make sure let's go back here and base the first latitude

55
00:03:29,900 --> 00:03:35,690
as the user latitude and longitude as the user longitude and do the same for the restaurant here.

56
00:03:35,720 --> 00:03:36,920
So let's go.

57
00:03:36,920 --> 00:03:41,120
And they feel to the restaurant door latitude.

58
00:03:42,320 --> 00:03:47,710
Actually it's first distance now is ducation.

59
00:03:48,410 --> 00:03:56,480
I got confused a bit here and select latitude and also the same thing we have to do for the longitude.

60
00:03:56,750 --> 00:04:02,300
Let's see here the same thing and select here longitude and perfect.

61
00:04:02,570 --> 00:04:08,180
Right now we have the calculation done as passing distance array, which is an array.

62
00:04:08,180 --> 00:04:13,150
And actually this map is going to change this distance array content.

63
00:04:13,160 --> 00:04:17,690
So when we are going to access the first element of the out of the distance array, we are going to

64
00:04:17,690 --> 00:04:18,740
get the actual result.

65
00:04:19,280 --> 00:04:26,510
So right now, let's also copy this here in this log and look how clean the actual logic of creating

66
00:04:26,510 --> 00:04:28,960
inculcating this distance is right now.

67
00:04:29,510 --> 00:04:33,260
Let's also make sure that the logic works fine on the map.

68
00:04:34,580 --> 00:04:34,930
All right.

69
00:04:34,940 --> 00:04:38,170
So the distance has been low, but we still forgot something.

70
00:04:38,180 --> 00:04:39,440
We have zero kilometers.

71
00:04:39,450 --> 00:04:44,750
So what we have to do is actually set this distance result to the filtered restaurant field field.

72
00:04:44,870 --> 00:04:51,230
So we'll go here and say distance equals distance result and that's much better.

73
00:04:51,830 --> 00:04:56,390
But we still have an error now telling us that, hey, we need a internat afloat.

74
00:04:56,400 --> 00:04:57,890
So let's go to entier.

75
00:04:57,890 --> 00:05:00,350
Just we don't really care about the accuracy of this.

76
00:05:04,170 --> 00:05:08,370
And right now, the distance has been calculated properly, so we have refactor the code.

77
00:05:09,060 --> 00:05:15,600
The app is working fine and we have cleaned the actual code of doing this on this crazy computation

78
00:05:15,600 --> 00:05:15,890
here.

79
00:05:15,900 --> 00:05:21,360
So the first thing to remember is that we should always take the easy route when there is an easy implementation

80
00:05:21,360 --> 00:05:22,950
that has been already done.

81
00:05:23,190 --> 00:05:24,270
Don't reinvent this.

82
00:05:24,750 --> 00:05:30,270
So before moving into the next lecture, let's do a bit of summary on what we just learned.

