I have a nested struct like so struct1.struct2.foo. I would like to check if foo exists. However, struct2 isn’t guaranteed to exists either. I loathe to use isDefined(), but I also think that calling structKeyExists() twice is wasteful (e.g., if (structKeyExists(struct, 'struct2') && structKeyExists(struct.struct2, 'foo')) {}
I thought about using structFindKey(), but then I don’t want to run into an issue if there exists struct1.foo
Is there a better way to accomplish this?
This is a similar question to this question, but I am not dealing with an XML document so most of the answers in that post doesn’t work for me.
isDefined() could potentially return misleading results. It’s not as accurate as structKeyExists(). And unless you’re iterating through this code thousands of times at a pop, you won’t notice any performance difference. They both perform pretty well. But if the key doesn’t exist, that would be where you might notice the difference (again, only with thousands of iterations). isDefined() will traverse through the various available scopes if it can’t find it on the first pass. Even if you passed it a variable that looked like it specified the scope you wanted to check. structKeyExists() is pretty explicit in where it looks for that key. If it doesn’t find it, it will stop and return FALSE. It might look like you’re doing unnecessary work, but it’s returning a much more accurate result. And reading the code gives you a pretty good idea of what you’re looking for, so you don’t have to worry about it not being clean.
Peter’s suggestion was a pretty good one. Check out that link.