Checking for an empty (Strong Typed) String

This is a follow up article to Checking for an empty String, which was aimed at non typed eScript.

This time around we will look at the pitfalls of string comparison in the ST engine.

I'll assume that you've read the previous article, so i'll jump right into it with this scenario.

//declare vars
var iZero = 0;
var sEmpty:String = "";
var sEmpty2:String = "";


[1st Pitfall]
Before we begin, i'd like to highlight the first pitfall. Since we've declared our variables as a String Object, we can no longer do

if (sEmpty == sEmpty2)
//returns FALSE

Even if we've assigned sEmpty and sEmpty2 the same values, and we trace out the values of both variables, it will look the same, but it will return false.

The reason is sEmpty and sEmpty2 are both pointers to two different objects in memory, so what we are really asking the engine is, are these two objects the same object? Even if they are identical, the answer is No.

[2nd Pitfall]
The correct method of evaluating strings is to use the valueOf or
toString method.

Siebel provides an example of how to perform this in Siebel Support [ID:
757503.1]


if (o1Test.valueOf == o2Test.valueOf)
//OR
if (o1Test.toString == o2Test.toString)


Unfortunately, the above examples will always return true regardless of the value of the string.

If you have strong typing enabled in your Tools, type sEmpty followed by the dot and you will get a drop down list of available methods for this object, select the valueOf option, and add the () after it.



This is very important, as explained below
valueOf This returns a pointer to the function
valueOf() This executes the function and returns the value.

So when we use the following expression

if (o1Test.valueOf == o2Test.valueOf)

We are asking the engine, does the function of o1Test.valueOf match the function of o2Test.valueOf? Yes, both functions are the same.

If we used the parenthesis

if (o1Test.valueOf() == o2Test.valueOf())

We are asking does the value of o1Test match the value of o2Test, which is the correct way of executing this. Siebel should default its intellisense to include this parenthesis for function calls.

[Checking if a ST String is empty]

[ST Method 1]

if (sEmpty.valueOf()=="")
//TRUE

[Method 2]

if (sEmpty.length==0)
//TRUE

[ST Method 3]

if (sEmpty.valueOf())
//TRUE

[ST Method 4]

if (sEmpty.valueOf()==="")
//TRUE


If you strong type your string variables, the compiler will give a warning if you try to assign or initialise the number 0 to a string, so you avoid the pitfalls of method 1 & 4 of the T engine.

So what happens if we dynamically assign the string object a number as in the example below?

sEmpty = iZero;//dynamically assign number to ST String

In this scenario, Siebel will not catch it during compile, but it will implicitly convert the number to a string during runtime.

The problem still remains when sEmpty is set to null, because .length, .valueOf will not work on null objects, the workaround for this is similiar to the T Engine. Either initialise your variables or check that the object is not null first.

[Conclusion]

If we strong type our string we avoid the problem when a Zero is assigned to a string, either Tools will prevent you from compiling the code, or the engine will convert it to a string for you anyway.

But the question on everyone's mind at this point should be, is it recommended to strong type Strings and other primitive variables in the Siebel ST engine?

This article from Ponderproserve provides a good answer on this.
http://www.ponderproserve.com/SiebelScriptEngineBenchMark.html

In a nutshell, strong typing primitives will slow down scripting in your application and is not recommended until Siebel fixes this problem.


6 comments:

  1. Very thorough article, thanks!
    We've no fixed best practice regarding strong typing on our project. Personally I like the idea of typing and treating things as objects (I started in C++, then Java, might be something to do with that!) I think the first pitfall is the most common, and usually enough to deter against any further use of the STing.

    ReplyDelete
  2. Good Article Jason !
    -Geeksajan
    http://geeksajan.blogspot.com

    ReplyDelete
  3. The conclusion is wrong about performances, and also are you about the use of valueOf.
    In the link you provided in your conclusion, there is a paragraph before their own conclusion, named "Update - 4/4/2007".
    In this paragraph, they explain that Siebel fixed the lack of performances of ST scripts Also, they say that the use of valueOf is no more necessary (it is implicitely called when refering the String object instance in a string context)

    ReplyDelete
  4. Hi toxcct

    The conclusion is not wrong =). You should look at it from the perspective of what Siebel version you are on.

    The use of valueOf() for comparisons is still neccesary (tested on 8.1.1 and 8.1.1.2). Its best to verify this for yourself.

    If you do decide to change the type your primitives, beaware of the effect that this will have on your program logic.

    ReplyDelete
  5. I just wasted three days on some strong typed script that compared strings, removed the strong typing and it all worked fine. Then found this article, which made me realise it was a wise decision.

    Thanks Jason!

    ReplyDelete

Comments are open to all, please make it constructive.