cancel
Showing results for 
Search instead for 
Did you mean: 

a branch(if;do;while;$[.;.;.]) more than 255 byte codes away

hzadonis
New Contributor
Hi, Dear Masters:
  Last week, I'm blocked by the "if-else if-else" structure with a limitation of 255 byte.
  My input is “According to several conditions(more than 6) and the position what I had”, output is the next step I should do. The structure like this:
      $[
cond1;
[stat1-1;];
cond2;
[stat2-1;];
cond3;
[stat3-1;];
cond4;
[stat4-1;];
cond5;
[stat5-1;];
cond6;
[stat6-1;];
[else7-1;]
      ]
  
  The real code I input is:
      $[
        (bs=bf) and (0=sum LL);
        [
          oplg[crp;clt;LP;LL]; /OpenLong
        ];
        (bs=sf) and (0=sum SL);
        [
          opst[crp;clt;SP;SL]; /OpenShot
        ];
        (bs=bf) and (0<sum LL);
        [
          ovlg[crp;clt;LP;LL]; /OverWeightLong;
        ];
        (bs=sf) and (0<sum LL);
        [
          cllg[ci;crp;cdt;stg;LP;LL]; /ClearLong
        ];
        (bs=bf) and (0>sum SL);
        [
          clst[ci;crp;cdt;stg;SP;SL]; /ClearShort
        ];
        (bs=sf) and (0>sum SL);
        [
          ovst[crp;clt;SP;SL]; /OverWeightShort
        ];
        [0N!ci,cricidx,cntRIC]
      ]
  The "if-else if-else" is in the main function, and will invoke other function based on one of conditions.
  I had simplified my variable and function naming, but I'm afraid it is not the 255 limitation made my failure. What's your opinion?

Zheng
4 REPLIES 4

Flying
New Contributor III
First of all, I think it's best if you could review your logic to see if there is any better way to organize your dispatcher like code.

If you still decide that this is the only way you could do it, you may break this long if-else-if cascade into pieces:

For example:
fun:{
  $[cond1;
      :action1;
    cond2:
      :action2];
  /to be continued
  $[cond3;
      :action3;
     cond4;
      :action4];
  /.....
  };

Something similar to the above.

Thanks, Flying.
  I resolved the problem last night. Yes, you're right. I must break the "long" if-else if-else" into smaller piece.
  I had to just judge the sum position I had and then decide how to do by Buy or Sell flag.
  I'm not sure it is my free kdb+ feature or not. Because in real cases, long and several conditions must be there.

Thanks!
Zheng

在 2017年2月27日星期一 UTC+8下午6:43:23,Flying写道:
First of all, I think it's best if you could review your logic to see if there is any better way to organize your dispatcher like code.

If you still decide that this is the only way you could do it, you may break this long if-else-if cascade into pieces:

For example:
fun:{
  $[cond1;
      :action1;
    cond2:
      :action2];
  /to be continued
  $[cond3;
      :action3;
     cond4;
      :action4];
  /.....
  };

Something similar to the above.

Flying
New Contributor III
This limitation exists in both the 32-bit and the 64-bit versions, I'm afraid.

I consider this a good feature, though, as I'd usually consider alternative design patterns of my logic once I hit the need for a if-else-if cascade with more than a few levels. Futhermore, from your single-phrase description, I suspect that there might be better ways to compute the liquidity information you need than using long if-else-if cascades.

I used to thought the Switch structure to resolve my problem, but not have the related document to reference. Maybe it's a good solution for several conditions workflow.

在 2017年2月28日星期二 UTC+8下午7:46:40,Flying写道:
This limitation exists in both the 32-bit and the 64-bit versions, I'm afraid.

I consider this a good feature, though, as I'd usually consider alternative design patterns of my logic once I hit the need for a if-else-if cascade with more than a few levels. Futhermore, from your single-phrase description, I suspect that there might be better ways to compute the liquidity information you need than using long if-else-if cascades.