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 | # (Without this, the fully expanded details grow very lopsided.) |
||
26 | # |
||
27 | def balanced_session_columns_for_slot(slot, &block) |
||
28 | |||
29 | unassigned = slot.sessions.sort_by { |s| -estimated_height(s) } |
||
30 | |||
31 | columns = [[], []] |
||
32 | heights = [0, 0] |
||
33 | i = 0 |
||
34 | first = true |
||
35 | until unassigned.empty? |
||
36 | if unassigned.size == 1 && columns[0].size == columns[1].size # odd number of sessions, so last one can go in either column |
||
37 | i = if heights[0] < heights[1] |
||
38 | 0 |
||
39 | else |
||
40 | 1 |
||
41 | end |
||
42 | end |
||
43 | |||
44 | if first |
||
45 | # Start by placing longest description |
||
46 | session = unassigned.shift |
||
47 | first = false |
||
48 | else |
||
49 | # Greedy algo: choose next session to try to keep heights as close as possible |
||
50 | desired_height = heights[1-i] - heights[i] |
||
51 | session = nil |
||
52 | best_diff = 1 / 0.0 |
||
53 | unassigned.each do |candidate| # O(n^2), so watch this one if we start assigning lots of sessions per slot! |
||
54 | diff = (estimated_height(candidate) - desired_height).abs |
||
55 | if diff < best_diff |
||
56 | best_diff = diff |
||
57 | session = candidate |
||
58 | end |
||
59 | end |
||
60 | unassigned.delete(session) |
||
61 | end |
||
62 | break unless session |
||
63 | |||
64 | columns[i] << session |
||
65 | heights[i] += estimated_height(session) |
||
66 | i = 1-i |
||
67 | end |
||
68 | |||
69 | # Now yield each column with session sorted by room size. |
||
70 | |||
71 | columns.map! { |col| col.sort_by { |s| session_sort_order(s) } } |
||
72 | unless columns[0].empty? || columns[1].empty? |
||
73 | if columns[0].first.attendance_count < columns[1].first.attendance_count |
||
74 | columns = [columns[1], columns[0]] |
||
75 | end |
||
76 | end |
||
77 | |||
78 | columns.each(&block) |
||
98 | |||