So when I wrote that blog post, I will admit I got a little academic in solutions. A really basic thought was to simply stop providing componentN() methods by default for data class, or at least let us opt out. I will give you that it makes 100% sense for Tuple, Pair, heck even List should such a day come. But most data classes by normal non library user are not really intended to be based on position.
On the topic of constructors, I will give you that positional constructor has the exact same problem. The only reason it isn’t as big of a problem is really just our experience, and because every argument is required. We have lived with positional arguments long enough to know not to do that. I don’t think that really makes it a good thing to do.
In your example you show a Person class. I will give you that it looks harmless, but I think we do inexperienced developers a disservice by letting them make mistakes unknowingly. In Person you could easily need to change “name” to “first” and "last. Once again depending on the destructring call, you can have errors.
val (name, _) = personCreatedFromSomeEntirelyDifferentPartOfTheCodeBase // wrong
Heck maybe this is even easier then we think.
data class could continue to exist and we just remove
class to opt out, use a different name like
struct, or something else. I don’t know. The point being, we all like Kotlin more then Java for a lot of reasons, safety being one of them. I don’t want there to ever be a book called “Effective Kotlin” which is basically a book entirely dedicated to informing the user where the language lets you make easy mistakes.
Take for example, Effective Javas Item 9 Always override hashCode when you override equals, Kotlin data classes don’t really have this problem.
Java Concurrency in Practice 3.2.1 Safe Constructor Practices, Kotlin actually helps in this regard by making secondary constructors harder to get to. Granted, it doesn’t fix the problem, but I’ll take it over nothing. (Full disclosure I was an idiot and did this in many programs before I read this book )