Initial Values of Strings

jjweimer
jjweimer's picture
Posts: 739
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


[ last edited April 9, 2010 - 09:51 ]
andyfaff
Posts: 382
Joined: 2007-09-11
Location: Australia

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:
char *msStr = NULL;
and
char *msStr = 0x1234567;
where that memory address has zero length. One often tests for whether an C operation has succeeded by testing for non NULLness of the pointer, not whether the memory area pointed to by the pointer has zero length. One form has no information, the other has information (just not a lot).
Also, it may be a big igor language change, possibly breaking pre-existing code.


[ last edited April 11, 2010 - 17:54 ]
jjweimer
jjweimer's picture
Posts: 739
Joined: 2007-08-14
Location: United States

andyfaff wrote:
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.

--
J. J. Weimer
Chemistry / Chemical & Materials Engineering, UAHuntsville


Posts: 497
Joined: 2007-03-01
Location: United States

jjweimer wrote:

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.

jjweimer wrote:

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
jjweimer's picture
Posts: 739
Joined: 2007-08-14
Location: United States

aclight wrote:
... 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. :-)

--
J. J. Weimer
Chemistry / Chemical & Materials Engineering, UAHuntsville


RGerkin
RGerkin's picture
Posts: 123
Joined: 2008-02-22
Location: United States

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?


Back to top