26 Replies
look at the doc for the records, you don't need all that stuff
record HumanCouple(Human first, Human second);
thats a primary constructor?
ok hold on
are these equivalent?
More or less
You can even look into ValueTuples, you don’t need to declare a Record
@Many Nights. if you would really like to you can get even fancier with the record
if you wanted lol
Why use a Record over a ValueTuple for simple data storage/move?
Not trying to argue against it, seems like extra steps to do something as basic as return two objects at the same time from a method.
It's more strongly typed, and it's just plain easier to use when it needs to be passed around
It's easier to make a method that takes a
Couple
as a parameter, rather than a (Human first, Human second)
It's also more descriptive
After all, (Human first, Human second)
could refer to mortal enemies, besties, parents, organ donor and the patient who got it, etc
Couple
or HumanCouple
immediately tells you what it isSo it adds readability
Now, you can (or will be able to?) alias tuples with names
But it's only on a per-file level, IIRC
Yep, adds readability
Nothing technical
No, it's not like there's a performance benefit or something
Not that I know of, at least
What if you had a ValueTuple of
(Person husband, Person wife) GetCouple()
?Yeah, that is more understandable
But then you have to take care to keep naming it like that
Because names for tuple elements don't matter, IIRC
That is right
So this will be assignable to a variable of type
(Person justADude, Person mortalEnemy)
The benefit is that you don’t have to write up a Record, but at the expense that data flow can get lost in the weeds easier
Benefit of Record is that there’s stricter data flow so it’s harder for intent to get lost
But you do have to create a new Record everytime you want to use one
That's not very 2023 of you.
So is using JS for backend but people still do always analyze technical benefits first, abstract benefits come second. But if the abstract benefits outweigh the technical
Then that choice wins
Making a record takes like 2 seconds though.
Making a single record takes 2 second
You think an app will only have 1 record in its lifetime?
I dunno about you but I try to keep my codebase surface area as small as possible, less areas to debug.
Then you could argue in favour of never using classes, just tuples and extension methods on them
You could, but I’m not dense enough to know that not using classes would be better
Use it if it makes sense to
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.