Тема: if (a == true)
Показать сообщение отдельно
Старый 23.05.2012, 15:21   #13  
fed is offline
fed
Moderator
Аватар для fed
Ex AND Project
Соотечественники
Лучший по профессии 2017
Лучший по профессии 2015
Лучший по профессии 2014
Лучший по профессии AXAWARD 2013
Лучший по профессии 2011
Лучший по профессии 2009
 
2,913 / 5736 (197) ++++++++++
Регистрация: 13.03.2002
Адрес: Hüfingen,DE
Цитата:
Сообщение от Napalm Посмотреть сообщение
Общеизвестно, что для работы разработчиком Dynamics AX не обязательно знать X++ или уметь программировать.

Пример из LedgerJournalEngine\projQtyModified (AX 2012):

X++:
// This switch handles the normal(default) case and 2 other special cases
switch(true)
{
    case _ledgerJournalTrans_Project.Qty == 0:
        _ledgerJournalTrans.AmountCurCredit = 0;
        _ledgerJournalTrans.AmountCurDebit = 0;
        break;

    // If Credit/Debit Amounts are both 0, the Cost Price would be incorrectly set to 0 in the normal case
    // Handle this case separately
    case !_ledgerJournalTrans.AmountCurDebit && !_ledgerJournalTrans.AmountCurCredit && _ledgerJournalTrans_Project.CostPrice:
        this.projRecalcAmountCurDebitCredit(_ledgerJournalTrans, _ledgerJournalTrans_Project);
        break;

    default:
        // this.amount gets the debit amount or the negative credit amount
        offsetFactor = this.projOffsetFactor(_ledgerJournalTrans, _ledgerJournalTrans_Project);
        _ledgerJournalTrans_Project.CostPrice = Currency::price((offsetFactor * _ledgerJournalTrans.amount())/_ledgerJournalTrans_Project.Qty);
        // Cost Price should always be positive
        if (_ledgerJournalTrans_Project.CostPrice < 0)
            _ledgerJournalTrans_Project.CostPrice = - _ledgerJournalTrans_Project.CostPrice;
        // Cost Price have changed, recalc debit and credit amounts to avoid rounding diff
        this.projRecalcAmountCurDebitCredit(_ledgerJournalTrans, _ledgerJournalTrans_Project);
        break;
}
PS: Рекомендовано Microsoft! http://msdn.microsoft.com/en-us/libr...(v=ax.50).aspx
Ну это такой любопытный побочный эффект от использования оператора switch(). Сама по себе конструкция абсолютно легальна - пробегает по веткам case и заходит в первую ветку с выполненным условием. Теоретически может быть заменено на серию вложенных if() {} else{ if() {} else {if () {}}, однако вариант с switch() превосходит их по читаемости. На мой взгляд все легально.
Для раздумья о нелегкой судьбе оператора switch() в различных языках программирования, рекомендую прочитать: Duff's device