Very long class names

后端 未结 7 1583
一个人的身影
一个人的身影 2021-02-18 13:41

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 mor

7条回答
  •  灰色年华
    2021-02-18 13:52

    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?

    A class is supposed to represent a type of thing: it's a noun (not a verb, and certainly not a whole a sentence).

    I like to describe my things using one or two words, maybe three: just one noun, or a compound noun (like in German), or maybe an adjective and a noun.

    For example, here's a list of some of my classes in one project:

    Ancestors.cs
    Block.cs
    BlockCollection.cs
    BlockDocument.cs
    BlockInline.cs
    BlockInlineText.cs
    BlockListBullet.cs
    BlockListItem.cs
    BlockP.cs
    BlockSequence.cs
    BlockTable.cs
    BlockText.cs
    BlockTextParent.cs
    Border.cs
    BorderEdges.cs
    Box.cs
    Canvass.cs
    CaretLocation.cs
    CaretLocationAndNode.cs
    CaretLocationAndPoint.cs
    CaretLocationData
    CaretLocationPair.cs
    CaretLocationPairAndPoint.cs
    CaretsInBlock.cs
    DebugDumpDom.cs
    DebugOutput.cs
    Display.cs
    Displayed.cs
    DocumentHandle.cs
    DomDocument.cs
    DomDocumentFragment.cs
    DomElementBody
    DomElementHandle.cs
    DomElementTag.cs
    DomEvent.cs
    DomList.cs
    DomNode.cs
    DomNodeChild.cs
    DomRange.cs
    DomRangeBase.cs
    DomTextBody.cs
    DomTextHandle.cs
    Edge.cs
    Editing.cs
    EditingState.cs
    Editor.cs
    EditorAction
    EditorActions
    EditorMutations.cs
    EditorTransaction
    EventListeners.cs
    ExtractedRangeInDomDocument.cs
    ExtraDebugInformation.cs
    FormControl.cs
    ... etc ...
    

    As you can see, most class names are just two words (a compound noun).

    I also use namespaces to separate classes into different categories and different projects (which helps to keep the names of the classes shorter: because some or most of the hierarchical information is in the namespace name, not the class name).

    If I'm fortunate, then class names are unique or almost unique after the first few letters: so if I type in the first few letters, I can then select the specific class using Intellisense. For that reason, I don't need to use abbreviations.

提交回复
热议问题