Created
March 12, 2012 17:34
-
-
Save dbrady/2023535 to your computer and use it in GitHub Desktop.
Iterating by columns with a nudge factor
Why not
COLUMNS.times do |i|
draw_vertical_line_at COLUMN_WIDTH*i
end
draw_vertical_line_at PAGE_WIDTH
COLUMNS.times do |i|
draw_vertical_line_at (COLUMN_WIDTH)*i+CHECK_COLUMN_OFFSET
end
?
Then it's clear you are drawing COLUMNS
columns, with a following border. In addition, there's another line after. Explicitly making the trailing line implies to the reader that the important nouns are the columns not the lines.
Because of the focus on columns, it's also more composable.
def draw_basic_columns
COLUMNS.times do |i|
draw_vertical_line_at COLUMN_WIDTH*i
yield i
end
draw_vertical_line_at PAGE_WIDTH
end
def draw_check_columns
draw_basic_columns do |i|
draw_vertical_line_at (COLUMN_WIDTH)*i+CHECK_COLUMN_OFFSET
end
end
def draw_smiley_columns
draw_basic_columns do |i|
draw_smiley_at (COLUMN_WIDTH)*i+SMILEY_COLUMN_OFFSET
end
end
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can you not draw both sets of columns at once? Instead of two iterations, why not one that does something like this:
That pretty clearly expresses "I'm drawing columns, each of which has a check column in it". (To me, at least.)
Update, because I forgot to actually write this bit down. I think the special-case to avoid drawing the fencepost check column is a bit ugly, and I'm not convinced it's valid if
x
is a float. Fixing that elegantly is left as an exercise for the reader.