Order of evaluation and side effects

mabl4367mabl4367 Member Posts: 143
Hello everyone!

Please have a look at the code below. I think the variable names are pretty self explanatory:

CLEAR(recCust);
IF recCust.GET(custno) AND recCont.GET(recCust."Primary Contact No.) THEN
DoSomething;

Will "DoSomething" be executed provided that there is a customer with "No."="custno" and a contact with "No."=the customers "Primary Contact No."? The answer is dependent on two things:
Will the IF-statement always be evaluated from left to right? and
Will the side effects of "recCust.GET(custno)" affect the evaluation of "recCont.GET(recCust."Primary Contact No.")"?

Comments

  • KYDutchieKYDutchie Member Posts: 345
    Hi,

    I don't think that will work, this is better code and also more readable:
    CLEAR(recCust);
    IF recCust.GET(custno) THEN BEGIN
      IF recCont.GET(recCust."Primary Contact No.) THEN BEGIN
        DoSomething;
      END ELSE BEGIN
       DoSomethingElse;
      END;
    END ELSE BEGIN
      DONothing;
    END
    

    I personally do not like the method you used.
    Sure you will understand it now, but will you still remember in 3 years why you did it that way?
    Creating readable of code now, will cut the amount of time spend in the future on maintaining the code drastically.
    Especially if it is someone else that needs to maintain the code.

    Hope this helps,

    Regards,

    Willy
    Fostering a homeless, abused child is the hardest yet most rewarding thing I have ever done.
  • kapamaroukapamarou Member Posts: 1,152
    I think it will work.
  • mabl4367mabl4367 Member Posts: 143
    Maby I created a bad example.

    I just want to know in what order expressions are evaluated and if the evaluation of diffrent parts of the expression can have side effects that influence the evaluation of the rest of the expression.
  • kapamaroukapamarou Member Posts: 1,152
    They are evaluated from Left to Right but I think All will be evaluated.

    This is why if you use
    IF rec.FIND('-') THEN Repeat
    
    until (Stop or (rec.Next = 0));
    
    if you meet stop and exit the loop the rec variable will point to the next record that caused it to stop (if it exists).
  • KYDutchieKYDutchie Member Posts: 345
    Ah Ok,

    C/AL does not process evaluations conditionally. All evalutations within a statement will be processed.

    PS:
    I did not intend to be harsh, but I replied to your post before I finished my first pot of coffee in the morning. :mrgreen:
    Fostering a homeless, abused child is the hardest yet most rewarding thing I have ever done.
  • mabl4367mabl4367 Member Posts: 143
    Ok thank you for your replies!

    One question still remains:

    Can there be side effects of evaluating the first part of the expression that influence the evaluation of the remaining part?
  • KYDutchieKYDutchie Member Posts: 345
    Hi,

    I don't think there are harmful side effects. I would however check if the "Primary Contact No." is not blank prior to reading the contact table.
    The only thing in this example is that you are not sure which part of the evaluation failed.
    In your example, you cannot be entirely sure if it was the get of the customer record or the get of the contact record that failed the evaluation.

    Regards,

    Willy
    Fostering a homeless, abused child is the hardest yet most rewarding thing I have ever done.
  • mabl4367mabl4367 Member Posts: 143
    In this perticular example I would not care why I did not get a record. If ither of the GET:s fail I would do nothing.

    I'm also not concerned with if any side effects might be "harmfull" or not.

    Maby it's not clear what I mean by "side effect". For me a side effect is any state change caused by a function call aside from the function returning av value. I consider a function that modifies the value of a global variable or one or more of its own parameters, to have side effects. I want to know if those side effects can influence the evaluation of remaining part of the same expression containing the function call that caused them in the first place.

    I just want to know if the evaluation of the first part of the expression can have any effect what so ever on the evaluation of the remaining part.

    If it can, then the recCont.GET(recCust."Primary Contac No.") would probably succede since the recCust(custno) that was evaluated before, would have returned TRUE and more importantly, set recCust to be a customer with "No."=custno as a side effect.

    If it can not , then recCont.GET(recCust."Primary Contac No.") would fail since recCust would not yet have been initalized.

    Obviusly I could test my code exampel and se wat happens but I would still not know that the result would be the same every time. This is really a question of how C/AL is defined.
Sign In or Register to comment.