<br><br><div><span class="gmail_quote">On 7/23/07, <b class="gmail_sendername">Alan Gauld</b> <<a href="mailto:alan.gauld@btinternet.com">alan.gauld@btinternet.com</a>> wrote:</span><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>The prime directive of coding is make it readable!<br>The DRY principle is just that a principle. If repeating makes for<br>more maintainable or readable code then repeat yourself.<br><br>Remember 80% of the cost of software is in maintenance not initial
<br>development. DRY is one way to improve maintainablility <br>PS The most serious problem with your code from my perpspective<br>is that your variable names are way too long. That affects maintenance<br>and readability more that the repetition (and in the case of email it
<br>causes line wrapping that makes it even worse!) <br><br>Finally the class is not a good class in an OOP sense since it is<br>nearly a verb.<br>It is better done as a simple function in my view, just pass in the<br>parameters to
<br>the create method. Like this:<br><br>def create_table(num_items, row_size):<br> start = 0<br> table = []<br> num_rows = num_items/row_size<br> for n in range(num_rows):<br> row = [num for num in range(start,start+row_size)]
<br> table.append(tuple(row))<br> start += row_size<br> return table<br><br>Or better still build a table class that knows how to create<br>itself, but also knows how to maniplulate itself too - doing whatever
<br>it is you intend doing to the table you just created! This reflects<br>another<br>programming "rule" - the law of demeter" - one of the fundamentals of<br>OOP.</blockquote><div><br><br>Alan, I spent today going over what you took the time to patiently illustrate to me, and I believe I learned the following things:
<br>- code readability and ease of maintenance are the primary goals of a computer programmer<br>- classes that are very nearly verbs should be converted to functions, unless you can write a class that knows how to create and manipulate itself, following the law of demeter
<br><br>About the last bit of code, where you propose to create the table using list comprehension, I did want to capture partial rows, so I put the finishing touches to your work so it could. When I subjected the code to a battery of tests, it failed in the following way:
<br><br>def create_table(num_items, row_size):<br> start = 0<br> table = []<br> num_rows = (num_items/row_size) + (num_items%row_size)<br> for n in range(num_rows):<br> row = [num for num in range(num_items)[start:start+row_size]]
<br> table.append(tuple(row))<br> start += row_size<br> return table<br><br>def test_harness():<br> assert create_table(3, 1) == [(0,), (1,), (2,)]<br> assert create_table(4, 1) == [(0,), (1,), (2,), (3,)]
<br> assert create_table(1, 2) == [(0,)]<br> assert create_table(2, 2) == [(0, 1)]<br> assert create_table(4, 2) == [(0, 1), (2, 3)]<br> assert create_table(5, 2) == [(0, 1), (2, 3), (4, )]<br> assert create_table(5, 3) == [(0, 1, 2), (3, 4)]
<br> assert create_table(7, 4) == [(0, 1, 2, 3), (4, 5, 6)]<br><br>assert create_table(5, 3) == [(0, 1, 2), (3, 4)]<br>AssertionError<br><br>I know that my method of calculating the number of rows is faulty, but I'm not sure how to correct it.
<br></div><br></div><br>