Given at Boulder Ruby Group in February 2012. A presentation examining DSLs, what they might be teaching us or especially new programmers, and some common tactics or methodologies for creating them.
• A lingual subset dedicated to a particular problem (domain) area • Code that makes it easier to work on specific (types of) problems • A specialized tool
| topic.acronym true , [ "Domain" , "Specific" , "Language" ] topic.definition "A lingual subset dedicated to a particular problem (domain) area" topic.definition "Code that makes it easier to work on specific (types of) problems" topic.definition "A specialized tool" end
a powerful and declarative abstraction around a specific set of underlying library/code functionality • From @justicefries -- "Provide a means for developers to effectively communicate with each other and domain experts in a domain."
Sims - "Programming is the new literacy" ◦ http://bit.ly/wO7UP3 • "Well understood problems lend themselves to DSLs" ◦ http://bit.ly/wCqFKk • "Human beings, as tool makers, can create things that amplify our ability" ◦ Steve Jobs (1980) re: Scientific American study about inter-species locomotive efficiency ◦ http://bit.ly/wE6HyH
teach someone to code is within a meaningful context, where they are given an existing solution and guided how to alter and customise it to their preferences or requirements, as often required in the “real world”." ◦ Andy Young of Kernel Mag on "Coding For Success" ◦ http://bit.ly/xGxDAn
of methods through which to communicate (GET, POST, etc.) • Understand that you have access to certain things in an HTTP request • Understand that there are specific ways to respond to HTTP requests (response codes) • Understand that there are parts of an HTTP response that you can control to have the desired effect
• Understand the power behind callbacks and how they can help you manage your data • Understand validations and how they can help you keep your data standardized
easier to understand how to accomplish a problem though programming • They lower the barrier to entry • Help introduce people to programming concepts at a more gradual pace • Provide more output for less input
it easier • People know problem domains, but they don't always know how to program • DSLs puts programming in a language that people understand inherently
Writing a ton of DSLs around the problems WE solve isn't necessarily helpful • If people can be taught how to use DSLs then they're one step closer to being a programmer
def self.parse_file( config , opts = Server::Options.new ) ... other code ... # cfgfile is the result of a File::read app = eval \ "Rack::Builder.new {\n" + cfgfile + "\n}.to_app", TOPLEVEL_BINDING, config ... other code ... end end end
module Loading; module ClassMethods def load( *params , &block ) ... other code ... # options[ :string ] is the result of a File::read instance_eval options[:string] , options[:name] || "<eval>" ... other code ... end end; end end; end
module Routing class RouteSet def draw( &block ) eval_block block end def eval_block( block ) Mapping.new( self ).instance_exec &block end end end; end
`class_variable_set` as well as their instance variable counterparts • `Forwardable` and `def_delegators` • Defining things directly on the included class with `Module::included`