patternrubyMinor
Readability and cyclomatic complexity of database transaction code
Viewed 0 times
readabilitycyclomaticdatabasecodetransactionandcomplexity
Problem
I consider these two options exactly the same. I prefer the first one but I'm not sure if it's better than the second option in terms of cyclomatic complexity or readability.
Assuming that this is not pure Ruby and I can call to the
Assuming that this is not pure Ruby and I can call to the
try method.existing_transaction = database.find_transaction_by_id(transaction.id)
database.create_transaction(transaction) unless existing_transaction
database.update_transaction(transaction) unless existing_transaction.try(:processed)existing_transaction = database.find_transaction_by_id(transaction.id)
if existing_transaction
unless existing_transaction.processed
database.update_transaction(transaction)
end
else
database.create_transaction(transaction)
endSolution
It's obviously subjective, but IMHO inline conditionals make code harder to understand, indentation is very important. So I'd definitely take the second approach, but writing it differently to use only one indentation level:
existing_transaction = database.find_transaction_by_id(transaction.id)
if !existing_transaction
database.create_transaction(transaction)
elsif !existing_transaction.processed
database.update_transaction(transaction)
endCode Snippets
existing_transaction = database.find_transaction_by_id(transaction.id)
if !existing_transaction
database.create_transaction(transaction)
elsif !existing_transaction.processed
database.update_transaction(transaction)
endContext
StackExchange Code Review Q#44441, answer score: 4
Revisions (0)
No revisions yet.