On the fifth day, God decided the earth needed a class of birds that made a neat quacking noise and were cute with white feathers, orange webbed feet and rounded orange bill. God decided to call his new creation…Cute White Feathered with Orange Bill and Orange Webbed Feet Quacker. God realized that this name was too awkward to speak out loud in conversation, too cumbersome to write and read, and just all around too unwieldy to work with. God then recognized that this name was not a name at all, but a description. So God, in his infinite wisdom, created a whole new name for his Cute White Feathered Orange Bill and Orange Webbed Feet Quacker, God called it… a duck.
I use the term duck when talking about a class that needs a new name either because its name is a description or just a concatenation of all the things it contains. I can’t recall ever thinking more about ducks prior to working on one particular project at my office. It is either the mindset of one of the other developers or it may very well be the nature of the business. I'm not sure, but either way I am constantly biting my tongue or carefully broaching the subject when I feel the need to rid the design of any Cute White Feathered with Orange Bill and Orange Webbed Feet Quackers.
So for the record and for bad memory, here are Joe’s class-naming guidelines:
- Name your class representing a thing the same name as the business stakeholders call that thing. We had an object called a MassEmailSendee for someone who we intended to send a mass email distribution to. We did not want to use the word Recipient because it implied the person received the email. What great analysis! However, as you can imagine, everyone from the developers to the end users referred to people who are supposed to get an email as a Recipient. We went back and changed the name.
- Do not name your class based on the data it contains. If the word Address did not exist, I would hope we would invent a duck instead of calling the class CStreetWithNumberCityStateAndZip.
- When you do have a class name that at conception is nothing more than the nouns that make it up or is more like a description than a name…
- Find out if it is an actual thing from your business people. They might be able to tell you that a CPersonOnTeam can be called a CPlayer, or a CRouteWithLocationsAndPath is called a CTrip
- Your stakeholders might also be able to tell you metaphors or slang being used to describe a complex item. For example in a scuba diving app you might have a class that represents lobsters that are below the legal size ( CLobsterOfIllegalSize). The stakeholders should be able to tell you that they are often called Crickets which makes for a much nicer name.
- See if you can remove any words that do not contribute to the name being unique. Library Check Out Card can be reduced to Library Card since there isn’t another kind of library card other than the “Check out” kind. If ever there is one, you could rename the class, or just denote the exception on the new kind of card.
- Can you turn an abbreviation for the words into a pronounceable word? The class CLightAmplificationByStimulatedEmissionOfRadiation could be called CLaser. Documents Waiting for Administrative Approval could be called Dwaps.
- If you have a class that is the concatenation of nouns, write a description of the class and there is a good chance the name will spring from that. For example, true story believe it or not, but I once had a class called Organization Import Output Object Property Input Data Table Value (COrgImportOutputObjPropInputDataTblVal). When we started using the class and clarifying its purpose in life (the description) we realized it was an Object Property Mapping. The long name was a result of designing based on data as opposed to behavior or purpose in life.
- Modern IDEs make changing names simple.
- Unless you are developing a framework to be used by people and systems you have no contact with, do not be afraid of changing names when you realize a class has a poor name.
- We should not get hung up on names during design since changing them later is not a big deal. Even the long descriptive names or noun-concatenation names could be used until the better name is realized. Don’t forgo trying to come up with a name, but don’t get hung up on it while designing.
- It can be argued that by naming a class a Duck you make the meaning less clear to someone as they now don’t know the details. Well, that’s what abstraction is all about. We don't want to mislead them with the name, but we also purposely don't want to bother them with the details. When the user of the class is interested in the details they can look behind the scenes. When the user is interested in the class as it relates to other classes then the smaller “duck” name is priceless.
- Using abbreviations makes the length of a class name shorter, but not any more intelligible. Your brain deals with the same Cute White Feathered with Orange Bill and Orange Webbed Feet Quacker regardless of whether you call it that, or CuteWhtFthrdOrOrgBillOrgWbdFtQckr
- Researchers have done lots of studies on the number of things we can remember at once in our “working memory”. It varies for different people but in general it seems to be about 4 to 7. A class called WhiteTailedBuckedToothBeast only takes up 3 to 6 of our memory slots depending on how your brain handles the compound words, so when we are only looking at that one class it is not too awful. However, when working with a few classes all named like this then our thinking gets jumbled as our brain tackles the 20 or so nouns floating around. Call the WhiteTailedBuckedToothBeast a Rabbit.
- By using Namespaces in .Net (Packages in Java) we can organize our code in such a way that object names can be made shorter. Let's say I have an ImportWidgetControlThing and a MaintenanceWidgetControlThing. If Import and Maintenance are subsytems that we organize with Namespaces then I should be able to name them both WidgetControlThing without the preface. This only becomes an issue if the same code deals with both.