Solved

Naming Conventions - How to Choose

Posted on 2012-03-10
5
395 Views
Last Modified: 2012-03-10
I'm starting a development project and would like to know:  How to name my variables, data fields, modules, etc.

What are the most sensible ways to name objects, etc.

Surely someone has a clear answer.
0
Comment
Question by:brothertruffle880
  • 3
  • 2
5 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 37705829
The best way is to give them natural names - if your object represnts a Student call class Student, if it represents a Professor call class Professor
If the fields inside represent name, age, cal them this way - it is all very straighforwward

It is very much recommended also to follow convention accepted for the language,
say in java you name classes starting with capital letters, fields and methods start with lower case; several words represent with starting each word (other than the forst for a field) capitalized, (I guees it si called camel concvention or something)
say
bookTitle
bookPrice
0
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 37705844
raed this about java naming conventions:

http://java.about.com/od/javasyntax/a/nameconventions.htm
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37705851
0
 
LVL 40

Expert Comment

by:evilrix
ID: 37705914
What language? Coding styles that make sense in one language don't necessarily make sense in another.

IMHO people spend far too much time worrying about style and not enough time worrying about code quality (in terms of it being robust, not how pretty it is). Once it's compiled it all looks the same anyway. Code should be readable and maintainable; that really is all that matters. Put your effort into defining coding quality standards (for example, ensuring all functions enforce one of the three exception safety guarantees or that you always use smart pointers to manage memory allocation).

As a general case; however, it helps (and makes sense) that classes should be nouns, functions should be verbs and variable names adjectives as they generally indicate an attribute or a noun. Don't be tempted to indicate the variables type in the name. You'll regret this if you refactor the code and change the type!

Example (borrowing from for_yan's example of Student):

class Student // noun
{
   void do_homework(); // verb

   int age; // noun
   bool is_sleepy; // adjective
};

Open in new window


For example, if you have a bool indicating whether something is ready call it is_ready rather than ready_flag. Aim for self documenting code. Don't get too bogged down with style; however. It really doesn't matter whether you call your functions DoSomething or doSomething or do_something. What does matter is that you're consistent.

I've read so many coding standard the try to enforce the (half based) preferred style of the author. For example, they'll often call for using "Hungarian Notation" as a naming convention or insist that a certain level of indentation is used (python zelots go mad if you use anything other than 4 spaces for each level!). It's all nonsense. If you can code you can read code regardless of the style used. It becomes a lot harder if that style is not consistent.

So, my advice is don't waste too much energy on this. Have a few loose set of guidelines that every one can work to and leave the detail up to the engineers. You should be peer reviewing code. If an engineer goes completely bonkers and start using (for example) l33t for variable names you can just reject it. I promise they won't do that too many times :)
0
 
LVL 40

Expert Comment

by:evilrix
ID: 37705917
NB. If you can stretch to it invest in this book. It's about the only thing you need to know about coding standards.

http://www.gotw.ca/publications/c++cs.htm
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now