在 Ruby 中命名命名空间的首选方式(更好的风格)是什么?单数还是复数?
有何优缺点
FooLib::Plugins
FooLib::Plugins::Bar
对您而言,使用:与
FooLib::Plugin
FooLib::Plugin::Bar
命名约定 ?你会使用什么或者你正在使用什么?社会上比较常用的是什么?
What are for you the pros and cons of using:
FooLib::Plugins
FooLib::Plugins::Bar
vs.
FooLib::Plugin
FooLib::Plugin::Bar
naming conventions? And what would you use or what are you using? What is more commonly used in the comunity?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用:
或者换句话说:
也像这样排列文件:
这是 Rails 是如何做到的 (所以这是铁路方式)。即查看 Associations 命名空间和 Associations::Association 类 构成 Associations 命名空间的所有类均从中继承(即关联::SingularAssociation)。
Use:
or in a different words:
Also arrange the files like this:
This is how Rails does it (so this is the Rails Way). I.e. look at the Associations namespace and the Associations::Association class from which all of the classes form the Associations namespace inherits (i.e. Associations::SingularAssociation).
对我来说,
FooLib::Plugins
看起来像一个模块,用作保存各种插件类的命名空间。FooLib::Plugin
看起来像 FooLib 插件的超类。在
FooLib::Plugins::Bar
中,Bar
看起来绝对像是插件的名称。对于FooLib::Plugin::Bar
,我会怀疑Bar
是否是Foo::Plugin
使用的辅助类,或者名称一个插件的。To me
FooLib::Plugins
appears like a module, used as a namespace which various plugin classes are kept in.FooLib::Plugin
looks like a superclass for FooLib plugins.In
FooLib::Plugins::Bar
,Bar
definitely seems like the name of a plugin. WithFooLib::Plugin::Bar
, I would be doubtful whetherBar
was a helper class used byFoo::Plugin
, or the name of a plugin.假设
Plugin
是基类:class FooLib::Plugin::Bar
class FooLib::Plugin::Bar
class FooLib::Plugin::Bar
class FooLib::Plugin::Bar
FooLib::插件
这是我使用和推荐的。
Bar
是FooLib
中的一个Plugin
并且它继承自FooLib::Plugin
。它还将 FooLib 库提供的插件嵌套在通用类的命名空间下,读起来很自然:如果我要为您的库开发第三方插件,我将创建以下结构:
请注意,我镜像了
FooLib
层次结构,但位于BarLib
的命名空间下。我不会直接扩展它。类 FooLib::Plugins::Bar < FooLib::插件
我也用过这个,我觉得这个最有意义。
Bar
扩展了FooLib::Plugin
,并且是FooLib
提供的Plugins
之一。然而,它创建了一个可能不需要的模块。如果
Plugins
是一个实现Plugins.add
、Plugins.all
等方法的中央插件存储库,我认为这将是一个不错的选择和Plugins.loaded
。如果您可以证明额外的模块是合理的,请使用它。
类 FooLib::Plugins::Bar < FooLib::插件
对我来说没有多大意义。
Bar
是FooLib
中的Plugins
之一,该部分看起来不错。但是,它继承自Plugins
。它是否继承自多个插件?这对我来说听起来很奇怪;类名不应该暗示不可能的事情。Assuming
Plugin
is a base class:class FooLib::Plugin::Bar < FooLib::Plugin
This is the one I use and recommend.
Bar
is aPlugin
inFooLib
and it inherits fromFooLib::Plugin
. It also keeps the plugins provided by theFooLib
library nested under the namespace of the general class, which reads naturally:If I were to develop a third party plugin for your library, I would create the following structure:
Note that I mirror the
FooLib
hierarchy, but underBarLib
's namespace. I would not extend it directly.class FooLib::Plugins::Bar < FooLib::Plugin
I have also used this one, and I think it makes the most sense.
Bar
extendsFooLib::Plugin
and is one of thePlugins
provided byFooLib
. However, it creates a potentially needless module.I think this would be a great choice if
Plugins
was a central plugin repository that implements methods likePlugins.add
,Plugins.all
andPlugins.loaded
.Use it if you can justify the extra module.
class FooLib::Plugins::Bar < FooLib::Plugins
Doesn't make a lot of sense to me.
Bar
is one of thePlugins
inFooLib
, that part looks fine. However, it inherits fromPlugins
. Is it inheriting from more than one plugin? It sounds strange to me; the class name shouldn't suggest something that is impossible.我赞同@jtrim 概述的方法。
鉴于模块(即插件)仅用于命名空间,我通常会覆盖模块中的新方法:
I would second the approach outlined by @jtrim.
Given that the module (i.e. Plugin) is being used for namespacing only, I typically override the new method in the module:
一般来说,我倾向于采用的方法是:
插件的
Base
类是一种约定,在 RubyOnRails 代码库以及许多其他代码库中随处可见。 (例如ActiveRecord::Base
、ActionController::Base
等)我不同意@Matheus Moreira使用
Foo::Plugin
的方法既作为插件的基类又作为命名空间。不应该这样做的唯一功能性原因与约定有关 - 在 Ruby 社区中,人们会发现作为命名空间的类实例比模块要少得多。我真正看到类用作另一个类的命名空间的唯一一次是当该类的用途对于命名空间类来说是私有的并且不在外部使用时。
Generally, the approach I tend to take is:
The
Base
class for plugins is a convention found all over the place in the RubyOnRails codebase as well as many others. (e.g.ActiveRecord::Base
,ActionController::Base
, etc.)I disagree with @Matheus Moreira's approach where
Foo::Plugin
is used both as the base class and the namespace for plugins.The only functional reason why this shouldn't be done has to do with convention - in the Ruby community one will find many less instances of classes as namespaces than modules. The only time I really see classes used as a namespace for another class is when the purpose of said class is private to the namespace class and is not used externally.