too much de-structuring sweetness will hurt your tooth!
Featured on :
Destructuring is an easier way take out data from your objects. Basically
So, now you know another way to initial you variables ! Wow hurray! TheEnd !..
Well wait no! Don't use it just Yet. Destructuring thou comes in handy its has its own quirks and limitations lets go through them one by one.
Gotcha #1 : Missing component 🕵🏻♂️
Under the Hood! ⚙️
- Positional (for arrays and iterables)
- Name/Structural (for objects).
Gotcha #2 : how to do it in a non-data class? 🧐
So the second problem is related to the first one itself these extensions only exist in the data class, so in case of normal class you need to create these function yourself.
Gotcha #3 : Unpack All 📂
If we require second member of the object we still need to destructure all members object or atleast to the position we require
Gotcha #4 : Reusing/mutable variables 🔃
you can declare them "var" for reusability but cannot use them again for destructuring.
Gotcha #5 : No support for default values 🤲
default values not supported for default params while unpacking.
Gotcha #6: Major Deal Breaker ❌
Using positional data for representing a class, is not a good way for accessing its parameter, it doesn't provide any type/name check method to Android Studio/Lint to figure out if any refactoring is been done on the class and the points of destructing are also required to be refactored! i.e Ordering is part of the API contract! if you change your data class constructor arg reordering it will break.
That's all fokes 🐰🥕 ! See ya again in next post .. Happy Hacking 💻 .
Enjoy the article?
a clap is much appreciated if you enjoyed. No sign up or cost associated :)
Update#1 : now you know the limitations positional destructing possess, a valid question to ask is when? and where? should we use destructing, those details and a sneaky little trick that I figured out to overcome positional refactoring issues are shared in the post below. see you on the other side👽.