1

00:00:00,870  -->  00:00:07,770
Hi there non-structured a simple advice from Effective Java hit the item say is that if your method

2

00:00:07,860  -->  00:00:13,880
returns a collection ordinary and if the method does not have anything written then it triggered then

3

00:00:14,010  -->  00:00:19,110
an empty collection on an empty array but not a null value.

4

00:00:19,110  -->  00:00:21,660
For instance let's consider this code fragment.

5

00:00:21,960  -->  00:00:23,220
Let's assume that this method.

6

00:00:23,280  -->  00:00:29,170
So just for titles given the author mean he takes an array of the book titles.

7

00:00:29,540  -->  00:00:36,390
Now as you can see it fetches the data from the database by walking a similar method in the Davola.

8

00:00:36,510  -->  00:00:40,230
Now if not books were fun then this method returns a null value.

9

00:00:40,230  -->  00:00:47,550
And this kind of practice is already common knowledge say that this is the client code that is invoking

10

00:00:47,550  -->  00:00:48,810
ometer.

11

00:00:49,110  -->  00:00:50,410
Once that bind gets done.

12

00:00:50,490  -->  00:00:56,790
It performs a null check and only if this check succeeds it will check if the R.A.F. titles contain

13

00:00:56,790  -->  00:00:58,840
some target data.

14

00:00:59,630  -->  00:01:05,150
Now the problem with this implementation is that book client as well as the method have to write some

15

00:01:05,190  -->  00:01:12,690
external court as emphasized here in Redfern Now that this sort of implementation there is a chance

16

00:01:12,690  -->  00:01:18,990
that the client programmer might sometimes forget to perform the null check and there is a possibility

17

00:01:19,050  -->  00:01:22,790
that this would not have any effect for a long long time.

18

00:01:22,890  -->  00:01:28,830
But whenever a null is return we of course would end up with a null pointer exception.

19

00:01:29,100  -->  00:01:36,240
So the advice from this idea is to return an empty array or an empty collection then a valid argument

20

00:01:36,260  -->  00:01:42,420
would be that wouldn't it be expensive to create an empty array or an empty collection every single

21

00:01:42,420  -->  00:01:44,170
time the search fails.

22

00:01:44,280  -->  00:01:45,260
There is no date.

23

00:01:45,290  -->  00:01:51,870
I found this the counter argument is that just do not worry about performance for this sort of thing

24

00:01:52,620  -->  00:01:58,410
unless you're really sure that retaining an empty array on an empty collection is the real source of

25

00:01:58,410  -->  00:02:00,360
your performance problem.

26

00:02:00,370  -->  00:02:02,360
Your profiler would indicate that.

27

00:02:02,760  -->  00:02:09,810
So this item from effective Djala wants us not to optimize every single thing it instead advises us

28

00:02:09,930  -->  00:02:15,600
to write good programs with just good design principles like information hiding.

29

00:02:15,630  -->  00:02:20,100
Now once the design is good improving speed is a lot easier to do.

30

00:02:20,120  -->  00:02:21,280
Take a look at this item.

31

00:02:21,300  -->  00:02:28,440
I can 55 which a simple and includes some very popular software engineering quotations.

32

00:02:28,440  -->  00:02:34,500
But if performance is an issue you can still return the same instance of an employee a or the same instance

33

00:02:34,500  -->  00:02:36,300
of an empty collection.

34

00:02:36,540  -->  00:02:39,280
That is you do not have to create a new empty object.

35

00:02:39,300  -->  00:02:43,930
Each single time and there are some idioms for doing it.

36

00:02:44,100  -->  00:02:50,220
And here is an idiom for returning the same empty array for my collection here we are creating an empty

37

00:02:50,230  -->  00:02:54,300
are the only ones outside of them added on within the method.

38

00:02:54,420  -->  00:02:57,210
Once we get the list of titles from there it is.

39

00:02:57,450  -->  00:03:02,400
We simply invoke the two or method to return an array of items.

40

00:03:02,460  -->  00:03:05,850
You may recall that two array is from the collection interface.

41

00:03:06,030  -->  00:03:10,070
That is what you would use to convert a collection into an art.

42

00:03:10,500  -->  00:03:16,500
You would use Oslo's method from the collections class to convert an arcane playlist.

43

00:03:16,680  -->  00:03:18,500
Right we saw that in the collections.

44

00:03:18,510  -->  00:03:20,600
I'm not here.

45

00:03:20,640  -->  00:03:27,060
If some titled softphone then the method would internally create a new array and would fill it with

46

00:03:27,060  -->  00:03:29,790
that items and would return that ID.

47

00:03:30,150  -->  00:03:33,190
But if not items are found then the two are method.

48

00:03:33,240  -->  00:03:40,340
In this particular case I would simply return the input MTI And as mentioned earlier that input empty

49

00:03:40,340  -->  00:03:46,990
array is created only once as it is defined as static and Feinman.

50

00:03:47,340  -->  00:03:54,960
Generally if the collection has some data then the method always returns the input with the data.

51

00:03:54,960  -->  00:03:59,720
If the input array size is at least as large as the collection size.

52

00:04:00,120  -->  00:04:01,790
Only if that is not the case.

53

00:04:01,800  -->  00:04:04,470
It would create a new ID.

54

00:04:05,260  -->  00:04:12,270
And here is the idiom for always returning the same M-B immutable collection we affecting the list of

55

00:04:12,280  -->  00:04:13,840
titles from that database.

56

00:04:13,950  -->  00:04:20,520
And if this list is empty then we simply invoke the method empty list in the Collections class Collections

57

00:04:20,670  -->  00:04:27,050
class would ensure that it would always return the same empty list instance which is immutable.

58

00:04:27,420  -->  00:04:32,410
That is you cannot add anything to the return list earlier in the collections demo.

59

00:04:32,640  -->  00:04:38,190
I mentioned about the methods starting with the method name empty and also said that they will look

60

00:04:38,190  -->  00:04:44,090
at them and this lesson so empty This is one of them and there are also other similar methods like.

61

00:04:44,100  -->  00:04:46,710
Empty Set an empty map.

62

00:04:46,770  -->  00:04:50,420
Also when it comes to checking whether a collection is empty or not.

63

00:04:50,580  -->  00:04:53,550
Use the easy empty method as we see here.

64

00:04:54,030  -->  00:05:01,830
As opposed to invoking the seismometer uncompelling read 0 ism B is much more readable and it also internally

65

00:05:01,830  -->  00:05:06,090
in the size method and makes it a similar comparison.

66

00:05:06,090  -->  00:05:07,590
So that's about it.

67

00:05:07,590  -->  00:05:14,520
If your method returns either an array or collection rate under MP motions rather than returning a null

68

00:05:14,520  -->  00:05:17,450
value when there is no date to return.

69

00:05:17,460  -->  00:05:17,910
Thank you
