Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

レガシーコード改善ガイド

826 views

Published on

レガシーコード改善ガイド 11章

Published in: Education
  • Be the first to comment

レガシーコード改善ガイド

  1. 1. 11 (Ruby Geek−− CafeToshio Maki(@Kirika_K2)
  2. 2.
  3. 3. sampleclass InMemoryDirectory def initialize @elements = [] end def add_element(new_element) @elements << new_element end def generate_index index = Element.new("index") @elements.each{|e| index.add_text(e.name + "n") } @elements << index end def element_count @elements.length end def element(name) @elements.find{|e| e.name == name } endend
  4. 4. • • •
  5. 5. •••
  6. 6. • addElement • generate_index def add_element(new_element) @elements << new_element end def add_element(new_element) if @elements.empty? @elements << Element.new("index") end @elements << new_element element("index").add_text(new_element.name + "n") end
  7. 7. ••
  8. 8. def add_element(new_element) @elements << new_element end • @elements • element_count • element
  9. 9. def generate_index index = Element.new("index") @elements.each{|e| index.add_text(e.name + "n") } @elements << index end • @elements • element_count • element
  10. 10. class Element attr_reader :name, :text def initialize(name) @name = name @text = "" end def add_text(new_text) @text += new_text endend • add_text elements
  11. 11. •• element_count• element
  12. 12. require test/unitrequire in_memory_directoryclass InMemoryDirectoryTest < Test::Unit::TestCase def setup @dir = InMemoryDirectory.new @hoge_object = Element.new("hoge") @dir.add_element(@hoge_object) end def test_element_count assert_equal(1, @dir.element_count) end def test_element assert_equal(@hoge_object, @dir.element("hoge")) endend
  13. 13. $ ruby in_memory_directory_test.rbLoaded suite in_memory_directory_testStarted..Finished in 0.001858 seconds.2 tests, 2 assertions, 0 failures, 0 errors
  14. 14. class InMemoryDirectory def initialize @elements = [] end def add_element(new_element) if @elements.empty? @elements << Element.new("index") end @elements << new_element element("index").add_text(new_element.name + "n") end def element_count @elements.length end def element(name) @elements.find{|e| e.name == name } endend
  15. 15. $ ruby in_memory_directory_test.rbLoaded suite in_memory_directory_testStarted.FFinished in 0.015029 seconds. 1) Failure:test_element_count(InMemoryDirectoryTest)[in_memory_directory_test.rb:12]:<1> expected but was<2>.2 tests, 2 assertions, 1 failures, 0 errors
  16. 16. • add_element Index Element 2•
  17. 17. require test/unitrequire in_memory_directoryclass InMemoryDirectoryTest < Test::Unit::TestCase def setup @dir = InMemoryDirectory.new @hoge_object = Element.new("hoge") @dir.add_element(@hoge_object) @dir.generate_index end generate_index def test_element_count assert_equal(2, @dir.element_count) end def test_element assert_equal(@hoge_object, @dir.element("hoge")) endend
  18. 18. class InMemoryDirectory def initialize @elements = [] end def add_element(new_element) if @elements.empty? @elements << Element.new("index") end @elements << new_element element("index").add_text(new_element.name + "n") end def generate_index puts "WARNING: generate_index deplicated" end def element_count @elements.length end def element(name) @elements.find{|e| e.name == name } endend
  19. 19. • ※• static• final, const•
  20. 20. ••

×