This has been something that has been bugging me and I realise that it might be a very subjective point.
I generally follow the return empty collections vs null approach. Is checking if a list is empty before streaming over it against streaming over it regardless since with the list being empty means that no operation will happen a moot point?
With the check there won't be any setup by JAVA that is required for setting up a stream that won't be needed but that feels like a micro optimisation that thankfully in this age we don't need to bother (I assume compilers do a lot of work themselves on that part) On the other hand I don't have to create an extra branch/indentation that might make the code more readable and easy to follow. In general I try to keep methods small but personally when I see indentation or check I naturally assume that I have to look for the other case. In a lot of case, streaming will also create a subset that also might need a check about something and might create a second indentation (inner branch).
I realise this might be subjective but I guess I wanted to get the opinion of other people as well, or I guess affirmation that it's better in cases like this to focus on code readability (especially for future developers after me)
Aucun commentaire:
Enregistrer un commentaire