在哪里放置规格Clojure.Spec? [英] Where to put specs for Clojure.Spec?

查看:141
本文介绍了在哪里放置规格Clojure.Spec?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我在 Clojure.Spec 中深入和深入。



我偶然发现的一件事是放置我的规格。我看到三个选项:



全局规格文件



在大多数示例中, spec.clj 文件,它在主命名空间中是必需的。它具有所有数据类型和函数的所有(s / def)(s / fdef)



Pro:







对照:




  • li>
  • 违反单一责任原则?



生产名称空间中的规格



您可以将(s / def)(s / fdef)您的生产代码。因此,实现和规范共存于同一命名空间。



Pro:




  • 实施和规范的共同位置

  • 一个命名空间 - 一个关注?



<
  • 一个命名空间 - 两个问题?

    li>


    专用规格命名空间结构



    然后我想,也许规格是第三种的代码(旁边的生产和测试)。所以也许他们应该有自己的命名空间结构,像这样:

     ├─src 
    │└─package
    │├─a.clj
    │└─b.clj
    ├─测试
    │└─包
    │├─a_test.clj
    │└─ b_test.clj
    └─spec
    └─包
    ├─a_spec.clj
    └─b_spec.clj

    Pro:




    • 规格的专用(但相关)命名空间



    对照:




    • 正确的命名空间






    谁有使用其中一种方法的经验?

    有另一个选项吗?

    你对不同的选项有什么看法?

    解决方案

    将specs放在自己的命名空间中,以及它们所描述的命名空间。它们并不特别重要,只要他们使用一些一致的命名约定。例如,如果我的代码在 my.app.foo ,我会把规格放在 my.app.foo.specs



    规范键名称最好放在代码的命名空间中,但不是规范的命名空间。这仍然很容易通过在关键字上使用命名空间别名:

     (ns my.app.foo.specs 
    (:require [my.app.foo:as f]))

    (s / def :: f / name string?)
    pre>

    我不会试图把一切放在一个巨大的规范命名空间(一个噩梦。)虽然我当然可以把他们正确的旁边的规范代码在同一个文件中,这损害了可读性IMO。



    您可以将所有spec命名空间放在单独的源路径中,但没有实际的好处这样做除非你在一个情况下,你想分发的代码,但不是规格,反之亦然,难以想象这是什么。


    So, I'm diving deeper and deeper into Clojure.Spec.

    One thing I stumbled upon is, where to put my specs. I see three options:

    Global Spec File

    In most examples, I found online, there is one big spec.clj file, that gets required in the main namespace. It has all the (s/def) and (s/fdef) for all the "data types" and functions.

    Pro:

    • One file to rule them all

    Contra:

    • This file can be big
    • Single Responsibliy Principle violated?

    Specs in production namespaces

    You could put your (s/def) and (s/fdef) right next to your production code. So that, the implementation and the spec co-exist in the same namespace.

    Pro:

    • co-location of implementation and spec
    • one namespace - one concern?

    Contra:

    • production code could get messy
    • one namespace - two concerns?

    Dedicated spec namespace structure

    Then I thought, maybe Specs are a third kind of code (next to production and test). So maybe they deserve their own structure of namespaces, like this:

    ├─ src
    │  └─ package
    │     ├─ a.clj
    │     └─ b.clj
    ├─ test
    │  └─ package
    │     ├─ a_test.clj
    │     └─ b_test.clj
    └─ spec
       └─ package
          ├─ a_spec.clj
          └─ b_spec.clj
    

    Pro:

    • dedicated (but related) namespaces for specs

    Contra:

    • you have to source and require the correct namespaces

    Who has experience with one of the approaches?
    Is there another option?
    What do you think about the different options?

    解决方案

    I usually put specs in their own namespace, alongside the namespace that they are describing. It doesn't particularly matter what they're named, as long as they use some consistent naming convention. For example, if my code is in my.app.foo, I'll put specs in my.app.foo.specs.

    It is preferable for spec key names to be in the namespace of the code, however, not the namespace of the spec. This is still easy to do by using a namespace alias on the keyword:

    (ns my.app.foo.specs
      (:require [my.app.foo :as f]))
    
    (s/def ::f/name string?)
    

    I'd stay away from trying to put everything in one giant spec namespace (what a nightmare.) While I certainly could put them right alongside the spec'd code in the same file, that hurts readability IMO.

    You could put all your spec namespaces in a separate source path, but there's no real benefit to doing so unless you're in a situation where you want to distribute the code but not the specs or vice versa... hard to imagine what that'd be though.

    这篇关于在哪里放置规格Clojure.Spec?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆