Click here to Skip to main content
15,886,919 members
Articles / Operating Systems / Windows

Naming Conventions: Grouping within a Class

Rate me:
Please Sign up or sign in to vote.
2.84/5 (22 votes)
25 Jul 2008CPOL2 min read 25.3K   12   8
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)


Written By
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? Pin
Dave Jellison26-Jul-08 13:42
Dave Jellison26-Jul-08 13:42 
I google landed on this article looking for something else and the idea reminded me that I've often thought about how to better organize method verbs inside of a class, project, solution, company, enterprise and even ego allowing the development community. I read this article more as a forum post asking developers for feedback rather than an article guiding the reader in one direction or another. It probably could have been written better with a little more care but the idea is sound.

There are probably more mature threads on the topic. I did a quick google and didn't quite come up with anything I could link here. Maybe someone else could.

My thoughts on the topic are not english related or even intellisense related. Obviously, in the Microsoft Naming Conventions you do see some verb patterns emerge. Common method verbs include UpdateFoo, RefreshFoo, DoFoo (my favorite), PerformFoo, etc... All jokes aside about making "Do" a verb for a method at least when I'm searching for a method that is MS code I know to check certain verbs first, intellisense or otherwise, when looking to perform some action, whether it be a windows control, asp.net control, etc.

Now consider methods involving SQL. Should they start with InsertFoo, DeleteFoo, etc.? I say they should for the simple matter of clarity. I know when I'm browsing my own code if I see a method that begins with "Insert" I, at some point, wrote some code inside that method that inserts a record into a SQL table, more than likely. To me, it also reads well against a business object. (Contact.Insert() or Contact.Update()) for example. (Yes I'm well aware of nTier software development but sometimes working on a tiny projects doesn't warrant writing or even implementing a DAL so yes I do have some Contact.Insert() type code out there Smile | :)

The real issue is there are so many tasks being accomplished by each and every application that you can't possibly account for each inevitability of course. I would love to hear some decent discussion on the matter and possible links to other threads that address the topic.

One other major issue I have as a contract software developer is my "good sense" doesn't always come into play or isn't even allowed to come into play. Take, for instance, working with a new client that has a massive code base. They either have a defined coding standard including verb choices or those verb choices have evolved naturally over the course of the project or company or hopefully at least through the personalities/developers that have contributed to the project. Me trying to force feed my naming conventions into their project because "I'm right and they're wrong" isn't really debateable. It's just poor form. I have often thought about a way to better understand and visualize a project or solutions verb choices more clearly and quickly. To be honest I didn't spend enough time and it just isn't a high enough priority but lately one thing sparked up my thoughts on the idea again. The "word cloud" controls you see everywhere on the web. If the project/solution you're working with is at least, hopefully, following a "verb-first" naming convention, you could reflect all of the methods in said solution and produce a useful word cloud that illustrates that projects verb choices. Just a thought.

Anyway nothing in this article is ground-breaking or new but I will give it a 5 for selfish reasons. I want to bump it and get some good feedback from other developers much like the author, I belive.
AnswerRe: Why not make the article a forum post and add something useful? Pin
Emilio Garavaglia27-Jul-08 6:11
Emilio Garavaglia27-Jul-08 6:11 
GeneralRe: Why not make the article a forum post and add something useful? Pin
Dave Jellison27-Jul-08 6:52
Dave Jellison27-Jul-08 6:52 
GeneralRe: Why not make the article a forum post and add something useful? Pin
ShofarNexus28-Jul-08 6:55
ShofarNexus28-Jul-08 6:55 
GeneralRe: Why not make the article a forum post and add something useful? Pin
Dave Jellison28-Jul-08 7:25
Dave Jellison28-Jul-08 7:25 
GeneralRe: Why not make the article a forum post and add something useful? Pin
ShofarNexus28-Jul-08 8:13
ShofarNexus28-Jul-08 8:13 
QuestionWhy voting 1 ? Pin
Emilio Garavaglia25-Jul-08 21:36
Emilio Garavaglia25-Jul-08 21:36 
AnswerRe: Why voting 1 ? Pin
ShofarNexus26-Jul-08 7:50
ShofarNexus26-Jul-08 7:50 

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

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