On 4/20/06, Molitor, Stephen L [email protected] wrote:
James,
Great summary. Wish I had participated in the quiz!
I liked Dave B.'s ‘Removed Unused Scope’ example. Would you (or
someone else) care to give examples of ‘Replace Similar Methods with
Metaprogrammed Definitions’
I’m not sure what James had in mind, but I think this works well as an
example of post refactoring ‘Replace Similar Methods with
Metaprogrammed Definitions’:
def add_test(name, clean_or_dirty, number_of_errors, definition)
eval <<“END_OF_TEST”
def test_#{name}
assert_class_is_#{clean_or_dirty} Class.new {#{definition}}
assert_number_of_errors #{number_of_errors}
end
END_OF_TEST
end
def dont_add_test(*args)
not gonna do it
end
class CheckRTest < Test::Unit::TestCase
def setup
@checkr = CheckR.new
end
def test_initialize
assert_kind_of CheckR, @checkr
end
def test_is_class_clean_exists
assert_respond_to @checkr, “is_class_clean?”
end
def test_errors_exists
assert_respond_to @checkr, “errors”
end
add_test :clean_if,
:clean, 0,
“def foo; true if true; end”
add_test :clean_if_2,
:clean, 0,
“def foo;if true;a=1;puts a;end;end”
add_test :clean_while,
:clean, 0,
“def foo;while a==2;break;end;end”
add_test :clean_assignment_in_if_body,
:clean, 0,
“def foo;if a==2;a=3;a;end;end”
add_test :clean_return_value,
:clean, 0,
“def foo;1;end”
[thirty more test definitions deleted]
private
def assert_class_is_clean(klass)
assert_equal true, check_class(klass)
end
def assert_class_is_dirty(klass)
assert_equal false, check_class(klass)
end
def assert_number_of_errors(n)
assert_equal n, @checkr.errors.length
end
def check_class(klass)
@checkr.is_class_clean?(klass)
end
designed with the static languages in mind. I’m very intrigued by how a
I don’t think we have one. We can #freeze the object, but that won’t
def foo
bar
and games. I think Pat hit on a super important topic here and we would
all do well to learn more about it.
Tomorrow, Matthew M. is back with a quiz about txet smnraiclbg…
–
thanks,
-pate