如何在项目和/或语言中不重复自己
我正在使用不同的编程语言从事几个不同但相关的项目。 其中一些项目需要解析其他项目编写的文件名,并期望某种文件名模式。
该模式现在在多个位置和多种语言中进行了硬编码,使其成为维护炸弹。 在给定项目中一次定义此模式相当容易,但是对于所有项目和使用的所有语言一劳永逸地定义它的技术是什么?
I'm working on several distinct but related projects in different programming languages. Some of these projects need to parse filenames written by other projects, and expect a certain filename pattern.
This pattern is now hardcoded in several places and in several languages, making it a maintenance bomb. It is fairly easy to define this pattern exactly once in a given project, but what are the techniques for defining it once and for all for all projects and for all languages in use?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
将模式放入数据库中 - 最简单、舒适的方法是使用 XML 数据库。 所有项目都可以访问该数据库,并且他们将从那里读取模式
Put the pattern in a database - the easiest and comfortable way could be using XML database. This database will be accessible by all the projects and they will read the pattern from there
我不知道你说的是哪种语言,但大多数语言都可以使用外部动态库 dll/共享对象并从此库导出常用功能。
例如,您在简单的 c lib 中实现函数获取文件名并使用 acrros 其余语言。
另一种选择是动态创建通用代码作为每种语言构建过程的一部分,这不应该太复杂。
如果可行的话,我建议使用动态链接方法(您没有提供足够的信息来确定这一点),因为维护这个解决方案比维护不同语言的代码生成要容易得多。
I don't know which languages you are speaking about but most of languages can use external dynamic libraries dlls/shared objects and export common functionality from this library.
For example you implement function get file name in simple c lib and use acrros rest of languages.
Another option will be to create common code dynamically as part of the build process for each language this should not be to complex.
I will suggest using dynamic link approach if feasible (you did not give enough information to determine this),since maintaining this solution will be much easier then maintaining code generation for different languages.
您可以使用通用脚本、进程或 Web 服务来生成文件名(取决于您的设置)。
You could use a common script, process or web service for generating the file names (depending on your set-up).
我会将模式存储在一个简单的文本文件中,并根据特定的项目:
编辑:I假设该模式并不比正则表达式更复杂,否则我会使用另一个答案中的 DSL 解决方案。
I'd store the pattern in a simple text file and, depending on a particular project:
Edit: I assume the pattern is something no more complicated than a regex, otherwise I'd go with the DSL solution from another answer.
创建领域特定语言,然后将其编译成您正在使用的每种目标语言的代码将是最好的解决方案(也是最优雅的)。
制作 DSL 并不困难 - 将其嵌入到某些东西中(比如在 Ruby 中,因为它现在很流行,或者像 LISP/Haskell 这样的另一种语言......),或者从头开始创建语法(使用 Antlr?) 。 看来工程量很大,那么这条路值得你走一走。
Creating a Domain Specific Language, then compile that into the code for each of the target languages that you are using would be the best solution (and most elegant).
Its not difficult to make a DSL - wither embed it in something (like inside Ruby since its the 'in' thing right now, or another language like LISP/Haskell...), or create a grammar from scratch (use Antlr?). It seems like the project is large, then this path is worth your while.