| Conditions | 10 |
| Total Lines | 53 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like SchedulesHelper.balanced_session_columns_for_slot() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | module SchedulesHelper |
||
| 25 | def balanced_session_columns_for_slot(slot, &block) |
||
| 26 | |||
| 27 | unassigned = slot.sessions.sort_by { |s| -estimated_height(s) } |
||
| 28 | |||
| 29 | columns = [[], []] |
||
| 30 | heights = [0, 0] |
||
| 31 | i = 0 |
||
| 32 | first = true |
||
| 33 | until unassigned.empty? |
||
| 34 | if unassigned.size == 1 && columns[0].size == columns[1].size # odd number of sessions, so last one can go in either column |
||
| 35 | i = if heights[0] < heights[1] |
||
| 36 | 0 |
||
| 37 | else |
||
| 38 | 1 |
||
| 39 | end |
||
| 40 | end |
||
| 41 | |||
| 42 | if first |
||
| 43 | # Start by placing longest description |
||
| 44 | session = unassigned.shift |
||
| 45 | first = false |
||
| 46 | else |
||
| 47 | # Greedy algo: choose next session to try to keep heights as close as possible |
||
| 48 | desired_height = heights[1-i] - heights[i] |
||
| 49 | session = nil |
||
| 50 | best_diff = 1 / 0.0 |
||
| 51 | unassigned.each do |candidate| # O(n^2), so watch this one if we start assigning lots of sessions per slot! |
||
| 52 | diff = (estimated_height(candidate) - desired_height).abs |
||
| 53 | if diff < best_diff |
||
| 54 | best_diff = diff |
||
| 55 | session = candidate |
||
| 56 | end |
||
| 57 | end |
||
| 58 | unassigned.delete(session) |
||
| 59 | end |
||
| 60 | break unless session |
||
| 61 | |||
| 62 | columns[i] << session |
||
| 63 | heights[i] += estimated_height(session) |
||
| 64 | i = 1-i |
||
| 65 | end |
||
| 66 | |||
| 67 | # Now yield each column with session sorted by room size. |
||
| 68 | |||
| 69 | columns.map! { |col| col.sort_by { |s| session_sort_order(s) } } |
||
| 70 | unless columns[0].empty? || columns[1].empty? |
||
| 71 | if columns[0].first.attendance_count < columns[1].first.attendance_count |
||
| 72 | columns = [columns[1], columns[0]] |
||
| 73 | end |
||
| 74 | end |
||
| 75 | |||
| 76 | columns.each(&block) |
||
| 77 | end |
||
| 78 | |||
| 98 | |||