我该如何命名“雨伞”?打字理智吗?
这“总是”困扰着我......
假设我有一个接口 IFiddle
和另一个接口,它除了聚合几个不同的 IFiddle
之外什么也不做:(
public interface IFiddleFrobbler
{
IFiddle Superior { get; }
IFiddle Better { get; }
IFiddle Ordinary { get; }
IFiddle Worse { get; }
IFiddle Crackpot { get; }
}
具体的 IFiddle
>IFiddleFrobbler 和 IFiddle
取决于配置并由工厂创建。)
我多次偶然发现此类“伞”类型的命名 - 我想将“Frobbler”与描述性的东西。
- “Collection”、“List”和“Set”还不够好,因为它不是可以枚举、添加或删除元素的集合/列表/集合。
- “经理”已被淘汰,因为没有进行任何管理 - 工厂和配置会处理该问题。
- “聚合器”听起来像是直接从 GoF 书中挑选的(尽管我不认为他们会打破“德米特定律”——这不是这里的主题)。
请告诉我,我的“伞”类型的良好命名方案是什么?
编辑:如xtofl指出在评论中,实际上比我上面首先暴露的语义更多。如果我改为执行以下操作,我认为我的需求会更清晰:
//
// Used for places where the font width might need
// to be tapered for a rendered text to fit.
//
public interface ITaperableFont
{
Font Font { get; }
Boolean CanTaper { get; }
void Taper();
}
//
// Used for rendering a simple marked-up text in
// a restricted area.
//
public interface ITaperableFonts
{
ITaperableFont Biggest{ get; }
ITaperableFont Big { get; }
ITaperableFont Normal { get; }
ITaperableFont Small { get; }
ITaperableFont Smallest { get; }
}
事实上,我已经将上面现实生活中的添加问题确定为设计缺陷,而不是命名问题,下面有几个人指出了这个问题。
This has "always" bothered me...
Let's say I have an interface IFiddle
and another interface that does nothing more than aggregate several distinct IFiddle
s:
public interface IFiddleFrobbler
{
IFiddle Superior { get; }
IFiddle Better { get; }
IFiddle Ordinary { get; }
IFiddle Worse { get; }
IFiddle Crackpot { get; }
}
(The concrete IFiddleFrobbler
s and IFiddle
s depend on configurations and are created by a factory.)
I repeatedly stumble on the naming of such "umbrella" types - I want to exchange the "Frobbler" with something descriptive.
- "Collection", "List" and "Set" aren't good enough, in the sense that it's not a collection/list/set where elements can be enumerated, added or removed.
- "Manager" is out, because there's no management being done - the factory and configurations handle that.
- "Aggregator" makes it sound like picked straight from the GoF book (although I don't think they would break "the law of demeter" - that's out of topic here).
Please, enlighten me, what's a good naming scheme for my "umbrella" types?
Edit: As xtofl pointed out in a comment, there's actually more semantics to this than I first exposed above. If I instead do the following, I think my need is clearer:
//
// Used for places where the font width might need
// to be tapered for a rendered text to fit.
//
public interface ITaperableFont
{
Font Font { get; }
Boolean CanTaper { get; }
void Taper();
}
//
// Used for rendering a simple marked-up text in
// a restricted area.
//
public interface ITaperableFonts
{
ITaperableFont Biggest{ get; }
ITaperableFont Big { get; }
ITaperableFont Normal { get; }
ITaperableFont Small { get; }
ITaperableFont Smallest { get; }
}
In fact, I've identified my problem in the real-life addition above as a design flaw, not a naming problem, the smell of which several people has pointed out below.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以只使用复数形式:
IFiddles
吗?Can you just use the plural:
IFiddles
?我同意你的观点,命名类、接口或抽象类确实很难正确命名。我之前使用过的一些您可能会喜欢的名称是:
Assistant
Composite
xxxCoordinator
Group
I agree with you in that naming a class, interface or abstract class is really difficult to get right. Some possible names that I have used before which you might like are:
Assistant
Composite
xxxCoordinator
Group
我想说,您选择的名称应该取决于您想要实现的目标。在代码示例中,我会说您正在做的是设置评级,所以我可能会将其称为
IFiddleRating
作为一般答案,我会说“这取决于”:) 我发现它一个好主意是根据事物想要做什么而不是它“是什么”来命名
I would say that the name you choose should be dependent on what you are trying to achieve. In the code example I'd say what you are doing is setting up a rating, so I'd probably call it
IFiddleRating
As a general answer I'd say "it depends" :) I find its a good idea to name stuff after what its trying to do, not what it "is"
正如 Pop 所说:“如果你不能说出它的名字,那么我就在设计中闻到了一些可疑的味道。”
也许重新考虑你的设计。
更新:如果您希望它是只读的,请自行创建一个界面:
您的生活仍然可以像以前一样轻松。
As Pop said: "If you can't name it, then I smell something fishy in the design."
maybe rethink your design.
Update: If you want it to be readonly create an interface for that yourself:
Your life can still as easy as before.
我认为这确实是个人喜好的问题,所以我启动了同义词库并寻找“家庭”的同义词(这本身就可以)。
亲属、分类、流派、团体、种类、细分、协会、从属关系、联盟、氏族、集团、俱乐部、联盟、组合、组合、邦联、联盟、国会、合作社、家庭、联邦、联谊会、兄弟会、帮派、行会、联盟、暴徒、秩序、组织、戒指、社会、联谊会、联谊会、辛迪加、捆绑、结盟、部落、部队、剧团、动物园
我删除了一些具有特定技术含义的词(例如联盟、戒指、池) )...选择你最喜欢的。
I think this is really a problem of personal preference, so I fired up a thesaurus and looked for synonyms of "Family" (which could be ok in itself).
kin, classification, genre, group, kind, subdivision, association, affiliation, alliance, clan, clique, club, coalition, combination, combo, confederacy, confederation, congress, cooperative, family, federation, fellowship, fraternity, gang, guild, league, mob, order, organization, ring, society, sodality, sorority, syndicate, tie-in, tie-up, tribe, troops, troupe, zoo
I dropped a few which have a specific technical meaning (like union, ring, pool)... pick what you like best.