There is a note http://service.sap.com/sap/support/notes/1090369 which describes difference between IF and IFS. Here I will try to explain with the help of mapping example how both the Boolean functions will behave with same input.
As per SAP Note, IF and IFS Pseudocode
IF:
========================================================================
Value = if (<condition>, <expression1>, <expression2>)
“That is, all arguments are evaluated first, then value of one of them is returned. To minimize compatibility problems with old usages, the function makes one exception to this rule: in case when evaluating one of the branches throws an Exception, the function ignores it, if the exception is in the branch that is not selected by condition”
IFS:
========================================================================
If <condition>
value = <expression1>
else
value = <expression2>
end
“In this case, one expects that the <condition> expression is evaluated first and then, depending on the value of the condition, one of the branch expressions is evaluated and assigned to variable 'value'.”
IFS and IFSWIthoutElse functions have a Limitation.
When the The IFS and IFSWIthoutElse functions have the UDF of type Queue or Context, it will be executed unconditionally.
i.e the Queue UDF will be executed once even it does not have a matching condition in the input queue and the Context UDF will be executed once for every Context in the input queue, even if there is no matching condition in the input context.
For situation with functions leaving side-effects described above, we recommend to add a new argument to the User-Defined Function and create the side-effect conditionally based on that argument. Most probably you will want to put in this additional argument the same queue that you use in condition for function 'ifS'.
1. Standard functions
When IF function used, both the expressions are evaluated whereas in IFS case, only one expression will be evaluated based on the condition.
IF
condition is true
condition is false
You get the same result when you use IFS function in the above mapping. With standard function it is difficult to show how both the expressions are evaluated for if function.
2. Simple UDF
I wrote small UDF function to write trace to show both the UDFs are evaluated when if function is used.
Condition: when Field value is equal to “IF” return expression 1 result. Else expression 2 result.
Expression 1: UDFA
UDF B
IFS
Condition is true:you can notice, only one UDF gets evaluated here based on the condition.
Condition is false
with multiple condition
3. Simple UDF with exceptions
Let us put exception statement in UDFB.
UDF B
IF
condition is true
condition is false
IFS
condition is true
condition is false
4. Advanced UDF
Change the execution type to all values of context or all values of Queue. In my UDF I have used all values of Queue for both UDF A and UDF B.
I am changing the simple udf java code to fit into execution type all values of context/all values of queue.
UDF
If function behaviour is same in this use case as it works in simple UDF. As usual both the expressions are evaluated and only selected branch output is given.
IF
condition is true
IFS
As mentioned in the note, when IFS used with advanced UDFS, false condition branch executes at least once per context/queue. output is returned from branch one.
Let us put some exception statement in UDFB and use the same input for if and ifs.
IF result
condition is true. Both the expression are evaluated. Exceptions thrown in UDFB is ignored as branch is not selected. Mapping test produces output
IFS result
condition is true, expression 1 is evaluated. Expression 2 also evaluated (for ifs function, this gets executed at least once). Branch 2 is not selected, but exception thrown in branch 2 is not ignored. So mapping test ends in error.
5. Global container
Let us assume you want to generate numbers in sequence in target like 1,2,3. You will get different results for if and ifs
Create global variables in initialization block.
UDF A and UDF B are using the same code. ie. Incrementing the number by 1.
Please note execution type is single values
if function result
ifs function result
Only one UDF is called per condition so it produces expected output as 1,2,3
Similarly you can try yourself for look up use case.
Modified the blog as per artem.osukhovsky%40gmail.com comments.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
37 | |
10 | |
6 | |
4 | |
3 | |
3 | |
3 | |
2 | |
2 | |
2 |