Question Status

Verified
Khashayar asked a question on 18 May 2018 5:26 AM
    select RelateDiscountTo, percent
        from discTable
            where discTable.RelateDiscountTo == this.CustID;
                if (discTable.RelateDiscountTo == this.CustID)
                {
                    this.percent            = discTable.percent;
                    callCustGroupDiscount   = false;
                }

Hi Community, I wanted to know if the above Code with it's Statements, especially the if Statement is acceptable ?

I usually don't use the if Statement and started right away after the where with the Code in bracket.

For some reason, suddenly then it didn't work properly. Because in my custTable I didn't have some of the custId's I choosed, but it still (without the if) executed the Code in the bracket.

So I was forced to use the if Statement.


 

Reply
Verified Answer
Heinz Schweda responded on 18 May 2018 6:04 AM

Hello,

i would prefer this version

   select Recid, RelateDiscountTo, percent
       from discTable
           where discTable.RelateDiscountTo == this.CustID;

               if (discTable.RecId != 0)
               {
                   this.percent            = discTable.percent;
                   callCustGroupDiscount   = false;
               }


Regards,

Heinz

Reply
Khashayar responded on 18 May 2018 6:17 AM

Okay.

Now You chose RecId !=0 in Your if-statement.

Why ?

Is it more efficient ?

I guess so... because in my if-statement he then has to look if the custID  is in the discTable (plus I have it already in the where), wheras in your's he checks if a record exists in in the discTable, and if yes then he set's discTable.percent to this.percent, because he already knows (where Statement).

Reply
Verified Answer
Heinz Schweda responded on 18 May 2018 10:20 AM

Yes, that's the reason. If you have to change the WHERE-clause later, you don't have to change the IF as well. And from my point of view it's easier to read.

Also, if this.CustID is empty/blank (i cannot see if this is checked earlier) your IF may become true, and that's not what you want.

Reply
Verified Answer
Heinz Schweda responded on 18 May 2018 6:04 AM

Hello,

i would prefer this version

   select Recid, RelateDiscountTo, percent
       from discTable
           where discTable.RelateDiscountTo == this.CustID;

               if (discTable.RecId != 0)
               {
                   this.percent            = discTable.percent;
                   callCustGroupDiscount   = false;
               }


Regards,

Heinz

Reply
Verified Answer
Heinz Schweda responded on 18 May 2018 10:20 AM

Yes, that's the reason. If you have to change the WHERE-clause later, you don't have to change the IF as well. And from my point of view it's easier to read.

Also, if this.CustID is empty/blank (i cannot see if this is checked earlier) your IF may become true, and that's not what you want.

Reply