"It's a fix for a fix for a fix, apparently," writes Joan.
In this case, it's some Ruby code, which… gets a bit repetitive in that it repeats itself and does the same thing over and over again, in a repetitive fashion. Repeats.
def index
payment_service_parameters = { external_type: PaymentService::CANCEL_TYPES, state: 'pending' }
payment_service_parameters.merge!(params.fetch(:transfer_groups, {})) if params[:transfer_groups].present?
payment_service_parameters = { state: 'pending', external_type: PaymentService::CANCEL_TYPES }
if params[:transfer_groups].present?
payment_service_parameters = params.fetch(:transfer_groups, {})
payment_service_parameters[:external_type].reject!(&:blank?)
payment_service_parameters[:external_type] = PaymentService::CANCEL_TYPES if payment_service_parameters[:external_type].blank?
end
render locals: { transfer_groups: load_transfer_groups(payment_service_parameters) }
end
We start by populating the payment_service_parameters
property with a default value. Then, we check the params
collection and, if it has a :transfer_groups
key, we update the payment_service_parameters
. Then we blow that away and reset the payment_service_parameters
back to the original value.
Then we check for :transfer_groups
in the params
collection again, and if it exists we replace the entirety of payment_service_parameters
with :transfer_groups
, but then we make sure the key :external_type
has a value.
This is the sort of code that clearly was written by someone who didn't know what the code was supposed to do and just kept adding lines until the errors go away. Joan agrees:
Nobody asked what should the code do. The more you look at it, the worse it gets. Every check is done three times. But there are still several possible errors.