vendredi 29 octobre 2021

use function instead of multiple if else

this is my code and I want to break it to multiple function(clean code!), for these two section (status===edited) and (status === added) or two different function for (dataindex===ReportEffectiveDate) and (dataindex=== EffectiveDate).how can I put these if else statement in a separate function then I use this function for each status. totally I want to know which way is better : I use multiple if and else if or use multiple function for this code? thanks for your help!

function handleTableRowChange(record: LoadModel, oldValue: any, newValue: any, dataIndex: string) {
  console.log(record, oldValue, newValue, dataIndex);
  const status: RowStatus = tableStore.getRowStatus(record);
  if (!!newValue) {
    if (dataIndex === 'ReportEffectiveDate') {
      if (record.EffectiveDate > record.ReportEffectiveDate) {
        record.EffectiveDate = null;
        tableStore.update(record);
        Modal.error({
          content: translate('ReportEffectiveDatecantbelessthanoldeffectivedate'),
        });
        console.log('error');
      } else if (record.EffectiveDate == record.ReportEffectiveDate) {
        record.ReportEffectiveDate = null;
        tableStore.update(record);
      }
    }
    if (dataIndex === 'EffectiveDate') {
      if (status === 'added') {
        const isValid: boolean = checkIsEffectiveDateValid(record);
        if (!isValid) {
          record.EffectiveDate = null;
          tableStore.update(record);
        }
      } else if (status === 'edited') {
        const maxEffectiveDateRecord: LoadModel = getMaxEffectiveDateRecord(record);
        if (record.EffectiveDate > maxEffectiveDateRecord.EffectiveDate) {
          if (newValue < maxEffectiveDateRecord.EffectiveDate) {
            record.EffectiveDate = oldValue;
            tableStore.update(record);
          }
        }
      }
    }
  }
}

Aucun commentaire:

Enregistrer un commentaire