Programming Production Code as if writing Natural Language      Kazuya NUMATA         @kaznum
Self-introduction                        (Kazuya NUMATA)  •  •                          �  •  •  •      ���������������  •
My favorite•    •    •
Agile
Communication with team members     including stakeholder                &        Iterative Testing
Behavior Driven Development             BDD
Toolsto make BDD effective
•   RSpec (Ruby)•   NSpec (.NET)•   CppSpec (C++)•   JBehave (Java)•   instinct (Java)•   PHPSpec (PHP)•   and more....
Question
Question   Have you ever experienced *Spec?*Spec                                 ?
RSpec example:describe "A new chess board" do   before(:each) do     @board = Chess::Board.new   end   it "should have 32 ...
RSpec example:describe "A new chess board" do   before(:each) do     @board = Chess::Board.new   end   it "should have 32 ...
RSpec example:$ rspec -fd board_spec.rb(Output is...)A new chess board  should have 32 pieces                       D. Che...
It is just Natural Language!
�
The reality is that it’s hard for the Japanese.
Cucumber(              )
Cucumber Example:         :         :         "           "    "                 "         "       "   "        Cucumber" ...
/SpecBefore BDDAfter    Spec    SpecBDD
Before BDDAfterBDD
?
Uncle Bob   Complex fulcrumPoint = Complex.FromRealNumber(23.0);is generally better than   Complex fulcrumPoint = new Comp...
by Masayoshi Son
Person         name                        true                false
Ruby Example:   name   1ex1class Person  attr_accessor :name  def initialize(name = nil)    @name = name  endendshimada = ...
Ruby Example: name   1ex2class Person  attr_accessor :name  def initialize(name = nil)    @name = name  end  def has_a_nam...
Ruby Example: name    1ex3class Person  attr_accessor :name  def initialize(name = nil)    @name = name  end  def has_a_na...
Ruby Example: name                           1ex4      name                                                               ...
if air_plane.is_created_by? :bowing    if he.is_eligible_to_get? reward    if selected_value.is_valid?→    can_..., is_,
is does was didmodule Kernel  def is(expected)    expected  end alias_method :does, :is alias_method :was, :is alias_metho...
Conclusion & Impression•   Spec••      •                                                                          Receiver...
thank you :)
あたかも自然言語を書くようにコーディングしてみる
あたかも自然言語を書くようにコーディングしてみる
あたかも自然言語を書くようにコーディングしてみる
あたかも自然言語を書くようにコーディングしてみる
Upcoming SlideShare
Loading in …5
×

あたかも自然言語を書くようにコーディングしてみる

2,203 views

Published on

RSpecではまるで自然言語(英語の文章)を書くかのように、振舞の定義(*_spec.rb)が書けます。プロダクトコードでも同様に英語の文法を意識して書くようにしてみたらどうなるでしょうか。Rubyを使って試してみました。

0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,203
On SlideShare
0
From Embeds
0
Number of Embeds
101
Actions
Shares
0
Downloads
5
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • あたかも自然言語を書くようにコーディングしてみる

    1. 1. Programming Production Code as if writing Natural Language Kazuya NUMATA @kaznum
    2. 2. Self-introduction (Kazuya NUMATA) • • � • • • ��������������� •
    3. 3. My favorite• • •
    4. 4. Agile
    5. 5. Communication with team members including stakeholder & Iterative Testing
    6. 6. Behavior Driven Development BDD
    7. 7. Toolsto make BDD effective
    8. 8. • RSpec (Ruby)• NSpec (.NET)• CppSpec (C++)• JBehave (Java)• instinct (Java)• PHPSpec (PHP)• and more....
    9. 9. Question
    10. 10. Question Have you ever experienced *Spec?*Spec ?
    11. 11. RSpec example:describe "A new chess board" do before(:each) do @board = Chess::Board.new end it "should have 32 pieces" do @board.should have(32).pieces endend D. Chelimsky. The RSpec Book Section 13.7 Generated Descriptions
    12. 12. RSpec example:describe "A new chess board" do before(:each) do @board = Chess::Board.new end it "should have 32 pieces" do @board.should have(32).pieces endend D. Chelimsky. The RSpec Book Section 13.7 Generated Descriptions
    13. 13. RSpec example:$ rspec -fd board_spec.rb(Output is...)A new chess board should have 32 pieces D. Chelimsky. The RSpec Book Section 13.7 Generated Descriptions
    14. 14. It is just Natural Language!
    15. 15.
    16. 16. The reality is that it’s hard for the Japanese.
    17. 17. Cucumber( )
    18. 18. Cucumber Example: : : " " " " " " " Cucumber" " " " Cucumber" ( ∀ )o sasata299s blog http://blog.livedoor.jp/sasata299/archives/51278697.html
    19. 19. /SpecBefore BDDAfter Spec SpecBDD
    20. 20. Before BDDAfterBDD
    21. 21.
    22. 22. Uncle Bob Complex fulcrumPoint = Complex.FromRealNumber(23.0);is generally better than Complex fulcrumPoint = new Complex(23.0);Consider enforcing their use by making the corresponding constructors private. ( )
    23. 23. by Masayoshi Son
    24. 24. Person name true false
    25. 25. Ruby Example: name 1ex1class Person attr_accessor :name def initialize(name = nil) @name = name endendshimada = Person.new(“Shimada”)if shimada.name.split(//)[0] == “S” puts “Match”end
    26. 26. Ruby Example: name 1ex2class Person attr_accessor :name def initialize(name = nil) @name = name end def has_a_name_that_begins_with?(expected) @name.split(//)[0] == expected endendshimada = Person.new(“Shimada”)if shimada.has_a_name_that_begins_with?(“S”) puts “Match”end
    27. 27. Ruby Example: name 1ex3class Person attr_accessor :name def initialize(name = nil) @name = name end def has_a_name_that_begins_with?(expected) @name.split(//)[0] == expected end def method_missing(m, *args, &block) if m.to_s =~ /has_(a|the)_name_(that|which)_begins_with?/ has_a_name_that_begins_with?(args) else super end endendshimada = Person.new(“Shimada”)puts “Match” if shimada.has_a_name_that_begins_with?(“S”)puts “Match” if shimada.has_the_name_that_begins_with?(“S”)puts “Match” if shimada.has_a_name_which_begins_with?(“S”)puts “Match” if shimada.has_the_name_which_begins_with?(“S”)
    28. 28. Ruby Example: name 1ex4 name String has_(a|the)_(.+)_(which|that)_begins_with?Dynamic setter undef Singleton Methodclass Person def initialize(args) args.each do |key, value| self.class.send(:attr_accessor, :"#{key}") unless instance_variables.include?(:"@#{key}") instance_variable_set(:"@#{key}", value) end end def self.undef_has_a_var_which_begins_with(var) method_name = "has_a_#{var}_which_begins_with?" send("undef_method", method_name) if method_defined? method_name end def self.define_has_a_var_which_begins_with(var) method_name = "has_a_#{var}_which_begins_with?" define_method(method_name) do |expectation| value = instance_variable_get(:"@#{var}") if value.is_a?(String) value.split(//)[0].downcase == expectation.downcase else self.class.undef_has_a_var_which_begins_with(var) method_missing(method_name.to_sym) end end end def method_missing(m, *args, &block) if m.to_s =~ /has_(a|the)_(.+)_(which|that)_begins_with?/ && instance_variables.include?(:"@#{Regexp.last_match[2]}") && instance_variable_get(:"@#{Regexp.last_match[2]}").is_a?(String) var_name = Regexp.last_match[2] self.class.define_has_a_var_which_begins_with(var_name) send("has_a_#{var_name}_which_begins_with?".to_sym, *args, &block) else super(m, *args, &block) end endend#### sample #####shimada = Person.new(:name => "Shimada", :age => 18, :sex => :half, :fuga => "bababa")p shimada.has_a_name_which_begins_with?("S") ? "Good" : "Suck"p shimada.has_the_name_which_begins_with?("S") ? "Good" : "Suck"shimada.name = 5p shimada.has_the_name_which_begins_with?("5") ? "Good" : "Suck" #=> undefined method
    29. 29. if air_plane.is_created_by? :bowing if he.is_eligible_to_get? reward if selected_value.is_valid?→ can_..., is_,
    30. 30. is does was didmodule Kernel def is(expected) expected end alias_method :does, :is alias_method :was, :is alias_method :were, :is alias_method :did, :is # alias_method :do, :is × doendis air_plane.created_by?(bowing)does [1,2,3].include?is he.eligible_to_get?(bonus)does [1,2,3].include?is "h".included_in?("hello")is she, nancy # ?
    31. 31. Conclusion & Impression• Spec•• • Receiver Array#sort • Ruby Standard Library if obj.instance_of?(exp) be (Is Object#is Is#method_missing self return...) • • •
    32. 32. thank you :)

    ×