Initial Values of Strings
| andyfaff | April 10, 2010 - 14:34 | ||
|---|---|---|---|
|
On the face of it this seems perfectly reasonable. This could cause some hassle in developing xops though. In C there is a difference between: [ last edited April 11, 2010 - 17:54 ]
|
|||
| jjweimer | April 12, 2010 - 05:58 | ||
|---|---|---|---|
|
On the face of it this seems perfectly reasonable. .....
One wonders why such things are not the same with variables then? Consistency seems a bit off. Anyway, unlike in XOP coding, testing for NULL strings AFAIK is generally atypical of Igor coding (versus testing for empty strings), perhaps a compromise would be that NULL Igor strings be flagged as errors at compile time, rather than waiting until run-time to generate an error. This would go directly to the heart of my concerns. In addition, recommending that newly created strings be designated with something at creation, even if an empty value "", would seem be a good point to emphasize about doing proper Igor coding. -- |
|||
| April 12, 2010 - 08:49 | |||
|---|---|---|---|
|
One wonders why such things are not the same with variables then? Consistency seems a bit off. In C/C++, if you create a variable without assigning it a value, it will take the value of whatever the memory used by the variable has. Usually this value is nonsense. But since variables are an actual type (a double) they have to have some value (that is, they can't be null) and I suspect that Igor, and many other programming languages, initialize the value of variables to 0 to save some typing and because 0 is often a useful value for a variable to have. Anyway, unlike in XOP coding, testing for NULL strings AFAIK is generally atypical of Igor coding (versus testing for empty strings), perhaps a compromise would be that NULL Igor strings be flagged as errors at compile time, rather than waiting until run-time to generate an error. This would go directly to the heart of my concerns. Usually in Igor you don't have to worry too much about null strings. All (or at least most) Igor functions that return strings return an empty string or a non empty string, but not a null string. And when programmers create a string variable in code, they should assign it a value before using it. Usually a value of empty string is not very useful, so even if strings were initialized as empty I'm not sure how that would help prevent problems. Errors are good, in a sense, because they alert you of problems. If you didn't get an error, it might be a lot harder to track down bugs. |
|||
| jjweimer | April 12, 2010 - 09:09 | ||
|---|---|---|---|
|
... Errors are good, in a sense, because they alert you of problems. If you didn't get an error, it might be a lot harder to track down bugs.
Good in a sense except when they are in an obscure portion of code that only runs on occasion and that one forgets to test thoroughly via run-time examples before distributing a package. :-) -- |
|||
| RGerkin | April 12, 2010 - 11:44 | ||
|---|---|---|---|
|
I really wish I could pass the strings with null values. This would be useful in hierarchy of functions with optional string variables. For example: function smith1([str]) string str smith2(str) end function smith2([str]) string str str=selectstring(paramisdefault(str),str,"jones") print str end This won't work because I can't call smith2 from within smith1 unless I have assigned a value to str in smith1. But that renders the paramisdefault check in smith2 useless. I could assign it a value like "", but that is different from null, and might subvert whatever smith2 is supposed to be doing. Anyone else had this problem? |
|||



Joined: 2007-08-14
Location: United States
When variables are first created, their values are 0 unless otherwise set. When strings are first created, they are NULL. This sometimes throws an error should the coding not be carefully done.
Helpful would be when strings by default are created as "empty" (equivalent to string SomeStr="") in analogy to variable SomeVar=0 (default).
--
J. J. Weimer
Chemistry / Chemical & Materials Engineering, UAHuntsville