命名类-如何避免把所有的东西都称为"<WhatEver>Manager"?[英] Naming Classes - How to avoid calling everything a "<WhatEver>Manager"?

本文是小编为大家收集整理的关于命名类-如何避免把所有的东西都称为"<WhatEver>Manager"?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

很久以前,我已经阅读了一篇文章(我相信博客条目),这使我在命名对象上的"正确"曲目:对您的程序中的命名非常谨慎.

例如,如果我的应用程序是(作为一个典型的业务应用程序)处理用户,公司和地址,我将拥有User,a Company和Address域类 - 以及可能的某个地方UserManager ,CompanyManager和AddressManager会弹出处理这些问题的弹出.

那么,您能告诉那些UserManager,CompanyManager和AddressManager做什么?不,因为经理是一个非常通用的术语,适合您可以使用域对象的任何事情.

我阅读的文章建议使用非常具体的名称.如果这是C ++应用程序,并且UserManager的工作是分配并使用户摆脱堆,它不会管理用户,而是守护他们的出生和死亡.嗯,也许我们可以称其为UserShepherd.

或UserManager的工作是检查每个用户对象的数据并在密码上签名.那么我们将有一个UserRecordsClerk.

现在,这个想法一直困扰着我,我尝试应用它.并发现这个简单的想法很难.

我可以描述课程的工作,并且(只要我不进入快速和肮脏的编码)我写的课程完全 .我想念从该描述到名称的是一种名称的目录,词汇将概念映射到名称.

最终,我想在我的脑海中拥有类似模式目录的东西(经常设计模式很容易提供对象名称,例如a factory ))

  • 工厂 - 创建其他对象(命名从设计模式中获取)
  • 牧羊人 - 牧羊人处理对象的寿命,它们的创建和关闭
  • 同步器 - 在两个或多个对象(或对象层次结构)之间复制数据
  • Nanny-帮助对象在创建后达到"可用"状态 - 例如,通过接线到其他对象

  • 等等.

那么,您如何处理这个问题?您是否有固定的词汇,您是在即时发明新名称,还是考虑命名不太重要或不太错误的事物?

p.s.:我也对讨论该问题的文章和博客的链接感兴趣.首先,这是让我思考它的原始文章:没有"经理"


更新:答案摘要

这是我同时从这个问题中学到的东西的一些摘要.

  • 尽量不要创建新的隐喻(Nanny)
  • 看看其他框架做什么

有关此主题的更多文章/书籍:

和当前的名称前缀/后缀我从答案中收集(主观!):

  • 协调员
  • 建筑商
  • 作家
  • 读者
  • 处理程序
  • 容器
  • 协议
  • 目标
  • 转换器
  • 控制器
  • 查看
  • 工厂
  • 实体

和道路的好提示:

不要命名瘫痪.是的,名字非常重要,但它们不足以浪费大量时间.如果您在10分钟内想不出好名字,请继续.

推荐答案

我问了类似问题,但是在可能的情况下,我尝试复制 .net 框架中的名称,我在 javaandroid 框架.

似乎Helper,Manager和Util是您所附加的不可避免的名词,用于协调不包含状态并且通常是程序性和静态的类别.替代方案是Coordinator.

您可能会得到特别的紫色Prosey,并选择Minder,Overseer,Supervisor,Supervisor,Administrator和Master,但是正如我所说,我更喜欢像框架名称一样保持它你习惯了.


您在 .net 框架中也发现了其他一些常见后缀(如果是正确的术语)是:

  • Builder
    一种使用某些参数构建特殊类型实例的类型. Builder通常是一次抛弃.它甚至可能不需要分配变量.
    如果类型需要重复创建对象,请使用Factory.
    如果负责创建多个不同类型对象的类型,请使用Factories.
  • Writer
    将一些变量写入某物.
  • Reader
    阅读一些变量.
  • Handler
    旨在处理情况或其他事情.
  • Container
    可以把东西放入其中.

其他推荐答案

您可以看一下 source-code-wordle.de ,我在那里分析了最常的分析使用.NET框架和其他一些库的类名称的后缀.

前20名是:

  • 属性
  • 类型
  • 助手
  • 收集
  • 转换器
  • 处理程序
  • 信息
  • 提供商
  • 异常
  • 服务
  • 元素
  • 经理
  • 节点
  • 选项
  • 工厂
  • 上下文
  • 项目
  • 设计师
  • 基础
  • 编辑

其他推荐答案

我全都是好名字,我经常写关于选择事物名称时要小心的重要性.出于同样的原因,我对命名事物时的隐喻保持警惕.在最初的问题中,"工厂"和"同步器"看起来像是好名字的意思.但是,"牧羊人"和"保姆"不是,因为它们基于隐喻.您的代码中的一堂课实际上不能是保姆.您将其称为保姆,因为它非常像现实生活中的保姆照顾婴儿或孩子一样.在非正式的演讲中可以,但是(我认为)对代码中的命名课程不确定,这必须由谁知道谁知道谁知道.

.

为什么?因为隐喻是依赖文化的,而且通常也依赖于个人.对您来说,命名"保姆"的命名可能非常清楚,但也许对其他人来说并不清楚.我们不应该依靠这一点,除非您正在编写仅供个人使用的代码.

无论如何,惯例都可以制造或破坏隐喻. "工厂"本身的使用是基于一个隐喻,但是已经存在了相当长的时间,目前在编程世界中众所周知,所以我会说可以安全使用.但是," Nanny"和" Shepherd"是不可接受的.

本文地址:https://www.itbaoku.cn/post/627543.html

问题描述

A long time ago I have read an article (I believe a blog entry) which put me on the "right" track on naming objects: Be very very scrupulous about naming things in your program.

For example if my application was (as a typical business app) handling users, companies and addresses I'd have a User, a Company and an Address domain class - and probably somewhere a UserManager, a CompanyManager and an AddressManager would pop up that handles those things.

So can you tell what those UserManager, CompanyManager and AddressManager do? No, because Manager is a very very generic term that fits to anything you can do with your domain objects.

The article I read recommended using very specific names. If it was a C++ application and the UserManager's job was allocating and freeing users from the heap it would not manage the users but guard their birth and death. Hmm, maybe we could call this a UserShepherd.

Or maybe the UserManager's job is to examine each User object's data and sign the data cryptographically. Then we'd have a UserRecordsClerk.

Now that this idea stuck with me I try to apply it. And find this simple idea amazingly hard.

I can describe what the classes do and (as long as I don't slip into quick & dirty coding) the classes I write do exactly one thing. What I miss to go from that description to the names is a kind of catalogue of names, a vocabulary that maps the concepts to names.

Ultimately I'd like to have something like a pattern catalogue in my mind (frequently design patterns easily provide the object names, e.g. a factory)

  • Factory - Creates other objects (naming taken from the design pattern)
  • Shepherd - A shepherd handles the lifetime of objects, their creation and shutdown
  • Synchronizer - Copies data between two or more objects (or object hierarchies)
  • Nanny - Helps objects reach "usable" state after creation - for example by wiring to other objects

  • etc etc.

So, how do you handle that issue? Do you have a fixed vocabulary, do you invent new names on the fly or do you consider naming things not-so-important or wrong?

P.S.: I'm also interested in links to articles and blogs discussing the issue. As a start, here is the original article that got me thinking about it: Naming Java Classes without a 'Manager'


Update: Summary of answers

Here's a little summary of what I learned from this question in the meantime.

  • Try not to create new metaphors (Nanny)
  • Have a look at what other frameworks do

Further articles/books on this topic:

And a current list of name prefixes/suffixes I collected (subjectively!) from the answers:

  • Coordinator
  • Builder
  • Writer
  • Reader
  • Handler
  • Container
  • Protocol
  • Target
  • Converter
  • Controller
  • View
  • Factory
  • Entity
  • Bucket

And a good tip for the road:

Don't get naming paralysis. Yes, names are very important but they're not important enough to waste huge amounts of time on. If you can't think up a good name in 10 minutes, move on.

推荐答案

I asked a similar question, but where possible I try to copy the names already in the .NET framework, and I look for ideas in the Java and Android frameworks.

It seems Helper, Manager, and Util are the unavoidable nouns you attach for coordinating classes that contain no state and are generally procedural and static. An alternative is Coordinator.

You could get particularly purple prosey with the names and go for things like Minder, Overseer, Supervisor, Administrator, and Master, but as I said I prefer keeping it like the framework names you're used to.


Some other common suffixes (if that is the correct term) you also find in the .NET framework are:

  • Builder
    A type that use some parameters to construct an instance of a special type. Builder is usually a throwaway. It may not even need to allocate a variable.
    If the type needs to repeatedly create objects, please use Factory.
    if the type responsible for create multiple different type objects, please use Factories.
  • Writer
    Write some variable into something.
  • Reader
    Read something as variable.
  • Handler
    Designed to deal with a situation or something.
  • Container
    Can put something into it.

其他推荐答案

You can take a look at source-code-wordle.de, I have analyzed there the most frequently used suffixes of class names of the .NET framework and some other libraries.

The top 20 are:

  • attribute
  • type
  • helper
  • collection
  • converter
  • handler
  • info
  • provider
  • exception
  • service
  • element
  • manager
  • node
  • option
  • factory
  • context
  • item
  • designer
  • base
  • editor

其他推荐答案

I'm all for good names, and I often write about the importance of taking great care when choosing names for things. For this very same reason, I am wary of metaphors when naming things. In the original question, "factory" and "synchronizer" look like good names for what they seem to mean. However, "shepherd" and "nanny" are not, because they are based on metaphors. A class in your code can't be literally a nanny; you call it a nanny because it looks after some other things very much like a real-life nanny looks after babies or kids. That's OK in informal speech, but not OK (in my opinion) for naming classes in code that will have to be maintained by who knows whom who knows when.

Why? Because metaphors are culture dependent and often individual dependent as well. To you, naming a class "nanny" can be very clear, but maybe it's not that clear to somebody else. We shouldn't rely on that, unless you're writing code that is only for personal use.

In any case, convention can make or break a metaphor. The use of "factory" itself is based on a metaphor, but one that has been around for quite a while and is currently fairly well known in the programming world, so I would say it's safe to use. However, "nanny" and "shepherd" are unacceptable.