Map versus Map

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
3
down vote

favorite












Looking at legacy code. The developer is loading Account.ID and a Lookup field value. I would think the best map would be Map<Id,Id> but this was coded as Map<string,string>. The value can be NULL.



Any reason/benefit to declaring it with strings versus IDs?










share|improve this question









New contributor




Dean is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

























    up vote
    3
    down vote

    favorite












    Looking at legacy code. The developer is loading Account.ID and a Lookup field value. I would think the best map would be Map<Id,Id> but this was coded as Map<string,string>. The value can be NULL.



    Any reason/benefit to declaring it with strings versus IDs?










    share|improve this question









    New contributor




    Dean is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.





















      up vote
      3
      down vote

      favorite









      up vote
      3
      down vote

      favorite











      Looking at legacy code. The developer is loading Account.ID and a Lookup field value. I would think the best map would be Map<Id,Id> but this was coded as Map<string,string>. The value can be NULL.



      Any reason/benefit to declaring it with strings versus IDs?










      share|improve this question









      New contributor




      Dean is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      Looking at legacy code. The developer is loading Account.ID and a Lookup field value. I would think the best map would be Map<Id,Id> but this was coded as Map<string,string>. The value can be NULL.



      Any reason/benefit to declaring it with strings versus IDs?







      apex






      share|improve this question









      New contributor




      Dean is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      Dean is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 49 mins ago









      David Reed

      20.9k31640




      20.9k31640






      New contributor




      Dean is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 51 mins ago









      Dean

      161




      161




      New contributor




      Dean is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Dean is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Dean is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          4
          down vote













          Using Map<Id, Id> over Map<String, String> has at least two benefits I can think of.



          Clarity



          The more restricted type identifies that this isn't just a map of string-string key-value pairs, it is specifically a map of id-id key-value pairs. An experienced programmer, in particular, will immediately understand that we're making a link between records via their ID values. The string-string version leaves ambiguity if used improperly, so one should prefer the id-id version when practical (and, alternatively, Map<Id, String> and Map<String, Id>, if that's the intended use case).



          Runtime Conversion



          Sometimes we have to deal with 15-character and 18-character Id values. Mix them up in a map with a string key and you'll introduce logic bugs, since 001000000012345 is not the same as 001000000012345AAA. Using the correct key removes the possibility of a false negative when checking if a key is present or getting the associated value.




          In summary, using the correct data type helps make the code self-documenting and introduces clarity in to the code, especially for the next person who's unfamiliar with the code (possibly including you a year from now). In some edge cases, it can also prevent false negatives by coercing the data into the correct format, either by the compiler or at runtime, and logic bugs can be easily identified when the correct type is used (e.g. if you try to put a non-ID into an Id key, you'll get an exception).



          In any case, there is no benefit for using Map<String, String> over Map<Id, Id> when the situation calls for using Id values. Instead, it only risks the possibility of logic bugs and confusion.






          share|improve this answer



























            up vote
            2
            down vote













            I'd say there is no benefit, and that it is worse to store an Id in a String1.



            Using an Id gives you a compile-time check that you are, in fact, using something that could possibly be an Id



            • "0010000000ATest" can currently be, and is likely, the Id for someone's account

            • "0010RandomATest" looks like an Id at first glance, but cannot be an Id at time of writing due to this answer on the Salesforce Id format (thanks sfdcfox for pointing me to that link)

            An Id also gives you access to the getSObjetType() method, which can come in handy if/when you start abstracting logic to a point where you're dealing with collections of SObjects (as opposed to a List<Account> or a Map<Id, Case>).



            Map<Id, Id> is the right choice here, though a Map<String, String> isn't the end of the world.



            1: Such situations should be rare. I have run into a situation where I couldn't avoid storing an Id as a String. This was when I needed to "relate" an SObject to OpportunityLineItem, which we cannot have lookup or master-detail relationships to.






            share|improve this answer


















            • 1




              Actually, the sixth position is currently "reserved" and must always be 0. It's a simple check, but can definitely help identify if you're trying to abuse an arbitrary string as an Id. There's another answer here that goes in to more details. TMYK.
              – sfdcfox
              11 mins ago










            • @sfdcfox Today I learned.
              – Derek F
              8 mins ago










            Your Answer







            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "459"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );






            Dean is a new contributor. Be nice, and check out our Code of Conduct.









             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f234792%2fmapid-id-versus-mapstring-string%23new-answer', 'question_page');

            );

            Post as a guest






























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            4
            down vote













            Using Map<Id, Id> over Map<String, String> has at least two benefits I can think of.



            Clarity



            The more restricted type identifies that this isn't just a map of string-string key-value pairs, it is specifically a map of id-id key-value pairs. An experienced programmer, in particular, will immediately understand that we're making a link between records via their ID values. The string-string version leaves ambiguity if used improperly, so one should prefer the id-id version when practical (and, alternatively, Map<Id, String> and Map<String, Id>, if that's the intended use case).



            Runtime Conversion



            Sometimes we have to deal with 15-character and 18-character Id values. Mix them up in a map with a string key and you'll introduce logic bugs, since 001000000012345 is not the same as 001000000012345AAA. Using the correct key removes the possibility of a false negative when checking if a key is present or getting the associated value.




            In summary, using the correct data type helps make the code self-documenting and introduces clarity in to the code, especially for the next person who's unfamiliar with the code (possibly including you a year from now). In some edge cases, it can also prevent false negatives by coercing the data into the correct format, either by the compiler or at runtime, and logic bugs can be easily identified when the correct type is used (e.g. if you try to put a non-ID into an Id key, you'll get an exception).



            In any case, there is no benefit for using Map<String, String> over Map<Id, Id> when the situation calls for using Id values. Instead, it only risks the possibility of logic bugs and confusion.






            share|improve this answer
























              up vote
              4
              down vote













              Using Map<Id, Id> over Map<String, String> has at least two benefits I can think of.



              Clarity



              The more restricted type identifies that this isn't just a map of string-string key-value pairs, it is specifically a map of id-id key-value pairs. An experienced programmer, in particular, will immediately understand that we're making a link between records via their ID values. The string-string version leaves ambiguity if used improperly, so one should prefer the id-id version when practical (and, alternatively, Map<Id, String> and Map<String, Id>, if that's the intended use case).



              Runtime Conversion



              Sometimes we have to deal with 15-character and 18-character Id values. Mix them up in a map with a string key and you'll introduce logic bugs, since 001000000012345 is not the same as 001000000012345AAA. Using the correct key removes the possibility of a false negative when checking if a key is present or getting the associated value.




              In summary, using the correct data type helps make the code self-documenting and introduces clarity in to the code, especially for the next person who's unfamiliar with the code (possibly including you a year from now). In some edge cases, it can also prevent false negatives by coercing the data into the correct format, either by the compiler or at runtime, and logic bugs can be easily identified when the correct type is used (e.g. if you try to put a non-ID into an Id key, you'll get an exception).



              In any case, there is no benefit for using Map<String, String> over Map<Id, Id> when the situation calls for using Id values. Instead, it only risks the possibility of logic bugs and confusion.






              share|improve this answer






















                up vote
                4
                down vote










                up vote
                4
                down vote









                Using Map<Id, Id> over Map<String, String> has at least two benefits I can think of.



                Clarity



                The more restricted type identifies that this isn't just a map of string-string key-value pairs, it is specifically a map of id-id key-value pairs. An experienced programmer, in particular, will immediately understand that we're making a link between records via their ID values. The string-string version leaves ambiguity if used improperly, so one should prefer the id-id version when practical (and, alternatively, Map<Id, String> and Map<String, Id>, if that's the intended use case).



                Runtime Conversion



                Sometimes we have to deal with 15-character and 18-character Id values. Mix them up in a map with a string key and you'll introduce logic bugs, since 001000000012345 is not the same as 001000000012345AAA. Using the correct key removes the possibility of a false negative when checking if a key is present or getting the associated value.




                In summary, using the correct data type helps make the code self-documenting and introduces clarity in to the code, especially for the next person who's unfamiliar with the code (possibly including you a year from now). In some edge cases, it can also prevent false negatives by coercing the data into the correct format, either by the compiler or at runtime, and logic bugs can be easily identified when the correct type is used (e.g. if you try to put a non-ID into an Id key, you'll get an exception).



                In any case, there is no benefit for using Map<String, String> over Map<Id, Id> when the situation calls for using Id values. Instead, it only risks the possibility of logic bugs and confusion.






                share|improve this answer












                Using Map<Id, Id> over Map<String, String> has at least two benefits I can think of.



                Clarity



                The more restricted type identifies that this isn't just a map of string-string key-value pairs, it is specifically a map of id-id key-value pairs. An experienced programmer, in particular, will immediately understand that we're making a link between records via their ID values. The string-string version leaves ambiguity if used improperly, so one should prefer the id-id version when practical (and, alternatively, Map<Id, String> and Map<String, Id>, if that's the intended use case).



                Runtime Conversion



                Sometimes we have to deal with 15-character and 18-character Id values. Mix them up in a map with a string key and you'll introduce logic bugs, since 001000000012345 is not the same as 001000000012345AAA. Using the correct key removes the possibility of a false negative when checking if a key is present or getting the associated value.




                In summary, using the correct data type helps make the code self-documenting and introduces clarity in to the code, especially for the next person who's unfamiliar with the code (possibly including you a year from now). In some edge cases, it can also prevent false negatives by coercing the data into the correct format, either by the compiler or at runtime, and logic bugs can be easily identified when the correct type is used (e.g. if you try to put a non-ID into an Id key, you'll get an exception).



                In any case, there is no benefit for using Map<String, String> over Map<Id, Id> when the situation calls for using Id values. Instead, it only risks the possibility of logic bugs and confusion.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 18 mins ago









                sfdcfox

                231k10178393




                231k10178393






















                    up vote
                    2
                    down vote













                    I'd say there is no benefit, and that it is worse to store an Id in a String1.



                    Using an Id gives you a compile-time check that you are, in fact, using something that could possibly be an Id



                    • "0010000000ATest" can currently be, and is likely, the Id for someone's account

                    • "0010RandomATest" looks like an Id at first glance, but cannot be an Id at time of writing due to this answer on the Salesforce Id format (thanks sfdcfox for pointing me to that link)

                    An Id also gives you access to the getSObjetType() method, which can come in handy if/when you start abstracting logic to a point where you're dealing with collections of SObjects (as opposed to a List<Account> or a Map<Id, Case>).



                    Map<Id, Id> is the right choice here, though a Map<String, String> isn't the end of the world.



                    1: Such situations should be rare. I have run into a situation where I couldn't avoid storing an Id as a String. This was when I needed to "relate" an SObject to OpportunityLineItem, which we cannot have lookup or master-detail relationships to.






                    share|improve this answer


















                    • 1




                      Actually, the sixth position is currently "reserved" and must always be 0. It's a simple check, but can definitely help identify if you're trying to abuse an arbitrary string as an Id. There's another answer here that goes in to more details. TMYK.
                      – sfdcfox
                      11 mins ago










                    • @sfdcfox Today I learned.
                      – Derek F
                      8 mins ago














                    up vote
                    2
                    down vote













                    I'd say there is no benefit, and that it is worse to store an Id in a String1.



                    Using an Id gives you a compile-time check that you are, in fact, using something that could possibly be an Id



                    • "0010000000ATest" can currently be, and is likely, the Id for someone's account

                    • "0010RandomATest" looks like an Id at first glance, but cannot be an Id at time of writing due to this answer on the Salesforce Id format (thanks sfdcfox for pointing me to that link)

                    An Id also gives you access to the getSObjetType() method, which can come in handy if/when you start abstracting logic to a point where you're dealing with collections of SObjects (as opposed to a List<Account> or a Map<Id, Case>).



                    Map<Id, Id> is the right choice here, though a Map<String, String> isn't the end of the world.



                    1: Such situations should be rare. I have run into a situation where I couldn't avoid storing an Id as a String. This was when I needed to "relate" an SObject to OpportunityLineItem, which we cannot have lookup or master-detail relationships to.






                    share|improve this answer


















                    • 1




                      Actually, the sixth position is currently "reserved" and must always be 0. It's a simple check, but can definitely help identify if you're trying to abuse an arbitrary string as an Id. There's another answer here that goes in to more details. TMYK.
                      – sfdcfox
                      11 mins ago










                    • @sfdcfox Today I learned.
                      – Derek F
                      8 mins ago












                    up vote
                    2
                    down vote










                    up vote
                    2
                    down vote









                    I'd say there is no benefit, and that it is worse to store an Id in a String1.



                    Using an Id gives you a compile-time check that you are, in fact, using something that could possibly be an Id



                    • "0010000000ATest" can currently be, and is likely, the Id for someone's account

                    • "0010RandomATest" looks like an Id at first glance, but cannot be an Id at time of writing due to this answer on the Salesforce Id format (thanks sfdcfox for pointing me to that link)

                    An Id also gives you access to the getSObjetType() method, which can come in handy if/when you start abstracting logic to a point where you're dealing with collections of SObjects (as opposed to a List<Account> or a Map<Id, Case>).



                    Map<Id, Id> is the right choice here, though a Map<String, String> isn't the end of the world.



                    1: Such situations should be rare. I have run into a situation where I couldn't avoid storing an Id as a String. This was when I needed to "relate" an SObject to OpportunityLineItem, which we cannot have lookup or master-detail relationships to.






                    share|improve this answer














                    I'd say there is no benefit, and that it is worse to store an Id in a String1.



                    Using an Id gives you a compile-time check that you are, in fact, using something that could possibly be an Id



                    • "0010000000ATest" can currently be, and is likely, the Id for someone's account

                    • "0010RandomATest" looks like an Id at first glance, but cannot be an Id at time of writing due to this answer on the Salesforce Id format (thanks sfdcfox for pointing me to that link)

                    An Id also gives you access to the getSObjetType() method, which can come in handy if/when you start abstracting logic to a point where you're dealing with collections of SObjects (as opposed to a List<Account> or a Map<Id, Case>).



                    Map<Id, Id> is the right choice here, though a Map<String, String> isn't the end of the world.



                    1: Such situations should be rare. I have run into a situation where I couldn't avoid storing an Id as a String. This was when I needed to "relate" an SObject to OpportunityLineItem, which we cannot have lookup or master-detail relationships to.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited 5 mins ago

























                    answered 14 mins ago









                    Derek F

                    17.9k31545




                    17.9k31545







                    • 1




                      Actually, the sixth position is currently "reserved" and must always be 0. It's a simple check, but can definitely help identify if you're trying to abuse an arbitrary string as an Id. There's another answer here that goes in to more details. TMYK.
                      – sfdcfox
                      11 mins ago










                    • @sfdcfox Today I learned.
                      – Derek F
                      8 mins ago












                    • 1




                      Actually, the sixth position is currently "reserved" and must always be 0. It's a simple check, but can definitely help identify if you're trying to abuse an arbitrary string as an Id. There's another answer here that goes in to more details. TMYK.
                      – sfdcfox
                      11 mins ago










                    • @sfdcfox Today I learned.
                      – Derek F
                      8 mins ago







                    1




                    1




                    Actually, the sixth position is currently "reserved" and must always be 0. It's a simple check, but can definitely help identify if you're trying to abuse an arbitrary string as an Id. There's another answer here that goes in to more details. TMYK.
                    – sfdcfox
                    11 mins ago




                    Actually, the sixth position is currently "reserved" and must always be 0. It's a simple check, but can definitely help identify if you're trying to abuse an arbitrary string as an Id. There's another answer here that goes in to more details. TMYK.
                    – sfdcfox
                    11 mins ago












                    @sfdcfox Today I learned.
                    – Derek F
                    8 mins ago




                    @sfdcfox Today I learned.
                    – Derek F
                    8 mins ago










                    Dean is a new contributor. Be nice, and check out our Code of Conduct.









                     

                    draft saved


                    draft discarded


















                    Dean is a new contributor. Be nice, and check out our Code of Conduct.












                    Dean is a new contributor. Be nice, and check out our Code of Conduct.











                    Dean is a new contributor. Be nice, and check out our Code of Conduct.













                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f234792%2fmapid-id-versus-mapstring-string%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Comments

                    Popular posts from this blog

                    Long meetings (6-7 hours a day): Being “babysat” by supervisor

                    Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

                    Confectionery