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. Other Discussions
  3. The Insider News
  4. Avoiding primitive obsession in .NET

Avoiding primitive obsession in .NET

Scheduled Pinned Locked Moved The Insider News
csharpcsscomhelp
17 Posts 8 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.
  • K Kent Sharkey

    Telerik[^]:

    Avoiding the imperfection known as primitive obsession allows us to create high-quality, less error-prone code without much effort.

    For the non-cavepeople among us

    Richard DeemingR Offline
    Richard DeemingR Offline
    Richard Deeming
    wrote on last edited by
    #8

    Wrapping every primitive value in a class like that is going to dramatically increase the memory usage of the application, and potentially decrease the performance. After all, there's a reason Java has stack-allocated duplicates of certain heap-allocated classes. The author should really be using a struct for the wrappers - and preferably a record struct.


    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

    "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

    R 1 Reply Last reply
    0
    • L Lost User

      Sweet, Should we report [your comments](https://www.codeproject.com/Insider.aspx?fid=1658735&df=90&mpp=50&sort=Position&spc=Relaxed&select=5917537&tid=5915963) about Sanskrit and the Vedic scripture Bill? Edit: I'm just pointing out something I find hypocritical, if you check your post history you proclaimed your religion ages ago.

      B Offline
      B Offline
      BillWoodruff
      wrote on last edited by
      #9

      You can report anything you want; anything you feel violates CodeProject's ethos. There is a big difference between my comments on the history and linguistics of the Vedas and Sanskrit, on the Mahabharata as drama and metaphysical allegory, and a direct quote from a scripture used as a talking point. I very carefully avoided anything that showed disrespect for Hinduism the same way as I would carefully avoid showing disrespect for Christianity, Islam, etc. The quote (in Urdu) I used from Kabir is poetry. Glad to hear you read my posts !

      «The mind is not a vessel to be filled but a fire to be kindled» Plutarch

      L 1 Reply Last reply
      0
      • K Kent Sharkey

        Telerik[^]:

        Avoiding the imperfection known as primitive obsession allows us to create high-quality, less error-prone code without much effort.

        For the non-cavepeople among us

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

        What, no "stringly" typed data?

        1 Reply Last reply
        0
        • B BillWoodruff

          You can report anything you want; anything you feel violates CodeProject's ethos. There is a big difference between my comments on the history and linguistics of the Vedas and Sanskrit, on the Mahabharata as drama and metaphysical allegory, and a direct quote from a scripture used as a talking point. I very carefully avoided anything that showed disrespect for Hinduism the same way as I would carefully avoid showing disrespect for Christianity, Islam, etc. The quote (in Urdu) I used from Kabir is poetry. Glad to hear you read my posts !

          «The mind is not a vessel to be filled but a fire to be kindled» Plutarch

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #11

          Bill, It's probably just poetry. My way of expressing myself in Walcott-esque style. [THE LIGHT OF THE WORLD - Derek Walcott - Saint Lucia - Poetry International](https://www.poetryinternational.com/en/poets-poems/poems/poem/103-28371\_THE-LIGHT-OF-THE-WORLD) Read it to see if you can find that deeper meaning. Might have to read it three or four times. :thumbsup:

          1 Reply Last reply
          0
          • K Kent Sharkey

            Telerik[^]:

            Avoiding the imperfection known as primitive obsession allows us to create high-quality, less error-prone code without much effort.

            For the non-cavepeople among us

            M Offline
            M Offline
            Marc Clifton
            wrote on last edited by
            #12

            Yeah, it's called semantic types, and I wrote about this[^] in 2018. :rolleyes:

            Latest Article:
            Create a Digital Ocean Droplet for .NET Core Web API with a real SSL Certificate on a Domain

            1 Reply Last reply
            0
            • K Kent Sharkey

              Telerik[^]:

              Avoiding the imperfection known as primitive obsession allows us to create high-quality, less error-prone code without much effort.

              For the non-cavepeople among us

              E Offline
              E Offline
              englebart
              wrote on last edited by
              #13

              I thought the author’s “fixed” code was awful. Why leave email as a string? Boolean is the worst primitive for this, why did the author leave a boolean? I am trying to get a law passed that all booleans should be converted enums. If I see a class named ClubMember, I would never expect to call ClubMember.isMember(), ever. Maybe ClubMember.getMembershipStatus(). Oof!

              Richard DeemingR 1 Reply Last reply
              0
              • Richard DeemingR Richard Deeming

                Wrapping every primitive value in a class like that is going to dramatically increase the memory usage of the application, and potentially decrease the performance. After all, there's a reason Java has stack-allocated duplicates of certain heap-allocated classes. The author should really be using a struct for the wrappers - and preferably a record struct.


                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                R Offline
                R Offline
                Rob Grainger
                wrote on last edited by
                #14

                In a similar vein, the ClubMember type in particular made me wish (again) that C# supported enum types with methods.

                "If you don't fail at least 90 percent of the time, you're not aiming high enough." Alan Kay.

                Richard DeemingR 1 Reply Last reply
                0
                • R Rob Grainger

                  In a similar vein, the ClubMember type in particular made me wish (again) that C# supported enum types with methods.

                  "If you don't fail at least 90 percent of the time, you're not aiming high enough." Alan Kay.

                  Richard DeemingR Offline
                  Richard DeemingR Offline
                  Richard Deeming
                  wrote on last edited by
                  #15

                  Extension methods will get you close to that:

                  public enum ClubMember
                  {
                  NonMember,
                  Member,
                  }

                  public static class ClubMemberExtensions
                  {
                  public static decimal CalculateAmount(this ClubMember member, decimal productValue) => member switch
                  {
                  ClubMember.Member => productValue - 10,
                  _ => productValue,
                  };
                  }


                  "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                  "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                  1 Reply Last reply
                  0
                  • E englebart

                    I thought the author’s “fixed” code was awful. Why leave email as a string? Boolean is the worst primitive for this, why did the author leave a boolean? I am trying to get a law passed that all booleans should be converted enums. If I see a class named ClubMember, I would never expect to call ClubMember.isMember(), ever. Maybe ClubMember.getMembershipStatus(). Oof!

                    Richard DeemingR Offline
                    Richard DeemingR Offline
                    Richard Deeming
                    wrote on last edited by
                    #16

                    englebart wrote:

                    I am trying to get a law passed that all booleans should be converted enums.

                    That's easy:

                    public enum Bool
                    {
                    True,
                    False,
                    FileNotFound,
                    }

                    What Is Truth? - The Daily WTF[^]


                    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                    "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                    E 1 Reply Last reply
                    0
                    • Richard DeemingR Richard Deeming

                      englebart wrote:

                      I am trying to get a law passed that all booleans should be converted enums.

                      That's easy:

                      public enum Bool
                      {
                      True,
                      False,
                      FileNotFound,
                      }

                      What Is Truth? - The Daily WTF[^]


                      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                      E Offline
                      E Offline
                      englebart
                      wrote on last edited by
                      #17

                      Too funny. I used to visit that site, but their security was so bad that I thought I would pick up a virus from the comments. That very meme flashed through my mind while I was posting.

                      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