Maps - don't allow empty keys or raise a Compiler warning
- Andre
- PureBasic Team
- Posts: 2056
- Joined: Fri Apr 25, 2003 6:14 pm
- Location: Germany (Saxony, Deutscheinsiedel)
- Contact:
Maps - don't allow empty keys or raise a Compiler warning
The main advantage (in my point of view) of maps is, that you can easily store an item by using the 'key' parameter, and the other way around get the content of a specific element by calling it with the 'key'.
At least that's way I do.... for other uses or if I want to use the sorting commands of PB I'm using linked lists (which work without a key).
If I'm strictly using maps with a key, its elements can be called easily by a unique key.
But if there (by an accident) is a call of the map with an empty key (empty string or uninitialized string), this would add another map element (without my knowledge then). This could cause hard to find bugs in your program, for which you become aware at a later point...
I would vote to forbid map elements with an empty key (what's the use of a map then, compared to a linked list?), but at least a compiler warning should be added.
See examples and further discussion here: http://www.purebasic.fr/english/viewtop ... 2&start=21
Thank you
Further discussion is welcome, if you see other points about this topic.
At least that's way I do.... for other uses or if I want to use the sorting commands of PB I'm using linked lists (which work without a key).
If I'm strictly using maps with a key, its elements can be called easily by a unique key.
But if there (by an accident) is a call of the map with an empty key (empty string or uninitialized string), this would add another map element (without my knowledge then). This could cause hard to find bugs in your program, for which you become aware at a later point...
I would vote to forbid map elements with an empty key (what's the use of a map then, compared to a linked list?), but at least a compiler warning should be added.
See examples and further discussion here: http://www.purebasic.fr/english/viewtop ... 2&start=21
Thank you
Further discussion is welcome, if you see other points about this topic.
Re: Maps - don't allow empty keys or raise a Compiler warnin
many moons ago, i submitted a bug report, and also briefly mentioned this "issue". when fred fixed the bug, he also made maps no longer accept empty strings as the key name. but then freak and fred had a discussion, and he revoked the change. now in hindsight, im glad that he did. an empty string is still a string, and its perfectly valid to use it. in some cases you might want to use it as the root element. if you need to avoid this, then you should check if the string is empty, before actually using it. forcing this on every programmer, would only make things more difficult, for the ones that actually rely on it.
c ya,
nco2k
c ya,
nco2k
If OSVersion() = #PB_OS_Windows_ME : End : EndIf
Re: Maps - don't allow empty keys or raise a Compiler warnin
Both of You are right.
Maybe a flag in NewMap does the job?
Maybe a flag in NewMap does the job?
Re: Maps - don't allow empty keys or raise a Compiler warnin
There's nothing wrong with an empty key. Sanitize your inputs and throw and error there.
The only problem I ever have with maps is in Java where both the key and the value can be null. Eww.
The only problem I ever have with maps is in Java where both the key and the value can be null. Eww.
- Andre
- PureBasic Team
- Posts: 2056
- Joined: Fri Apr 25, 2003 6:14 pm
- Location: Germany (Saxony, Deutscheinsiedel)
- Contact:
Re: Maps - don't allow empty keys or raise a Compiler warnin
Thank you for your comments!
So there seems to be a reason to allow also empty/uninitialized strings as map keys. If so, ok!
And of course I can check the map key first, if it's a non-valid key (in my case, where I'm using unique ID numbers as key, all empty strings are forbidden). But I can avoid this "overhead", if there would be either
a) an optional parameter to NewMap to forbid such empty strings
b) a compiler warning so I would detect the problem during development.
And furthermore there should be some comments about such cases in the PB manual.
Maybe an implementation would be also possible together with introducing integer variables are possible map key (currently I'm always converting the integer ID numbers into strings to use them for the structured map...
So there seems to be a reason to allow also empty/uninitialized strings as map keys. If so, ok!
And of course I can check the map key first, if it's a non-valid key (in my case, where I'm using unique ID numbers as key, all empty strings are forbidden). But I can avoid this "overhead", if there would be either
a) an optional parameter to NewMap to forbid such empty strings
b) a compiler warning so I would detect the problem during development.
And furthermore there should be some comments about such cases in the PB manual.
Maybe an implementation would be also possible together with introducing integer variables are possible map key (currently I'm always converting the integer ID numbers into strings to use them for the structured map...
Re: Maps - don't allow empty keys or raise a Compiler warnin
I prefer PB's Maps accept all strings as keys and "" is a string.
Can you explain what error this option would prevent?
Can you explain what error this option would prevent?
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
- Andre
- PureBasic Team
- Posts: 2056
- Joined: Fri Apr 25, 2003 6:14 pm
- Location: Germany (Saxony, Deutscheinsiedel)
- Contact:
Re: Maps - don't allow empty keys or raise a Compiler warnin
It's no problem, if you and other people find the empty strings good as map keys. They shouldn't be forbidden then...skywalk wrote:I prefer PB's Maps accept all strings as keys and "" is a string.
Can you explain what error this option would prevent?
So we speak more about a compiler warning I think...
What my goal was/is: If I'm regularly using maps only with unique "Key ID numbers" (like "1", "2" and higher) there is no place for empty key strings. If I'm calling a map element (by accident of course) with an empty key string, the map was extended by this one element. I searched hours to find the bug in my code....
Of course I can check my key string (to not be empty) always before calling the map element, but this seems to be much overhead (performance loss). That's why I came up with this feature request - if this doesn't have any other disadvantages such an optional parameter to NewMap like
Code: Select all
NewMap Name.<Typ>([Slots [, #OnlyNonEmptyKeyStrings]])
Re: Maps - don't allow empty keys or raise a Compiler warnin
Can you delete the map element for key = "" before you begin, instead of checking for every instance?
Code: Select all
NewMap myMap()
myMap("") = 1
If FindMapElement(myMap(), "")
Debug MapSize(myMap())
Debug myMap("")
Else
Debug MapSize(myMap())
Debug myMap("")
EndIf
DeleteMapElement(myMap(), "")
If FindMapElement(myMap(), "")
; Skipped because deleted.
Debug MapSize(myMap())
Debug myMap("")
Else
Debug MapSize(myMap()) ;<- Returns 0
Debug myMap("") ;<- Returns 0
EndIf
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
- Andre
- PureBasic Team
- Posts: 2056
- Joined: Fri Apr 25, 2003 6:14 pm
- Location: Germany (Saxony, Deutscheinsiedel)
- Contact:
Re: Maps - don't allow empty keys or raise a Compiler warnin
Yes, this seems like solution (still to be tested). Thank you for the tip!
Now it's up to other users if there's a need for my initial request....
(because I still see no reason for using empty map keys, but that's my opinion)
Now it's up to other users if there's a need for my initial request....
(because I still see no reason for using empty map keys, but that's my opinion)
Re: Maps - don't allow empty keys or raise a Compiler warnin
the "overhead" would still exist. the map-library would have to internally check wether or not the key is "", whenever you attempt to add an element. i dont think thats a good idea.Andre wrote:a) an optional parameter to NewMap to forbid such empty strings.
i dont think thats a good idea either. a warning implies that you might be doing something wrong, but an empty string is perfectly valid.Andre wrote:b) a compiler warning so I would detect the problem during development.
if you want for example to have a map, where you use file-extensions as the key name ("bmp", "jpeg", etc.) a file doesnt have to have a file-extension, so you would use "" as the key name.Andre wrote:I still see no reason for using empty map keys, but that's my opinion
c ya,
nco2k
If OSVersion() = #PB_OS_Windows_ME : End : EndIf
- Andre
- PureBasic Team
- Posts: 2056
- Joined: Fri Apr 25, 2003 6:14 pm
- Location: Germany (Saxony, Deutscheinsiedel)
- Contact:
Re: Maps - don't allow empty keys or raise a Compiler warnin
@nco2k:
You're right about the file-extensions.
And of course there shouldn't be created an "overhead" for the map library or similar.
What I thought und still think is: activated by an optional parameter the empty strings can be declared as non-valid, and then a compiler (or better a debugger?) warning will be raised during executation if such an empty string occurs. Of course there shouldn't be any slow-down for the final executable because of my wish...
If Fred or freak say this is not possible or not worth the effort, we can close this request...
You're right about the file-extensions.
And of course there shouldn't be created an "overhead" for the map library or similar.
What I thought und still think is: activated by an optional parameter the empty strings can be declared as non-valid, and then a compiler (or better a debugger?) warning will be raised during executation if such an empty string occurs. Of course there shouldn't be any slow-down for the final executable because of my wish...
If Fred or freak say this is not possible or not worth the effort, we can close this request...
Re: Maps - don't allow empty keys or raise a Compiler warnin
Why don't you just check the value of the key before putting it into the array?