Lock in $30 Savings on PRO—Offer Ends Soon! ⏳

How NOT to make your DSL terrible

How NOT to make your DSL terrible

Presented at https://reddotrubyconf.com/
Video URL: [PLACEHOLDER]

If you're interested in DSL or you HATE DSL, this talk is for you.

Masafumi Okura

July 26, 2024
Tweet

More Decks by Masafumi Okura

Other Decks in Programming

Transcript

  1. How NOT to make your DSL terrible Red Dot Ruby

    Conference 2024 OKURA Masafumi, 2024-07-26
  2. Some top comments on that Reddit post • “I spend

    a lot of time reverse engineering some 34-GitHub-star gem’s meta programming” • “I know this is often an unpopular opinion, but I've grown to hate DSL's. They are almost NEVER good, solve the wrong problem, and generally badly.” • “Most of the time, though, if I see a gem with a DSL, I try to fi nd something else and only use the DSL based gem when its absolutely necessary.”
  3. How NOT to make your DSL terrible Red Dot Ruby

    Conference 2024 OKURA Masafumi, 2024-07-24 That's why I wanted to give this talk, to defend DSL
  4. Today we are going to talk about: 1. What is

    DSL, why is it useful 2. Terrible DSL 3. Good DSL 4. General ideas NOT to make your DSL terrible
  5. Dev.introduce self • Name: OKURA Masafumi (Masafumi is the fi

    rst name) • From: Tokyo, Japan • Work as: freelancer • Activities: Kaigi on Rails, Alba gem, public speaking (EuRuKo 2023, RubyConfTw 2023, etc.), OSS contributions (Ruby/Rails/RDoc, etc.), Rails Girls Coach (more than 10 times) • Like: Clean code, refactoring, testing (RSpec and Minitest), DSL
  6. 3 reasons why Ruby is great for DSL • Parenthesis-less

    method call • `describe(‘description’)` doesn’t look like DSL, more programs • Block • It structures DSL with variety of ways, and `do~end` doesn’t interrupt our reading, `{}` style is even better • Less restrictions of naming • `if` option works, `update!` and `updated?` work
  7. Why is it bad? • You cannot tell what happens

    from DSL name • It’s a “language”, so it’s useless unless we all know what it means • It does too many things • It involves Model, View and Controller • Limited customizability • Hard-to-remember options, still not possible for full customization
  8. Why is it (so) bad? • You cannot tell what

    happens from DSL name • `crud` de fi nes 7 actions, maybe? Or, maybe 4? • It does too many things • It creates controllers and views • Limited customizability • How to de fi ne custom views or actions?
  9. Why is it good (as far as I believe) •

    Predictable and understandable • `attributes` declaring “attributes” for the object • `association` and `nested` are straightforward • `root_key` de fi nes a root key • Each DSL has one thing to do • Easy-to-understand options
  10. Good DSL has a target domain that is… • simple

    • Usually one word or a short phrase, such as “ JSON serialization” • isolated • It doesn't affect other domains in a complex way • self-contained • You don’t have to know other things than the domain
  11. JSON serialization domain is… • simple • One short phrase

    • isolated • Input is an object, output is the JSON representation • self-contained • You don’t have to be familiar with Rails controller to use Alba
  12. Details of options and methods from Alba • Alba provides

    `if` option instead of `conditional_attributes` method • `if` option is intuitive, not worth adding another DSL method • Alba provides `nested` method instead of `nested` option • `nested` option didn’t make sense (at least to me) • `if` option is applied to `attributes`, `attribute` and `association` • Inference works, easy to remember
  13. General ideas NOT to make your DSL terrible 1. Stop

    and think again about whether it’s a simple, isolated and self- contained domain 1. If the domain is too complex or intricate, just use normal code 2. Keep each DSL method simple and let them do one thing well 1. Pick the right word that’s intuitive, readable and understandable 3. Provide options so that it’s fl exible enough to customize 1. Options also should be intuitive
  14. Good naming • One, dictionary-de fi ned word is always

    preferable • “association” is better than “de fi ne_association” • Do not use self-invented word unless it’s a ubiquitous language • Users of the DSL must understand it • Verbs/nouns for DSL methods, adverbs/adjectives for options • `attributes :some, if: …`, `has_many :books, through: …`
  15. There are only two hard things in Computer Science: cache

    invalidation and naming things. -- Phil Karlton
  16. Hire me! … maybe not, but talk to me! https://www.linkedin.com/in/masafumi-

    okura-82651128/ https://ruby.social/@okuramasafumi