skip to Main Content

I have created a method to toggle transaction type, like converting expense to income and income to expense…

and I created following method

void swapTransaction(TransactionModel transaction)
  {
    transaction.isExpense=!transaction.isExpense;
    update();
  }

it worked fine but than I was forced to apply following method,

  void swapTransaction(TransactionModel transaction)
  {
    int index=_transactions.indexOf(transaction);
    _transactions[index].isExpense=!_transactions[index].isExpense;
    update();
  }

here I want to know what is the better method to apply… is there any big difference?

2

Answers


  1. I’m not sure if such questions are suitable for this site, as it can be "opinion based" but here’s my take:

    I think the second method is better since you are updating the actual _transactions list, but in the first example, you are just updating what’s getting passed into the function – the TransactionModel, so you’re not actually changing the list of _transactions.

    Login or Signup to reply.
  2. According to me, second one is better.
    In first one you are passing one copy of object to update, which is just used within that function. While in second one you are actually updating your actual list of transaction, which you used to display.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search