I've got an app which is connected to CodeClimate and it shows me an error Method upstream_transactions has a Cognitive Complexity of 6 (exceeds 5 allowed)
with marked below code:
def upstream_transactions(bank_account:, external_account:, external_transactions:)
external_transactions.each do |transaction|
next if transaction.currency != 'USD'
transfer = BankTransfer.unscoped.find_or_create_by(
This error showed up after I added line next if transaction.currency != 'USD'
.
def upstream_transactions(bank_account:, external_account:, external_transactions:)
external_transactions.each do |transaction|
next if transaction.currency != 'USD'
transfer = BankTransfer.unscoped.find_or_create_by(
customer_id: customer.id, transaction_identifier: transaction.transaction_id
)
next if transfer.deleted_at?
transfer.update(
date: transaction.booked_at,
# (...) some other params
)
end
Success(bank_account: bank_account, external_account: external_account)
end
How to avoid such error?
You could reduce the complexity by making a few helper methods you can call. Maybe
There are fewer 'nots' in
although you could even
Should the method be called
upstream_USD_transactions
instead? Would it be reasonable for the prospective caller to somehow know it is going to discard all non-USD transfers?