r/csharp • u/PatrickSmacchia • Oct 12 '20
C#9 records: immutable classes
https://blog.ndepend.com/c9-records-immutable-classes/
118
Upvotes
r/csharp • u/PatrickSmacchia • Oct 12 '20
9
u/form_d_k Ṭakes things too var Oct 12 '20 edited Oct 13 '20
Wow. I didn't realize records were such a controversial addition. I also don't get this hyper obsession with performance. There are plenty of situations where that's going to be near the bottom of your concerns.
Consider the following scenario:
1
You're working on a game with dozens, maybe hundreds of people on the team; you don't know because when you were cross with facilities about them removing all the fluorescents, you got accused of being against the new energy saving initiative. Now you swim in a malevolent ocean of darkness that on some very late nights when you find yourself alone in the office, you swear is actively trying to consume you.
2
The team that preceded you inherited an engine that is older than OOP, when source repositories were stacks of 8-inch floppies, and it looked as if Jefferson Starship was going to take over the world. One year ago they bequeathed upon the company this nightmare of broken, undocumented GOTO spaghetti & anti-patterns. You're convinced this was their sadistic revenge for all getting fired post-acquisition.
3
Management denied your request to get headcount for an additional technical artist, but helpfully supplied you with an overly nervous intern. After several weeks working alongside them, you're beginning to suspect they're pursuing something other than a liberal arts degree.
4
Despite the many getting started guides you spent countless evenings writing, the endless brownbags nobody attended, and the daily dozen emails you forward to oppressively inquisitive artists comprised of a single passive-aggressive sentence suggesting they scroll down to the part that begins
FW: FW: FW: FW: FW: FW: FW: WE BROKE TOOL NEED WORKAROUND ASAP <eom>...Yes, despite all of that, the engineering team still spent days tracking down why the game kept crashing with
Error 107221: У вас ошибкаafter re-re-re-re-re-throwing anex_exceptionbecause it couldn't (and should never) load an 8K-textured floor mat.5
Despite your many attempts to politely excuse yourself, one blissfully unaware artist exhausts 48 minutes of your lunch break explaining how the Pitchfork review for the latest "dope slab" of this TikTok-Instagram-naphouse artist you never heard of was just sooooo unfair.
And then in their hurry to finish up & catch the 2:30 PM bus home, they forget to toggle Compress To CXIFF (Custom Extended Interchange File Format), set the Compression slider 5/6ths of the way between
-3&-2, look to their left, look to their right, click Export As .MA 0.9.3alpha7, and make absolutely, positively, 100% SURE not to be working in prod. And THAT is how the game explodicated.6
You know better than anyone the intermediate file format that the main game loop passes to
Game.dll, memory mapping it as a reverse top-middle Endian binary structure.You know for 381 of the parameter fields what the 2-7 character naming scheme probably means.
YOU know which 147 fields always have to be included, but with a null value, and that the field
ah_xlutmust ALWAYS be set to0unless it's Thursday, in which case that blackbox from hell requires its internal string equivalent:TRUE.YOU know that those two tech artists & one rapidly aging intern that report to you would totally overhaul tooling so artists would never "happen" again, but there just aren't enough winters, springs, summers, falls, July 4ths, Christmas breaks, Presidents Days, and wedding anniversaries in a year to properly do so.
7
If you could just find the time between morning standups, after lunch standups, watersprint post-mortems, Milbert's daily wasting of an hour at your desk trying to convince you engineering should just rebuild the engine from the ground up in JavaScript & React, & HR's mandatory EKG Monitor job satisfaction surveys, you might be able to get at least some desperately-needed tooling done.
And so you do. A blurry evening or two here. A 3:00 AM there. Sometimes just a solitary lunch hour.
Your dog no longer recognizes you.
You miss your wife calling to say she's finally cleaning out the hall closet and if you want to keep this box of old cards & something in plastic that says Underground Sea Beta 9.8 Grade, you better call her back immediately.
And your Aunt Midge, who doesn't understand how SMS works, bombards you one evening:
your father is...no longer with us...they found him...1 week ago...in an abandoned Piggly Wiggly...by an old culvert...split up...he was then...laid to rest...sent to St. Peter's...and your father...he's in a better place now...don't worry...it's totally okay...we decided we will all go...up to the mountainYou call your sister in a panic and, after a tidal wave of confusion & soul-rending anxiety, learn it was just Hoboken Wireless sending the messages out of order. This causes you to rapidly cycle.
8
On your bipolar's upswing, you find yourself more productive than you've ever been. Your mind is aglow with whirling, transient nodes of thought careening through a cosmic vapor of invention. It's like your brain is on 200mg of pure grade Adderall.
Your fingers ablaze with records, clean inheritance, beautiful pattern matching, bountiful expression syntax, aircraft carriers of green text that generate the most outstanding CHM for an internal tool the world has ever seen. Readable. PERFECTLY SOLID.
And after much labor, you gaze upon the GUI of your opus magnum with the kind of pride you imagine one would feel if they hadn't missed the birth of their son. Clean, customer-grade WPF; tooltips for every control; sanity checks left & right; support for plugins & light scripting. It's even integrated with source control!
THOSE GODDAMNED ARTISTS CAN'T FAIL. YOUR PIPELINE TOOL WON'T LET THEM.
All they have to do is drag content into the application window, select an options template or use the one your tool suggested based on content analysis, change a few options, click
Export, and wait for 3-5 minutes to generateGame.dll-compatible binary.Your optimism shines through the commit summary, your test plan carefree & giddy. With great anticipation, you wait for code review.
6
u/celluj34 Oct 12 '20
What the fuck is going on in this comment section? This place is a trainwreck.
11
u/[deleted] Oct 12 '20
Could anyone share with me a good simple usecase for records where there aren't a better more flexible alternative? :)
5
u/ske66 Oct 12 '20
Could be totally totally wrong with this but ive recently been using react and redux. Redux talks a lot about state immutability and with MAUI being released soon, I wonder if record has been created to help build upon the MVU architecture and state immutabulity.
I could be totally wrong about this though, im still learning uses for immutability
2
u/[deleted] Oct 12 '20
What's the difference between a struct and a record? Heap allocation?
23
u/LloydAtkinson Oct 12 '20
Anyway, ignoring the technically incorrect and irrelevant boomer rant, really happy we are getting more functional features :)
-1
u/Buttsuit69 Oct 12 '20
Hm...I had hoped for more performance features but records sound nice too
0
u/[deleted] Oct 12 '20
[deleted] — view deleted comment