很长的类名 [英] Very long class names

查看:97
本文介绍了很长的类名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试尽可能准确地命名一个类(还有成员、属性等等).但有时我不确定如果类名变得很大(50 个字符或更多)是否如此聪明.处理是如此不舒服,代码变得难以阅读.

I try to name a class (also members, properties and so forth) as exact as I can. But sometimes I’m not sure if this is so clever if the class name becomes huge (50 chars and more). The handling is so what uncomfortable and the code becomes difficult to read.

这个问题对我来说很少出现,因此我在处理这么长的名字方面没有太多经验,但它时不时(现在)出现.其他人如何处理?你有一个大概的上限然后做缩写还是值得处理这么长的名字?

The question occured for me seldom and therefore I don’t have much experience with working with such long names but from time to time (now) it occurs. How other people handle this? Have you an approximate upper limit and then make abbreviations or is it worth the pain to handle such long names?

更新

这里要求一个如此长的类名的例子.

As requested here an example of such a long class name.

ProjectContractChargingPeriodProjectAccountReferenceVM

第一个 Project 代表域,它可以省略,因为命名空间已经暗示它处理项目.问题是,如果我用这个类名来做这个,那么我必须用这个命名空间的所有类来做,我绝对不喜欢这个命名空间的许多(短)类名将失去它们表现力.[Project]ContractChargingPeriod 描述了该对象,该类用于并且ProjectAccountReference 表示该类是对ProjectAccount 的引用.ProjectAccountProjectContract 的问题相同.仅使用 Account 没有意义,因为应用程序中还存在其他 Account-Class.Reference 有点弱,因为实际上它不仅仅是一个参考,但这是通用目的.VM 是我一直使用的缩写,它代表 ViewModel.我认为这是合法的,因为每个使用 WPF 的人都知道 VM 是什么意思.

The first Project represents the domain, it may be omitted because the namespace implies already that it handles projects. The problem with that is, that if I do this with this class name, then I must do it with all classes of this namespace and that I definitively don’t like because then many (short) class-names of this namespace will lose their expressiveness. [Project]ContractChargingPeriod describes the object, this class is used for and ProjectAccountReference means that the class is a reference to a ProjectAccount. With ProjectAccount is the same problem as with the ProjectContract. Only using Account is not meaningful because in the app exists also other Account-Classes. The Reference is a little bit weak because in reality it’s a little bit more than only a reference, but this is the general purpose. The VM is an abbreviation I always use and it stands for ViewModel. I think this is legal because everyone who works with WPF knows what VM means.

我不得不说,该类用于将一个类从 ORM 中包装出来,该 ORM 是用我很久以前创建的旧工具构建的.那里的类​​代表准 1:1 的 ERM,我知道这不是最佳的,但改变它会是一项重大的努力.

I have to say, that the class is used to wrap a class out of an ORM that is built with an elder tool I created a long time ago. The classes there represent quasi 1:1 the ERM and I am aware that this is not optimal, but changing it would be a major effort.

推荐答案

(我使用 Java/C++,也使用过其他 OO 语言,但不是 C#,我在这里所说的几乎适用于我使用过的所有语言)

(I use Java/C++, and have used other OO languages, but not C#, what I say here pretty much applies to all the languages I have used)

我使用描述性的类名.但是,我认为我没有达到 50 个字符:-)一般长的类名不是公开的,通常隐藏在接口和工厂后面.接口的名称比类短得多.

I use descriptive class names. I don't think I have made it to 50 characters however :-) Generally long class names are not public, and are usually hidden behind an interface and a factory. The interface has a much shorter name than the classes.

您可能会做的一件事是,假设您有许多密切相关的长命名类,将它们放入一个包中.发现这一点的一种方法是,如果所有类都具有相同的前缀词.如果有许多类都以相同的名称开头,那么也许该名称应该是一个包.

One thing you might do is, assuming you have a number of long-named classes that are closely related, put them into a package. One way to spot this is if the classes all have the same prefix word(s). If there are a number of classes that all start with the same name then, perhaps, the name should be a package instead.

这篇关于很长的类名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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