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.
Simple Ruby DSL Techniques:
Big Project Impact!
Aman King
king@thoughtworks.com
Obligatory Humour…

No offence intended toward any community; hint of satire
What is common between
Beggars & Developers?
When two Beggars meet at
a railway station…
Or when two Developers meet
at a tech conference…
Both introduce
themselves…

with the question…
“What platform do you
work on?”
2
1

Java

Ruby
“What domain do you
work on?”
Finance

Retail
What about…
Implementation Concerns
Technical Domain
Technical Domain?
Business Domain?
What is a Domain?
Characteristics of a Domain
• Specialized field
• Common context shared by specialists
• Common vocabulary among specialis...
Characteristics of a Domain
• Specialized field
• Common context shared by specialists
• Common vocabulary among specialis...
“Is this method private?”

“Let’s simply delegate these
methods!”
“Is this method private?”

“Let’s simply delegate these
methods!”
“Is this method private?”

DSL!
“Let’s simply delegate these
methods!”
“Is this method private?”

DSL in core Ruby!
“Let’s simply delegate these
methods!”
More commonly cited
DSL examples…
ActiveRecord
ActionController
Sunspot
All Ruby DSLs
for Technical Domains
Where are the DSLs for
Business Domains?
Well, framework writers
don’t write them!
framework writers can only guess
generic technical domains
You need to write the DSLs
for your Business Domain!
only you know the business problem
your project solves
Why write them?
Well, why not!
Enough fluff,
now stuff…
Recognize your DSLs!
Recognize your DSLs!
Fluent Interface
Recognize your DSLs!
Single-line Declaration
Recognize your DSLs!
Block Declaration
Recognize your Secret Sauce!
Recognize your Secret Sauce!

Fluent Interface
Fluent Interface
Method Chaining & Builder Pattern
Recognize your Secret Sauce!

Single-line Declaration
Single-line Declaration
Class Methods & Metaprogramming
Recognize your Secret Sauce!

Block Declaration
Block Declaration
Class/Instance Methods & Metaprogramming
Some real-world
inspirations…
Fluent Interface
Domain: Production Support

https://github.com/amanking/ruby_dsl_talk_companion
More sneak peeks…
Single-line Declaration
Domain: Community Forum

https://github.com/amanking/ruby_dsl_talk_companion
More sneak peeks…
Block Declaration
Domain: Social Networking

https://github.com/amanking/ruby_dsl_talk_companion
More sneak peeks…
Secret Sauce Recap
Secret Sauce Recap
Fluent Interface
• simple object-oriented code
• builder pattern
• method chaining
Secret Sauce Recap
Single-line Declaration
• modules as mixins
• class-level methods
• basic metaprogramming
– define_meth...
Secret Sauce Recap
Block Declaration
• modules as mixins
• class-level methods
• builder pattern
• advanced metaprogrammin...
But mind it!
you win some, you lose some
Guidelines & Gotchas
Guidelines & Gotchas
Builder Pattern

• not everything needs a builder
• keep builder class separate from built class
• al...
Guidelines & Gotchas
Metaprogramming

•
•
•
•
•

do not shy away but be vigilant
split metaprogramming responsibilities
ex...
Guidelines & Gotchas
Possible Pains
• debugging (unit tests help but not always)
• understanding a metaprogramming-based
D...
Let’s talk benefits!
observed in real-world projects
Benefits
Productivity
• full cycle of user stories completed in half day or less
• reusable code
Maintenance
• improved re...
Final words
Final words

Custom DSLs for Business Domains
Final words

Custom DSLs for Business Domains

Just try it!
Thanks!

amanking.com
github.com/amanking
bitbucket.org/amanking
slideshare.net/amanking
Simple Ruby DSL Techniques: Big Project Impact!
Simple Ruby DSL Techniques: Big Project Impact!
Simple Ruby DSL Techniques: Big Project Impact!
Simple Ruby DSL Techniques: Big Project Impact!
Simple Ruby DSL Techniques: Big Project Impact!
Upcoming SlideShare
Loading in …5
×

Simple Ruby DSL Techniques: Big Project Impact!

1,455 views

Published on

Talk that introduces simple DSL techniques in Ruby. Developers are encouraged to apply such techniques for their project's business domain to gain benefits usually received from DSL-ish APIs of frameworks.

Presented at Garden City RubyConf 2014 in Bangalore, India.

Published in: Technology

Simple Ruby DSL Techniques: Big Project Impact!

  1. 1. Simple Ruby DSL Techniques: Big Project Impact! Aman King king@thoughtworks.com
  2. 2. Obligatory Humour… No offence intended toward any community; hint of satire
  3. 3. What is common between Beggars & Developers?
  4. 4. When two Beggars meet at a railway station…
  5. 5. Or when two Developers meet at a tech conference…
  6. 6. Both introduce themselves… with the question…
  7. 7. “What platform do you work on?” 2 1 Java Ruby
  8. 8. “What domain do you work on?” Finance Retail
  9. 9. What about…
  10. 10. Implementation Concerns
  11. 11. Technical Domain
  12. 12. Technical Domain? Business Domain? What is a Domain?
  13. 13. Characteristics of a Domain • Specialized field • Common context shared by specialists • Common vocabulary among specialists
  14. 14. Characteristics of a Domain • Specialized field • Common context shared by specialists • Common vocabulary among specialists
  15. 15. “Is this method private?” “Let’s simply delegate these methods!”
  16. 16. “Is this method private?” “Let’s simply delegate these methods!”
  17. 17. “Is this method private?” DSL! “Let’s simply delegate these methods!”
  18. 18. “Is this method private?” DSL in core Ruby! “Let’s simply delegate these methods!”
  19. 19. More commonly cited DSL examples…
  20. 20. ActiveRecord
  21. 21. ActionController
  22. 22. Sunspot
  23. 23. All Ruby DSLs for Technical Domains
  24. 24. Where are the DSLs for Business Domains?
  25. 25. Well, framework writers don’t write them! framework writers can only guess generic technical domains
  26. 26. You need to write the DSLs for your Business Domain! only you know the business problem your project solves
  27. 27. Why write them?
  28. 28. Well, why not!
  29. 29. Enough fluff, now stuff…
  30. 30. Recognize your DSLs!
  31. 31. Recognize your DSLs!
  32. 32. Fluent Interface
  33. 33. Recognize your DSLs!
  34. 34. Single-line Declaration
  35. 35. Recognize your DSLs!
  36. 36. Block Declaration
  37. 37. Recognize your Secret Sauce!
  38. 38. Recognize your Secret Sauce! Fluent Interface
  39. 39. Fluent Interface Method Chaining & Builder Pattern
  40. 40. Recognize your Secret Sauce! Single-line Declaration
  41. 41. Single-line Declaration Class Methods & Metaprogramming
  42. 42. Recognize your Secret Sauce! Block Declaration
  43. 43. Block Declaration Class/Instance Methods & Metaprogramming
  44. 44. Some real-world inspirations…
  45. 45. Fluent Interface
  46. 46. Domain: Production Support https://github.com/amanking/ruby_dsl_talk_companion
  47. 47. More sneak peeks…
  48. 48. Single-line Declaration
  49. 49. Domain: Community Forum https://github.com/amanking/ruby_dsl_talk_companion
  50. 50. More sneak peeks…
  51. 51. Block Declaration
  52. 52. Domain: Social Networking https://github.com/amanking/ruby_dsl_talk_companion
  53. 53. More sneak peeks…
  54. 54. Secret Sauce Recap
  55. 55. Secret Sauce Recap Fluent Interface • simple object-oriented code • builder pattern • method chaining
  56. 56. Secret Sauce Recap Single-line Declaration • modules as mixins • class-level methods • basic metaprogramming – define_method – alias_method
  57. 57. Secret Sauce Recap Block Declaration • modules as mixins • class-level methods • builder pattern • advanced metaprogramming – – – – instance_eval class_eval define_method def
  58. 58. But mind it! you win some, you lose some
  59. 59. Guidelines & Gotchas
  60. 60. Guidelines & Gotchas Builder Pattern • not everything needs a builder • keep builder class separate from built class • always setup reasonable default values
  61. 61. Guidelines & Gotchas Metaprogramming • • • • • do not shy away but be vigilant split metaprogramming responsibilities extra focus on readability control options explosion drive using tests or duplication removal
  62. 62. Guidelines & Gotchas Possible Pains • debugging (unit tests help but not always) • understanding a metaprogramming-based DSL implementation • dealing with variations • not everyone may “get” the beautiful DSL you write
  63. 63. Let’s talk benefits! observed in real-world projects
  64. 64. Benefits Productivity • full cycle of user stories completed in half day or less • reusable code Maintenance • improved readability & consistency • less defects • quick fixes Knowledge • deeper Ruby know-how for team members • deeper business domain exploration by team members • quick ramp-up for new team members
  65. 65. Final words
  66. 66. Final words Custom DSLs for Business Domains
  67. 67. Final words Custom DSLs for Business Domains Just try it!
  68. 68. Thanks! amanking.com github.com/amanking bitbucket.org/amanking slideshare.net/amanking

×