Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. Nondestructive mutation: tell me about a time

Nondestructive mutation: tell me about a time

Scheduled Pinned Locked Moved The Lounge
csharpcomquestion
26 Posts 13 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P PIEBALDconsult

    Yes indeed. You have to know what the feature is doing in the background and what the limitations are. One example (not a new one of course) is the foreach loop -- in many cases, using a for loop is a better choice (when possible). I use a foreach loop only when there is no other option. I avoid Linq because it frequently does things which I can do better. As to C# 6 in particular: String Interpolation just isn't anything I would ever use. Earlier this year I wrote some code which does something similar though, taking some cues from it. C# 3 added Object and Collection Initializers (which are good), then C# 6 added Dictionary Initializers, which are really not any better and have no benefit over them as far as I can tell. A lot of these features make it easier for an unskilled developer to get something working, but at the cost of performance or flexibility.

    Mike HankeyM Offline
    Mike HankeyM Offline
    Mike Hankey
    wrote on last edited by
    #17

    The more that goes on behind the scenes the more that ccan go wrong and the harder to debug.

    I don't think before I open my mouth, I like to be as surprised a everyone else. PartsBin an Electronics Part Organizer - Release Version 1.1.0 JaxCoder.com Latest Article: Simon Says, A Child's Game

    1 Reply Last reply
    0
    • R raddevus

      Just reading up on the new C# record keyword and nondestructive mutation[^] and wondering: When was a time you used this concept? Anyone? Anyone...? Bueller...? Bueller...? Just curious.

      Graeme_GrantG Offline
      Graeme_GrantG Offline
      Graeme_Grant
      wrote on last edited by
      #18

      I find them useful as DTOs or for simple POCOs. This should be of interest to you: What are record types in C# and how they ACTUALLY work - YouTube[^] (I've skipped past his usual course ad) or jump forward to What records are behind the scenes[^]

      Graeme


      "I fear not the man who has practiced ten thousand kicks one time, but I fear the man that has practiced one kick ten thousand times!" - Bruce Lee

      R 1 Reply Last reply
      0
      • R raddevus

        Just reading up on the new C# record keyword and nondestructive mutation[^] and wondering: When was a time you used this concept? Anyone? Anyone...? Bueller...? Bueller...? Just curious.

        D Offline
        D Offline
        Davyd McColl
        wrote on last edited by
        #19

        often, because a lot of common bugs can be traced back to unexpected mutations so if you send me an object and I don't know where it comes from, and I need a modified version of that to do something else, I'll make a copy first - I might use `.DeepClone()` from [NuGet Gallery | PeanutButter.Utils 3.0.167](https://www.nuget.org/packages/PeanutButter.Utils) followed by some (again, from that lib) `.With(...)` statements, eg ``` var foo = yourObject.DeepClone() .With(o => o.Name = o.Name.ToUpper()); ```

        ------------------------------------------------ If you say that getting the money is the most important thing You will spend your life completely wasting your time You will be doing things you don't like doing In order to go on living That is, to go on doing things you don't like doing Which is stupid. - Alan Watts https://www.youtube.com/watch?v=-gXTZM\_uPMY

        R 1 Reply Last reply
        0
        • Graeme_GrantG Graeme_Grant

          I find them useful as DTOs or for simple POCOs. This should be of interest to you: What are record types in C# and how they ACTUALLY work - YouTube[^] (I've skipped past his usual course ad) or jump forward to What records are behind the scenes[^]

          Graeme


          "I fear not the man who has practiced ten thousand kicks one time, but I fear the man that has practiced one kick ten thousand times!" - Bruce Lee

          R Offline
          R Offline
          raddevus
          wrote on last edited by
          #20

          Graeme_Grant wrote:

          I find them useful as DTOs or for simple POCOs.

          That makes a lot of sense to me. I will check out the video. Thanks

          Graeme_GrantG 1 Reply Last reply
          0
          • D Davyd McColl

            often, because a lot of common bugs can be traced back to unexpected mutations so if you send me an object and I don't know where it comes from, and I need a modified version of that to do something else, I'll make a copy first - I might use `.DeepClone()` from [NuGet Gallery | PeanutButter.Utils 3.0.167](https://www.nuget.org/packages/PeanutButter.Utils) followed by some (again, from that lib) `.With(...)` statements, eg ``` var foo = yourObject.DeepClone() .With(o => o.Name = o.Name.ToUpper()); ```

            ------------------------------------------------ If you say that getting the money is the most important thing You will spend your life completely wasting your time You will be doing things you don't like doing In order to go on living That is, to go on doing things you don't like doing Which is stupid. - Alan Watts https://www.youtube.com/watch?v=-gXTZM\_uPMY

            R Offline
            R Offline
            raddevus
            wrote on last edited by
            #21

            That is a great example that I understand better now because I watched the video: Coding Shorts: For The Record - Why You Should Use (Records in C#) - YouTube[^] Shawn Wildemuth (who is a great trainer and has created some top videos on PluralSight)

            1 Reply Last reply
            0
            • R raddevus

              Graeme_Grant wrote:

              I find them useful as DTOs or for simple POCOs.

              That makes a lot of sense to me. I will check out the video. Thanks

              Graeme_GrantG Offline
              Graeme_GrantG Offline
              Graeme_Grant
              wrote on last edited by
              #22

              raddevus wrote:

              I will check out the video. Thanks

              Nick is very good. He deep dives and is very thorough.

              Graeme


              "I fear not the man who has practiced ten thousand kicks one time, but I fear the man that has practiced one kick ten thousand times!" - Bruce Lee

              1 Reply Last reply
              0
              • R raddevus

                Just reading up on the new C# record keyword and nondestructive mutation[^] and wondering: When was a time you used this concept? Anyone? Anyone...? Bueller...? Bueller...? Just curious.

                O Offline
                O Offline
                obermd
                wrote on last edited by
                #23

                I use this all the time. The naming is a little odd, but the concept is very simple: Copy an object and then change just those fields and properties you need to change. Seriously, I'm not sure this is useful as syntactic sugar except in very limited cases.

                1 Reply Last reply
                0
                • R raddevus

                  Just reading up on the new C# record keyword and nondestructive mutation[^] and wondering: When was a time you used this concept? Anyone? Anyone...? Bueller...? Bueller...? Just curious.

                  J Offline
                  J Offline
                  jochance
                  wrote on last edited by
                  #24

                  It's excellent for data contracts in web services where the reception is generally out of the client's control and whatever comes in the response, that is the object, because that is what came and it should probably never be mutable. The bottom line is that record can help enforce SOLID adherence. Many POCOs in general should now probably be defined as records instead of class. They're great. The only drawback/beef I have with them is having to do hokey workarounds to use them while targetting .NET Standard (C# 9–Use Record types in .NET Standard 2.0[^]).

                  1 Reply Last reply
                  0
                  • R raddevus

                    Just reading up on the new C# record keyword and nondestructive mutation[^] and wondering: When was a time you used this concept? Anyone? Anyone...? Bueller...? Bueller...? Just curious.

                    U Offline
                    U Offline
                    User 11907673
                    wrote on last edited by
                    #25

                    Have I (to your point) occassionally has a use for this?...Yes. Is this overkill?...Yes. This is what you get when you open source things and can't think of a compelling reason to say "no". That being said there ARE some useful things that have come from the open sourcing. BTW, Top Level command NOT being one of them! :-)

                    1 Reply Last reply
                    0
                    • R raddevus

                      Just reading up on the new C# record keyword and nondestructive mutation[^] and wondering: When was a time you used this concept? Anyone? Anyone...? Bueller...? Bueller...? Just curious.

                      A Offline
                      A Offline
                      Andrei Bozantan
                      wrote on last edited by
                      #26

                      This feature is useful to avoid all kind of bugs related to the usage of mutable values with multiple ownership shared through references. Think about scenarios when a method 'foo' receives an object as an input parameter. The initial contract is that the object's fields are not changed inside 'foo' . In a big codebase there would be many places where 'foo' becomes used over time, with the semantics of not changing the fields of the input object. If at some point someone makes a change in 'foo' to mutate one of the fields, this could cause many non obvious problems. In practice, in complex codebases these problems might be difficult to detect in a code review. Immutable types and non destructive mutation provide a solution to better express this kind of semantic.

                      If you can't explain something to a six year old, you really don't understand it yourself. (Albert Einstein)

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups