Map versus Map
Clash 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?
apex
New contributor
add a comment |Â
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?
apex
New contributor
add a comment |Â
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?
apex
New contributor
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
apex
New contributor
New contributor
edited 49 mins ago
David Reed
20.9k31640
20.9k31640
New contributor
asked 51 mins ago
Dean
161
161
New contributor
New contributor
add a comment |Â
add a comment |Â
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.
add a comment |Â
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.
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
add a comment |Â
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.
add a comment |Â
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.
add a comment |Â
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.
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.
answered 18 mins ago
sfdcfox
231k10178393
231k10178393
add a comment |Â
add a comment |Â
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.
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
add a comment |Â
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.
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
add a comment |Â
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.
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.
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
add a comment |Â
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
add a comment |Â
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.
Dean is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password