I've got a simple method that counts total lesson hours in the university schedule for additional modules in the department (students can attend many departments)
def hours_total
@hours_total = user.open_departments.each_with_object({}) do |department, h|
h[department] = (sports_hours[department] || 0) +
(science_hours[department] || 0) +
(intership_sum[department] || 0) +
(art[department] || 0) -
((obligatory_topics[department] || 0) +
(base[department] || 0))
end
end
How can I fix here Cyclomatic complexity for hours_total is too high.
? I have no idea how to not repeat || 0
cause in some departments sports_hours[department]
can be nil
value
The first step I'd take
Note: if your hours can be float values, substitute
to_i
withto_f
.Now if you and your Rubocop are ok with that, I'd probably leave it. If any of you is unhappy, the
positive
andnegative
should be extracted to a method.