Page 1 of 1

The IF statement

Posted: Fri Aug 20, 2004 8:14 am
by denzilsyb
Hallo

Which IF statement is recommended, bearing in mind that the correct results, performance and ability to understand is what we are after..

Code: Select all

if NOT(LINK_LOOKUP.NOTFOUND) and 
(
 trimb(LINK_ROLLUP.ORGTG_IBT_N) = 2610 or 
 trimb(LINK_ROLLUP.ORGTG_IBT_N) = 2710 or 
 trimb(LINK_ROLLUP.ORGTG_IBT_N) = 5810 or 
 trimb(LINK_ROLLUP.ORGTG_IBT_N) = 1409 or 
 trimb(LINK_ROLLUP.ORGTG_IBT_N) = 7526 or 
 trimb(LINK_ROLLUP.ORGTG_IBT_N) = 1426 or 
 trimb(LINK_ROLLUP.ORGTG_IBT_N) = 5837 or 
 trimb(LINK_ROLLUP.ORGTG_IBT_N) = 9405 or 
 trimb(LINK_ROLLUP.ORGTG_IBT_N) = 5745 or 
 trimb(LINK_ROLLUP.ORGTG_IBT_N) = 3145 
) then 
  LINK_ROLLUP.CASH_A + LINK_LOOKUP.CASH_DEP_R100_CC_A else 
LINK_ROLLUP.CASH_A
or...

Code: Select all

if NOT(LINK_LOOKUP.NOTFOUND) and trimb(LINK_ROLLUP.ORGTG_IBT_N) = 2610 then 
 LINK_ROLLUP.CASH_A + LINK_LOOKUP.CASH_DEP_R100_CC_A else if
NOT(LINK_LOOKUP.NOTFOUND) and trimb(LINK_ROLLUP.ORGTG_IBT_N) = 2710 then 
 LINK_ROLLUP.CASH_A + LINK_LOOKUP.CASH_DEP_R100_CC_A else if
NOT(LINK_LOOKUP.NOTFOUND) and trimb(LINK_ROLLUP.ORGTG_IBT_N) = 5810 then 
 LINK_ROLLUP.CASH_A + LINK_LOOKUP.CASH_DEP_R100_CC_A else if
NOT(LINK_LOOKUP.NOTFOUND) and trimb(LINK_ROLLUP.ORGTG_IBT_N) = 1409 then 
 LINK_ROLLUP.CASH_A + LINK_LOOKUP.CASH_DEP_R100_CC_A else if
NOT(LINK_LOOKUP.NOTFOUND) and trimb(LINK_ROLLUP.ORGTG_IBT_N) = 7526 then 
 LINK_ROLLUP.CASH_A + LINK_LOOKUP.CASH_DEP_R100_CC_A else if
NOT(LINK_LOOKUP.NOTFOUND) and trimb(LINK_ROLLUP.ORGTG_IBT_N) = 1426 then 
 LINK_ROLLUP.CASH_A + LINK_LOOKUP.CASH_DEP_R100_CC_A else if
NOT(LINK_LOOKUP.NOTFOUND) and trimb(LINK_ROLLUP.ORGTG_IBT_N) = 5837 then 
 LINK_ROLLUP.CASH_A + LINK_LOOKUP.CASH_DEP_R100_CC_A else if
NOT(LINK_LOOKUP.NOTFOUND) and trimb(LINK_ROLLUP.ORGTG_IBT_N) = 9405 then 
 LINK_ROLLUP.CASH_A + LINK_LOOKUP.CASH_DEP_R100_CC_A else if
NOT(LINK_LOOKUP.NOTFOUND) and trimb(LINK_ROLLUP.ORGTG_IBT_N) = 5745 then 
 LINK_ROLLUP.CASH_A + LINK_LOOKUP.CASH_DEP_R100_CC_A else if
NOT(LINK_LOOKUP.NOTFOUND) and trimb(LINK_ROLLUP.ORGTG_IBT_N) = 3145 then  
 LINK_ROLLUP.CASH_A + LINK_LOOKUP.CASH_DEP_R100_CC_A else 
LINK_ROLLUP.CASH_A
Which will bring back the correct results?
If they return different values different.. why?
Which is faster to interpret by DS? Is the second piec of code resulting in 11 reads (maximum) and the first set only resulting in 2 reads (maximum)?

Posted: Fri Aug 20, 2004 8:44 am
by neena
Hi
Its not the question that which will run fast.. Its a question of which one is more effiecient way..
As my knowedge first one is easy to debug and easy to understand.
When ever you are building some logic .. keep it more simple... and more effective.. easy to understand by some others and easy to make some changes if there is any.
If you see ascential tips and tricks they clearly mentioned that "keep everthing simple" If you have a very complex situation .. devide that into number of pieces of work and work it.. This way you will have control where that is going wrong...

HTH.
Thanks,
Neena

Posted: Fri Aug 20, 2004 9:20 am
by chucksmith
Combine the two concepts, and use stage variables.

Code: Select all

svTrimbOrgtgIbtN = trimb(LINK_ROLLUP.ORGTG_IBT_N)

Code: Select all

svAddItPrep = If  
 svTrimbOrgtgIbtN  = 2610 Then @TRUE Else If  
 svTrimbOrgtgIbtN  = 2710 Then @TRUE Else If  
 svTrimbOrgtgIbtN  = 5810 Then @TRUE Else If  
 svTrimbOrgtgIbtN  = 1409 Then @TRUE Else If  
 svTrimbOrgtgIbtN  = 7526 Then @TRUE Else If  
 svTrimbOrgtgIbtN  = 1426 Then @TRUE Else If  
 svTrimbOrgtgIbtN  = 5837 Then @TRUE Else If  
 svTrimbOrgtgIbtN  = 9405 Then @TRUE Else If  
 svTrimbOrgtgIbtN  = 5745 Then @TRUE Else If  
 svTrimbOrgtgIbtN  = 3145 Then @TRUE Else @FALSE

Code: Select all

svAddIt = If Not(LINK_LOOKUP.NOTFOUND) And svAddItPrep Then @TRUE Else @FALSE
Now in your derivation:

Code: Select all

If SvAddIt Then LINK_ROLLUP.CASH_A + LINK_LOOKUP.CASH_DEP_R100_CC_A Else 
LINK_ROLLUP.CASH_A
This is both clear and efficient.

Posted: Fri Aug 20, 2004 9:45 am
by chulett
You could change the derivation of the stage variable to make it a little easier - or at least what I think is a little easier on the eyes and brain. :wink: You don't need to explicitly set svAddItPrep to @TRUE or @FALSE:

Code: Select all

svTrimbOrgtgIbtN  = 2610 or svTrimbOrgtgIbtN  = 2710 or svTrimbOrgtgIbtN  = 5810 or svTrimbOrgtgIbtN  = 1409 or svTrimbOrgtgIbtN  = 7526 or svTrimbOrgtgIbtN  = 1426 or svTrimbOrgtgIbtN  = 5837 or svTrimbOrgtgIbtN  = 9405 or svTrimbOrgtgIbtN  = 5745 or svTrimbOrgtgIbtN  = 3145
This expression will evaluate to true or false and set the stage variable accordingly.

Posted: Fri Aug 20, 2004 10:36 am
by kcbland
Or, you could create the INLIST function that I posted at
viewtopic.php?t=85538&postdays=0&postorder=asc&start=0
and make this really easy:

Code: Select all

if NOT(LINK_LOOKUP.NOTFOUND) and INLIST(trimb(LINK_ROLLUP.ORGTG_IBT_N), 2610,2710,5810,1409,7526,1426,5837,9405,5745,3145) then LINK_ROLLUP.CASH_A + LINK_LOOKUP.CASH_DEP_R100_CC_A else 
LINK_ROLLUP.CASH_A

Posted: Mon Aug 23, 2004 2:15 am
by denzilsyb
Thanks a lot guys!

This is the kind of response I was hoping for.

Posted: Mon Aug 23, 2004 6:41 am
by rasi
Consider putting this into a reference table and use it as a hash lookup. This is maintenance free solution. Also not hard coded in your job. Anytime you need to add or change in the reference table. Worth considering it.

Thanks
Siva

Posted: Mon Aug 23, 2004 7:16 pm
by vmcburney
I agree you should avoid hard coding these values in a transformer in case they need to be modified at regular intervals. A lookup table or even a routine would be easier. With a routine you just need to change the inlist and recompile the routine and the job will pick up the new values.