决明子有什么功效| 阴道恶臭是什么原因| 怀孕初期分泌物是什么样的| 水痘开始痒了说明什么| 什么食物对眼睛好| 肌酐是什么| 君子兰用什么土最好| 心慌吃点什么药| 妍字属于五行属什么| 向左向右向前看是什么歌| 产检建档需要什么资料| 7.9什么星座| 囹圄是什么意思| 头响脑鸣是什么原因引起的| 试管婴儿长方案是什么| 女人梦见蛇是什么预兆| acs是什么病| 通宵是什么意思| 孕妇白细胞高是什么原因| 厚黑学讲的是什么| hape是什么牌子| 人为什么不可以偏食| 血压低吃什么最快最有效| 赵云的武器叫什么| 81年属什么的| dears是什么意思| 送女生什么礼物比较好| 腱鞘囊肿是什么原因引起的| 舌苔很厚很白什么原因| 去湿气吃什么食物| 六畜兴旺是什么生肖| 体制外是什么意思| 沄字五行属什么| 出阁是什么意思| 白细胞酯酶弱阳性什么意思| 膝盖骨质增生用什么药效果好| queen是什么意思| 甘甜是什么意思| 什么是什么的摇篮| 晚上夜尿多是什么原因| 姜太公钓鱼愿者上钩是什么意思| 经常生闷气会得什么病| 补办港澳通行证需要什么材料| 淼是什么意思| 临床医学学什么| 蜂王浆是什么| pg是什么激素| 牙膏尾部的颜色代表什么意思| 乌鸡白凤丸有什么功效| 你算什么男人歌词| 什么是素质| 柿子什么时候成熟| 神经纤维是什么| 站桩有什么好处| 男怕初一女怕十五是什么意思| 腊猪脚炖什么好吃| 急性寻麻疹用什么药| 葛洲坝集团是什么级别| 阴阳两虚吃什么中成药| 肿瘤切开了里面是什么| 4岁小孩流鼻血是什么原因| 膝盖痛吃什么| camp医学上是什么意思| 左侧卵巢囊性回声是什么意思| 花甲炒什么配菜好吃| 特朗普为什么叫川普| 摘环后需要注意什么| 头孢治什么| 趴在桌子上睡觉有什么坏处| 什么叫非萎缩性胃炎| 玛尼是什么意思| 散光400度是什么概念| 12378是什么电话| 马拉色菌是什么| 左进右出有什么讲究| 西红柿可以做什么菜| 什么是试管婴儿| 什么是染色体| 站军姿有什么好处| 梦见被蛇咬了是什么意思| 一本线是什么意思| 脱落细胞学检查是什么| 艾灸肚脐有什么好处| 肛门被捅后有什么影响| 1936年中国发生了什么| 六月初二是什么星座| 慢性荨麻疹是什么症状| 手足口病吃什么药| 固液法白酒是什么意思| 单硬脂酸甘油酯是什么| 早上适合做什么运动| 大便干燥是什么原因| 早上喝一杯温开水有什么好处| 耀武扬威的意思是什么| 尿胆原阳性是什么意思| 什么的小手| 高密度脂蛋白是什么| 呃逆吃什么药| 扁桃和芒果有什么区别| 纪梵希属于什么档次| 壮志凌云是什么生肖| 泥鳅不能和什么一起吃| 什么好像什么一样| 身份证前六位代表什么| 什么算高危性行为| 跨宽穿什么裤子好看| 输卵管堵塞有什么样症状| 细菌性毛囊炎用什么药| 银屑病是什么引起的| 长脸适合什么眉形| 打call是什么意思| 35岁属相是什么生肖| 厄警失痣是什么意思| 未免是什么意思| 大暑吃什么| modal是什么意思| 曹丕为什么不杀曹植| 什么叫义齿| 青梅竹马什么意思| 大小休是什么意思| 胃出血大便是什么颜色| 芝五行属什么| 发烧吃什么水果| 小太阳是什么牌子| 手一直脱皮是什么原因| 现在创业做什么好| 耦合是什么意思| 今天适合穿什么衣服| 舒肝健胃丸治什么病| 舞是什么结构| 荨麻疹是什么症状| 十二指肠溃疡是什么原因引起的| 胃切除手术后吃什么好| 高汤是什么意思| 雪蛤是什么| 精索静脉曲张挂什么科| 鼻尖长痣代表什么| 奥利奥是什么意思| 狐臭是什么原因引起的| 腿麻脚麻用什么药能治| 智障什么意思| 什么情况下月经推迟| 糖类抗原什么意思| 爱出油的人身体缺什么| 菀字五行属什么| 尿精是什么原因造成的| 崩是什么意思| 1962属什么生肖| 什么时间量血压最准确| 北边是什么生肖| 农业户口和居民户口有什么区别| 甲亢病是什么原因引起的| 舌头裂开是什么原因| 技校算什么学历| 身份证什么时候开始有的| 血红蛋白低吃什么可以补起来| 老鼠疮是什么病| 灵芝的功效与作用是什么| 矿泉水敷脸有什么作用| 排骨汤里放什么食材好| 鸡翅木是什么木头| 嘴巴长溃疡是什么原因| 动土破土是什么意思| 吃什么补精子| 老板娘是什么意思| 做梦抓到很多鱼是什么征兆| 什么食物属于发物| 嘴角开裂是什么原因| 体质是什么意思| 雪茄是什么| 梦见前男友死了是什么意思| 翊什么意思| 餐后胆囊是什么意思| bff是什么意思| 鸡蛋和什么炒好吃| 席梦思床垫什么牌子好| 捡肥皂什么意思| 肚脐眼连着什么器官| 双皮奶是什么做的| nt检查需要注意什么| 炎黄子孙是什么生肖| 瑶五行属性是什么| 办电话卡需要什么| 蜗牛吃什么食物| 吃什么补叶酸最快| 11月29日什么星座| 卵子是什么| 珞字五行属什么| 突然晕倒是什么原因造成的| 什么容易误诊为水痘| 凝血四项是检查什么的| 什么叫玄学| 梦见跟别人打架是什么意思| 什么是海拔| 红花是什么| 烟酰胺有什么作用| 点痣不能吃什么东西| 处女膜破了什么症状| 为什么晒太阳能补钙| 珀莱雅适合什么年龄| ipmn是什么意思| 浑身乏力什么病的前兆| 小鸡仔吃什么| 吃什么降血糖快| 骨质增生的症状是什么| 蒙羞是什么意思| 黄瓜什么时候种植| 河南南阳产什么玉| 忧心忡忡是什么意思| 脸上发痒是什么原因| 脚背发麻是什么原因引起的| 中医减肥挂什么科| bj是什么意思| 秋葵与什么菜相克| 半夜猫叫有什么预兆| 肝疼吃什么药好| 银梳子梳头有什么好处| 幽门杆菌的症状是什么| senda是什么牌子| 4月12号是什么星座| 功能是什么意思| k2是什么| 1月21日什么星座| 美商是什么意思| 头臂长是什么意思| 单活胎是什么意思| 双生痣是什么意思| 喜欢是什么| 突然发胖要警惕什么病| 婴儿流鼻涕吃什么药| 梦见一条大蟒蛇是什么征兆| 什么是腐女| 胆固醇偏高是什么原因| 大便水状是什么原因| 叶酸起什么作用| 心窝窝疼是什么原因| 殊途同归什么意思| 炁怎么读什么意思| 猫弓背什么意思| 市公安局长什么级别| 笃行是什么意思| 大表哥是什么游戏| 子鼠是什么意思| 胳膊脱臼什么症状| 吃皮蛋不能和什么一起吃| 牙齿松动是什么原因引起的| 619是什么意思| 阴囊湿疹用什么药效果最好| u熊是什么意思| 奥运会五环颜色分别代表什么| 什么是走婚| 6月27号是什么星座| 牛油果吃了有什么好处| 狗为什么会咬人| 皮肤黄什么原因| 拉肚子可以吃什么食物| 长骨刺是什么原因导致的| 药食同源什么意思| 梦到狐狸是什么意思| 10月是什么星座| 高密度脂蛋白胆固醇偏低什么意思| 中指和无名指发麻是什么原因| 老年人吃饭老是噎着是什么原因| 宝宝热疹用什么药膏| 百度
BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News What's a Ruby DSL and what isn't?

捷创技术(股票代码831817)新三板上市最新公告列表

The term Domain Specific Language (DSL) has become very popular in the Ruby space. One popular example is the Make replacement Rake, particularly after an article by Martin Fowler  about it. The term DSL is also used to describe some of the concepts in ActiveRecord, which is part of Ruby on Rails.

The concept of a DSL is actually unrelated to Ruby, and has been around for a long time. It's generally used to describe a language that has constructs suited for expressing concepts of a particular domain. The build tool Make, for instance, has a language that allows to declaratively define build targets, how to build them and dependencies between them. Then there is the concept of an internal DSL, which uses the syntax of an ex?sting language, a host language, such as Ruby. The means of the language are used to build constructs resembling a distinct language. The, already mentioned, Rake uses this to make code like this possible:
task :codeGen do        
# do the code generation
end
As can be seen, the host language must be quite flexible to allow the DSL to have a distinct look.

Recently, however, the term DSL seems to have become overused, causing it to become vague. It seems that omitting parentheses from function calls already qualifies a piece of code to be labeled a DSL. O'Reilly blogger chromatic came up with  a tongue-in-cheek checklist for deciding if a piece of code is a DSL, or at least when it's labeled as one. Here a sample:
1. Have you ever programmed in a language other than Ruby? (PHP and HTML don’t count.) If not, it’s a DSL.
2. Is the defining syntactic feature that you’ve cleverly left the parentheses off of a list of function arguments? If so, it’s a DSL.
3. Is the code primarily a list of key-value pairs? Welcome to DSL Town, population you!
#5 on chromatic's list is particularly telling:
5. Have you ever used the phrase “… and it reads just like English!” in seriousness? You’d better get to the hospital; you’re coming down with a case of the DSLs!
This is a popular argument for creating DSLs: they allow to write code that is easy to read and understand and doesn't hide its meaning behind language constructs such as for loops, conditionals, et?. However, David A. Black offers another view of this:
This:

with Employee "123-45-6789" do
 dock_salary 1000
 warn_about :misconduct
end

is not a domain specific language. It is, rather, domain specific language. Note the lack of the “a”. It’s domain specific, and it uses language-like constructs. But it isn’t a language. It’s Ruby, using expressive method names and cushy semantics and therefore facilitating the use of domain specific idioms.
Using a domain specific vocabulary for naming code elements isn't a new concept, instead it should be an obvious choice for a designer.

So, is the term DSL bad or misleading? Not necessarily. Smalltalk developer Blaine Buxton shares his view:
I've always wondered what the big deal with DSLs was. Now, i'm not saying they are bad quite the contrary.But, I believe a DSL is a healthy bi-product of a good object-oriented design. So, I was a little annoyed with all of the talk and the tricks. I kept thingking to myself, "But, if you did a good design, you would have this!"

But, I should really be kicking myself. I should be glad that good design is back in vogue. and you know what? I am. I'm not annoyed any long and I relish all of this new talk on DSLs. I even feel guilty. You see, DSLs have always been part of the Smalltalk farbic. It's natural to us because it's the way we have learned to code. I remember having the mantra "Code should read like a conversation" shoved down my throat until it became second nature. The cool thing is that there is a whole new generation finding out about this and doing it.
Languages with syntax that can be twisted/bent to allow for more concise code are useful, but good design still depends on the developer.

There is another side to this. Some DSLs make use of Ruby constructs and corner cases of Ruby syntax. While this allows the developer to achieve a certain look, this doesn't mean that this is a good design. After all, the fact that code is written once, but read and modified much more often, is certainly still true.

A recent article on Urban Honking started of a debate about this. The article explains how the HTML parser library Hpricot makes this code possible:
Hpricot(my_document) 
Basically, there's a function called Hpricot which creates a new object of the class Hpricot and parses my_document.  Smalltalk blogger James Robertson, however, takes issue with that:
Urban Honking goes to great pains to explain why using syntax tricks in Ruby to get to something like this: Hpricot(my_document) Is a good thing. Here's a question - if you stumbled on that in code, would you have any idea what it did?
After explaining how to improve the code, he continues:
I've out-clevered myself in Smalltalk many times; it's never a good idea.
This view is also backed by Stuart Halloway:
If you are going to bend a language's idiomatic usage, you should have a compelling reason, and I share James' view that this example is not compelling.
He goes on to give an interesting overview of the many different solutions to API design, and how to design an API to make it possible to write expressive code with it.

What is your opinion? Where do you draw the line between good API design, that makes code easy to read, and using tricks that save a few key strokes but make code maintenance difficult? Are there ways to check that you've strayed from good design into the realm of hacking and clever syntax tricks?

Rate this Article

Adoption
Style

BT
bioisland是什么牌子 药流没流干净有什么症状 嘴巴里长泡是什么原因 人民检察院是国家的什么机关 结婚5年是什么婚
吃什么东西可以长高 右肺下叶纤维灶是什么意思 占位性病变是什么意思 白带多是什么原因引起的 梦见自己杀人了是什么意思
息肉是什么 低密度脂蛋白低是什么原因 胚胎停育是什么原因造成的 后腰出汗多是什么原因 诚字属于五行属什么
m是什么牌子 人参果吃了有什么好处 没胃口吃点什么好 眼睛干涩疼痛用什么滴眼液好 2006年属狗的是什么命
头晕吃什么食物好hcv9jop2ns2r.cn 孟母三迁的故事告诉我们什么道理hcv9jop4ns8r.cn 丝芙兰属于什么档次hcv9jop5ns2r.cn 丰富是什么意思hcv8jop8ns6r.cn notebook什么意思hcv9jop6ns8r.cn
中国第一个不平等条约是什么hcv7jop6ns8r.cn 锦囊妙计是什么意思hcv9jop6ns1r.cn 脚趾发紫是什么原因hcv7jop4ns5r.cn 打呼噜是什么原因引起的hcv9jop0ns7r.cn 槟榔为什么会上瘾hcv8jop5ns1r.cn
什么地移入hcv8jop1ns4r.cn 吃完饭就犯困是什么原因hcv9jop0ns0r.cn 鲁班是什么家ff14chat.com 什么是五常大米hcv8jop5ns0r.cn 5.16号是什么星座hcv9jop3ns4r.cn
尿常规是检查什么的hcv9jop2ns6r.cn 甲亢吃什么食物好hcv9jop4ns8r.cn 烤麸是什么做的jinxinzhichuang.com 乙肝235阳性是什么意思hcv9jop5ns1r.cn 三油甘脂是什么hcv8jop4ns1r.cn
百度