:rocket: Refactoring Smarter: Kotlin’s Vararg and ...
# feed
e
🚀 Refactoring Smarter: Kotlin’s Vararg and Spread Operator Simplified (This is my first article so please do share feedbacks) https://www.linkedin.com/pulse/refactoring-smarter-kotlins-vararg-spread-operator-simplified-r-saisc?utm_source=share&utm_medium=member_android&utm_campaign=share_via
k
Nice article, but it might be worth pointing out that passing
*list.toTypedArray()
as a vararg causes the list to be copied into a newly allocated array. This can waste CPU time and memory resources if you are doing this in a tight loop in an environment where resources are very limited and/or where performance is extremely important. I prefer having an overloaded function instead:
Copy code
fun doSomething(vararg items: Item) = doSomething(items.asList())
fun doSomething(items: List<Item>) { ... }
Also, I don't see any problem with passing an empty argument list. If
doSomething
can take a vararg of any size, where is this "danger" that you talk of? If you pass nothing,
doSomething
behaves correctly by doing nothing.
❤️ 1
e
Hey, I Highly Appreciate your feedback, will definitely include this point in the article, the suggested method overloading does not work in some cases for example RoomDao(In Android) does not allow method overloading hence this was introduced in our code base. The empty parameters, if the developer forgets to pass the parameter the lint and compiler will be quiet, the developer might think he has used the function but doesn't check if the parameter is passed, this might cause issues if not tested or found at PR(it's a possibility for non conscious coders) ;) Thank you so much for the feedback, it's so hard to get good feedback nowadays people are too busy..