经过深思熟虑的方法/函数名称是否会创建 DSL?
我在想,方法名称及其调用似乎通过包装通用内容并根据您想要实现的目标对其进行适当命名,在您的代码中创建了 DSL。
您知道,因此很容易推理出以下内容的含义
if (a.isSubReportOf(b) || b.isSubReportOf(a)) {
// do stuff
}
,但是如果不研究方法中的代码可能真的很难解释。
我知道人们有时认为整个 DSL 事物有一些特别之处 - 我们是否一直在代码中创建它们?
I was thinking, method names and their calls seem to create a DSL in your code, by wrapping up the generic stuff and naming it appropriately for what you're trying to achieve.
You know, so it's easy to reason about what the following means
if (a.isSubReportOf(b) || b.isSubReportOf(a)) {
// do stuff
}
but the code in the methods might be really difficult to interpret without studying it.
I know that people sometimes think there's something special about this whole DSL thing - do we create them in code all the time?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为您指的是 Martin Fowler(也许还有其他人)所说的内部 DSL。 请阅读此内容。
I think you are referring what Martin Fowler (and maybe others) call an Internal DSL. Read this.
您也可以询问 DSL 是否应该替换为经过深思熟虑的类、变量和方法名称。 我总是将这一点与 Kent Beck 联系起来,因为他一直是“编写读起来像诗歌的代码”的支持者。
您正在发明领域词汇,将业务操作的语法制作成对象、方法和变量。 对我来说听起来像 DSL ;)
You could ask alternately if DSL's should be replaced with well thought out class, variable and methods names. I have always come to associate this with Kent Beck, because he's always been a proponent of "writing code that reads like poetry".
You are inventing the domain vocabulary, crafting the grammar of the business operations into objects, methods and variables. Sounds DSL'ish to me ;)
这取决于你对 DSL 的定义,但对我来说,是的,我会这么说。 DDD 就是创建抽象,使您能够拥有一种普遍存在的语言并在更高层次上推理概念。 有些人可能会说它本身不是 DSL,但更重要的问题是,这样的代码是否对域进行推理?
我会说是的,这才是真正重要的。
It depends on your definition of DSL, but for me, yes I'd say so. DDD is all about creating abstractions which allow you to have a ubiquitous language and reason about concepts on a higher level. Some may argue it's not a DSL per-se, but the more important question is, is code like this reasoning about the domain?
I'd say yes, and that's what really matters.
在足够复杂或简单的情况下,你能用另一种来代替吗? 是的,
它们是一样的吗? 一点也不。
每种方法都有优点和缺点,这完全取决于需要什么。 如果您需要它在运行时可修改,那么使用编译语言中的函数可能不是最好的解决方案(尽管它仍然是可能的),因为它对于您的需求来说太重了。
DSL 的目标是查明您的需求并创建一种仅包含这些需求的语言,以便尽可能简单地表示。 这样,我们有限的大脑就可以轻松地想象出数据是什么,而无需编译程序。
此外,足够简单的 DSL 可以很容易地被另一个程序解析和编辑。 毕竟,您制定了语法并将其封装在库中,对吧?
在上面的例子中:
在心理上仍然比说更难理解:
Can you replace one by the other in a sufficiently complex or simple situation? Yes
Are they the same? Not at all.
Each have pros and cons and it all depends on what is needed. If you need it to be modifiable at run time, then using function in a compiled language might not be the best solution (it's still possible though) as it will be too heavy for your needs.
The goal of a DSL is to pinpoint your needs and make a language that only encompass those needs so that it's as simple as possible to represent. That way, our limited brains can easily picture what the data is without having to compile a program.
Also, a simple enough DSL could easily be parsed and edited by another program. After all, you made the syntax, and encapsulated it in a library, right?
In your example above:
is still harder to grasp mentally than say: