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