Click here to Skip to main content
Click here to Skip to main content

Naming Conventions: Grouping within a Class

, 25 Jul 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
We provide a challenge to conventional wisdom of verb first to aid in grouping of names.

Introduction

A common practice when selecting a naming convention is to use verb first resulting in names that read like an English phrase like GetThisThing. While establishing and sticking with a naming convention is wise, other factors are presented that may prove helpful, especially grouping concerns.

Although English words are used as the structure of virtually all programming languages, not all developers speak English as their native tongue. Non-English developers will frequently use English terms for their public classes and may go as far as commenting their code in English.

However the English sentence structure may not be the best when we are attempting to form a valuable descriptive phrase. We should consider the benefits of different sentence structures, most notably the position of the verb, when we form names.

If we have a class that has a value that we need to get, set and toggle we might have functions GetThisThing, SetThisThing and ToggleThisThing. We write our class with our three functions plus 38 more. A month later when it is no longer fresh in our minds, or when Joe Developer in the other department needs to make use of our class and work with an instance of ThisThing it is natural to use IntelliSense to find the functions we need.

We can assume that if we start typing .g that IntelliSense will quickly get us to the GetThisThing function. The same is true for our set function. However, since we don't remember or are not familiar with the code, we may not know that there is a toggle function that we require and end up doing the get, toggle, set sequence ourselves. IntelliSense will show us the toggle function, but not until we start with .t. When there are a lot of functions in our class, we are unlikely to notice it.

If we put the verb on the end, our function names would be ThisThingGet, ThisThingSet, ThisThingToggle. Now when we are working with an instance of the class and we need to work with ThisThing and we start typing .t we end up with all of our functions that address ThisThing together.

The use of namespaces allows us to group functional areas together which aids in code manageability. Grouping functions within a class by beginning names with its group accomplishes the same for a class.

We need to balance readability in an English sense and usability from a developer’s perspective. After all, the code is intended to be read by developers and not a general audience.

History

  • 25th July, 2008: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

ShofarNexus
Founder ShofarNexus Corporation
United States United States
ShofarNexus™ is software project started in 1998 with a lot of testing and rejecting of methodologies. Our goals were fast startup and execution, a clean presentation and reliable data distribution. We take ReST to the extreme. We focus some on eye-candy and mostly on the meat and vegetables that are good for business.
 
ShofarNexus™ mentality is well described by Antoine de Saint-Exupéry who wrote “Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away.”
 
ShofarNexus™ is single-handedly written by John Kozlowski, with over 35 years of software development experience and a lot of hardware design in the first two decades.

Comments and Discussions

 
QuestionWhy not make the article a forum post and add something useful? PinmemberDave Jellison26-Jul-08 14:42 
AnswerRe: Why not make the article a forum post and add something useful? Pinmemberemilio_grv27-Jul-08 7:11 
GeneralRe: Why not make the article a forum post and add something useful? PinmemberDave Jellison27-Jul-08 7:52 
GeneralRe: Why not make the article a forum post and add something useful? PinmemberDirectorWare28-Jul-08 7:55 
GeneralRe: Why not make the article a forum post and add something useful? PinmemberDave Jellison28-Jul-08 8:25 
GeneralRe: Why not make the article a forum post and add something useful? PinmemberDirectorWare28-Jul-08 9:13 
DirectorWare wrote:
"Personally rather than using regions, I make heavy use of partial classes and separate logical segments of a class into physically separate files. This allows particular segments to naturally appear in separate tabs and be instantly visible in the file tree. For example I might have Class.cs and Class.Xml.cs to separate XML serialization methods."

 
Dave Jellison wrote:
"I see no need for this, imho. Taking your example I would write a generic class (and have one for XML serialization e.g. (static class) XmlSerailizer.Serialize(string path), which also handles caching the XmlSerialization classes because of the reflection overhead due to loading a new type).

 
The “Class.Xml.cs” example I think is actually a good one since we are working on an article that addresses using our freeware RestDirector product in XML serialization. Since it is a rather different approach than what Microsoft has in the .Net Framework, it does require some hand coding in exchange for a radical performance increase, partially due to no reflection requirement. Therefore, the XML serialization is class specific and placing it in separate file or region makes sense.
 
But we miss the point. A given class may have functionality to render some HTML or XAML (class.Html.cs), do data access to a SQL database (class.Sql.cs), read and write settings (class.Settings.cs), the list goes on. What you might group into regions, I group into files. To get to the part of the class I’m interested in I double click in the file tree, rather than collapse the regions and then expand the one I want. Like regions it also keeps related stuff together so I can have most or all of what I’m interested in on my screen at once.
 
A second plus is that ClassA may have some of the method groups and ClassB may have a different mix. Simply looking at the file tree gives me instant knowledge of what class has what.
 
We are back to taste. Just as I tell my kids and practice myself, I try a variety of foods, especially from a different culture or country. Some I like, some I don’t, but I have gained from the experience. I tried regions and partial classes and found one I like and one I don’t.
QuestionWhy voting 1 ? Pinmemberemilio_grv25-Jul-08 22:36 
AnswerRe: Why voting 1 ? PinmemberDirectorWare26-Jul-08 8:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.1411023.1 | Last Updated 25 Jul 2008
Article Copyright 2008 by ShofarNexus
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid