473,395 Members | 1,938 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Help with Naming Conventions

Daz
Hi all!

This question may hopefully spark a little debate, but my problem is
this:

I am sure it's not just me who struggles to think up names for
variables. Obviously, thinking up a name 'can' be simple, but when you
are trying to create variable names that are easy to remember,
descriptive, and not more than say 15-20 characters long, I come a
cropper! Would anyone be able to give me any pointers as to how I can
create the variable names I am looking for, or is it simply a case of
having a notepad doc open and keeping track of what each name is?

Any comments would be appreciated.

Daz

May 30 '06 #1
8 2062
Have you considered you might be writing too long functions or not structuring
your arguments together in logical groups enough. Generally most functions
should not be more than 10-50 lines of code and contain 5 variables or so. If
you can't come up with names that are not to long you can put comments for each
variable in the beginning and try to document your algorithm, not your code.
Same goes for arguments to functions, they should be descriptive and few
(otherwise consider grouping them in a struct). Example:

/* This would be a bad name */
void LoadFile(const char* nameOfInputFileToLoadTextureFrom);

/* This along with a comment like:
@param inFile name of input file to load texture from
would be better.
*/
void LoadFile(const char* inFile);

Of course these are rules of thumb and not everybody will agree with me. But
these are at-least my thought on the matter.

/Daniel Aarno
Daz skrev:
Hi all!

This question may hopefully spark a little debate, but my problem is
this:

I am sure it's not just me who struggles to think up names for
variables. Obviously, thinking up a name 'can' be simple, but when you
are trying to create variable names that are easy to remember,
descriptive, and not more than say 15-20 characters long, I come a
cropper! Would anyone be able to give me any pointers as to how I can
create the variable names I am looking for, or is it simply a case of
having a notepad doc open and keeping track of what each name is?

Any comments would be appreciated.

Daz

May 30 '06 #2
Daz

Daniel Aarno wrote:
Have you considered you might be writing too long functions or not structuring
your arguments together in logical groups enough. Generally most functions
should not be more than 10-50 lines of code and contain 5 variables or so. If
you can't come up with names that are not to long you can put comments for each
variable in the beginning and try to document your algorithm, not your code.
Same goes for arguments to functions, they should be descriptive and few
(otherwise consider grouping them in a struct).


Thanks for that Daniel, I appreciate your input. I think the main
problem I am trying to address more than anything is consistancy. The
only trouble being, I can't seem to find one easy way to name functions
and variables that suits every situation. Take Hungarian Notation for
example. It has it's good points and it's bad points. Some people
prefer it, others do not.

May 30 '06 #3
Daz wrote:
Daniel Aarno wrote:
Have you considered you might be writing too long functions or not structuring
your arguments together in logical groups enough. Generally most functions
should not be more than 10-50 lines of code and contain 5 variables or so. If
you can't come up with names that are not to long you can put comments for each
variable in the beginning and try to document your algorithm, not your code.
Same goes for arguments to functions, they should be descriptive and few
(otherwise consider grouping them in a struct).


Thanks for that Daniel, I appreciate your input. I think the main
problem I am trying to address more than anything is consistancy. The
only trouble being, I can't seem to find one easy way to name functions
and variables that suits every situation. Take Hungarian Notation for
example. It has it's good points and it's bad points. Some people
prefer it, others do not.


Hungarian notation doesn't scale particularly well for user-defined
types, IMHO, and that is reason enough to discard it in C++. Daniel's
advice above is good. Listen to him.

Also, I prefer to name functions as Verb() or VerbObject() [e.g.,
Close(), SearchTree(), HasTerminated()]. The object-less versions are
more useful for member functions in the public interface of a class
since, e.g., there could be many things that need closing, but calling
file.Close() is clear and not redundant like file.CloseFile().

As for variables, keep them in as small a scope as possible, and you'll
prevent many name collisions and make things easier to follow. Most
programmers use some convention for identifying member variables such
as a trailing underscore or a leading m_, but beyond that, just use
meaningful names within that scope. Some also use a similar convention
for static and global objects.

You could adopt virtually any style and write readable code with it.
For instance, you might name a variable for holding the number of
elements elem_count, n_elems, arraySize, etc. All of these are fine --
just pick one style and go with it. Consistency is more important than
the particular style. One might choose to use underscores to separate
words (like the standard library does) over CamelCase (like Microsoft
does), and that's not really a big deal as long as you're consistent.

Cheers! --M

May 30 '06 #4
Daz
Vey clear, concise and makes perfect sense. Thanks very much.

May 30 '06 #5
Daniel Aarno wrote:
Have you considered you might be writing too long functions or not structuring
your arguments together in logical groups enough. Generally most functions
should not be more than 10-50 lines of code and contain 5 variables or so.


10-50 lines of code seems like a lot to me. I rarely end up with a
member-function that has more than 10 lines of code, non-member (and
static member) functions tend to be a little bigger but still rarely
more than 20 lines.

To the OP, IMHO variable and function names should be written such that
they make sense at the point of use, I try to get it looking as much
like standard english as I can.

To expand on Mr Aarno's example: 'nameOfInputFileToLoadTextureFrom' is
not a bad name because it is long, but because it would be hard to
speak out loud the place where it is used...

textureFile.open(nameOfInputFIleToLoadTextureFrom) ;

If you were talking to someone named "textureFile" and you wanted him
to open a file how would you say that in English? I would probably say
something like "textureFile, please open this file name." Now of course
in code we don't need to be so polite (so "please gets dumped") and
pronouns are superfluous so we can dump "this"... You end up with
"textureFile, open file name." No convert that back to code:

textureFile.open( fileName );

Generally, I try reading the code out loud as English, if it doesn't
make sense, then I need to change the names of some
functions/variables.

May 30 '06 #6
Daniel T. wrote:
Daniel Aarno wrote:
Have you considered you might be writing too long functions or not structuring
your arguments together in logical groups enough. Generally most functions
should not be more than 10-50 lines of code and contain 5 variables or so.
10-50 lines of code seems like a lot to me. I rarely end up with a
member-function that has more than 10 lines of code, non-member (and
static member) functions tend to be a little bigger but still rarely
more than 20 lines.

To the OP, IMHO variable and function names should be written such that
they make sense at the point of use, I try to get it looking as much
like standard english as I can.

To expand on Mr Aarno's example: 'nameOfInputFileToLoadTextureFrom' is
not a bad name because it is long, but because it would be hard to
speak out loud the place where it is used...

textureFile.open(nameOfInputFIleToLoadTextureFrom) ;


If you were talking to someone named "textureFile" and you wanted him
to open a file how would you say that in English? I would probably say
something like "textureFile, please open this file name." Now of course
in code we don't need to be so polite (so "please gets dumped") and
pronouns are superfluous so we can dump "this"... You end up with
"textureFile, open file name." No convert that back to code:

textureFile.open( fileName );

Generally, I try reading the code out loud as English, if it doesn't
make sense, then I need to change the names of some
functions/variables.


First of all, I agree with Daniel T. You have to keep in mind that once
working in Object Oriented world, each object (or Entity) has set of
actions it can perform.
I also encourage the use of descriptive names. I enjoy orking with
Visual Assist (www.wholetomato.com). That way I can still keep
descriptive variable and member names without cost of coding time
that's spent on writing long variable names. Also, when I say
descriptive I mean that names can be long, but not redundant.
You can take VA for free trial and see how great it does work of code
suggestions and other miracles.

I beleive that code should tell a story. I think that Daniel Aarno
meant that as well. There's no point in commenting the obvious, like:

++i; // increment i

The above line has few goals:
1). distruct programmer from real program
2). making the code bigger and uglier
.. maybe some more.. but none of goals is good.

The most important thing to keep in mind, IMHO, is that you have naming
conventions and that everybody in your team, even if it's 1 men crew,
follows it.

When telling a story, there should be objects. Objects can talk with
other objects. Objects can do things, like loding a texture and more
on.. A good coding practice is to make functions short, as people
already mentioned, 10-20 lines. Each function should have a
well-defined purpose.

here's a brief sum of naming conventions I use:
s_someStuffGoingOn - static
m_blaFooWidget - member

void funcName(int i_inputData, const Widget& i_anotherInput, float&
o_outputData, bool& io_inputAndOutputData)
{
int localVar;
}

class Widget
{
static const int SOME_NUMBER = 17;

static int s_foo; // don't forget to define it in .cpp file
bool m_isActive;
public:
void invoke(const SomeObject& i_fear);
};

Though, it's my personal taste.. I beleive that many C++ programmers
use this convention as well.

Regards,
Shimon

May 30 '06 #7
"Daz" <cu********@gmail.com> wrote in message
news:11**********************@y43g2000cwc.googlegr oups.com...
Hi all!

This question may hopefully spark a little debate, but my problem is
this:

I am sure it's not just me who struggles to think up names for
variables. Obviously, thinking up a name 'can' be simple, but when you
are trying to create variable names that are easy to remember,
descriptive, and not more than say 15-20 characters long, I come a
cropper! Would anyone be able to give me any pointers as to how I can
create the variable names I am looking for, or is it simply a case of
having a notepad doc open and keeping track of what each name is?

Any comments would be appreciated.


Variable names are generally easier, as they only have to make sense in
context.

void ReadPlayerFile( const std::string& Filename )

My function names are generally a verb and a noun. A verb as to what I'm
going to be doing to the noun. I think that ReadPlayerFile is perfectly
descriptive and doesn't even neeed any commenting. Filename is fine,
because if I want to know what filename, I just look at the function name,
it's the PlayerFile name of course.

Iterators I've decided on going with "it" with a prefix as to what it's an
iterator for, keeping them short. This stems from the age old concensus to
name for loop variables i. So I use it for iterators. Here's a few
examples of functions and paramters I'm using in my current program:

void SendHealthToPlayer( const CPlayer& ThisPlayer )
bool CheckIfPlayersHit( map_key_pcmissile::iterator& mit )
EBodyParts ChracterHit( const CCharacter& Char, const JVEC3& BeamPos )
void CheckLimb( const int Value, const int Max, bool& Disabled, bool&
Destroyed )
void ReadMapData( const std::string FileName, CMapValues& MapValues );
void SendMessageToPlayer( const SOCKET Socket, const BYTE MessageType, const
std::string& Message );
void SendMessageToGMs( const BYTE MessageType, const std::string& Message );
void SendMessageToRange( const int Map, const JVEC3 Pos, const BYTE
MessageType, const std::string& Message, const float Range = 0 );
void SendMessageToMap( const int Map, const BYTE MessageType, const
std::string& Message );
void SendMessageToWorld( const BYTE MessageType, const std::string&
Message );
void SaveServerData()
CMap& FindMap( const unsigned int MapNumber )
CPlayer& FindPlayer( const SOCKET Socket )
CNPC& FindNPC( const unsigned int ID )
CPlayer& FindPlayer( const std::string Name )
std::string trim( const std::string& text, const char TrimChar = ' ' )
std::string SendIfLongToPlayer( const SOCKET Socket, const BYTE MessageType,
const std::string SendMessage, const unsigned int MaxLength = 70 )
std::string CreateCharMessage( const unsigned int Key, const CCharacter&
Character )
May 31 '06 #8
Daz

Shimon Shvartsbroit wrote:
First of all, I agree with Daniel T. You have to keep in mind that once
working in Object Oriented world, each object (or Entity) has set of
actions it can perform.
I also encourage the use of descriptive names. I enjoy orking with
Visual Assist (www.wholetomato.com). That way I can still keep
descriptive variable and member names without cost of coding time
that's spent on writing long variable names. Also, when I say
descriptive I mean that names can be long, but not redundant.
You can take VA for free trial and see how great it does work of code
suggestions and other miracles.

I beleive that code should tell a story. I think that Daniel Aarno
meant that as well. There's no point in commenting the obvious, like:

++i; // increment i

The above line has few goals:
1). distruct programmer from real program
2). making the code bigger and uglier
.. maybe some more.. but none of goals is good.

The most important thing to keep in mind, IMHO, is that you have naming
conventions and that everybody in your team, even if it's 1 men crew,
follows it.

When telling a story, there should be objects. Objects can talk with
other objects. Objects can do things, like loding a texture and more
on.. A good coding practice is to make functions short, as people
already mentioned, 10-20 lines. Each function should have a
well-defined purpose.

here's a brief sum of naming conventions I use:
s_someStuffGoingOn - static
m_blaFooWidget - member

Though, it's my personal taste.. I beleive that many C++ programmers
use this convention as well.


Thanks for such a well-thought-out response. I think everyone works in
very similar ways. My functions tend to be a lot longer, and I think I
see why they should be smaller. It's easier to maintain the code for
starters, however, I can never think up decent enough names for my
functions, that are not long and ugly. Eventually, I get functions
named like 'function1' with a brief description of what it does. I
don't believe that this is an acceptable mannor of coding, but I can't
help thinking that many more 'smaller' functions would make the code
harder to follow without using a debugger.

Thanks for the reccomendation of Visual Assist. It sounds useful, and
although I am still a little unclear as to what it does exactly, I will
sure give it a go.

Many thanks.

Daz

May 31 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Cristof Falk | last post by:
I wanted to get a feel. The documentation gives naming conventions for public/protected members. Is this truly widely adopted? And what about using the same conventions for private members and...
7
by: cmiddlebrook | last post by:
Hi there, I keep finding myself getting inconsistent with naming conventions for things like member variables, class names etc and I just want to find something that suits me and stick to it. I...
1
by: clintonG | last post by:
Does the use of DTD, XML Schema and similar constructs adopt the use of C# naming conventions? If so how do I make the distinction of how to apply C# conventions with XML elements, attributes and...
4
by: Mark Broadbent | last post by:
stupid question time again to most of you experts but this is something that continually bothers me. I am trying to get into the habit of naming variables and controls in an assembly as per...
5
by: rastaman | last post by:
Hi all, I know of the existence of Object Naming Conventions for Visual Basic 6. Now I'm reading some books about VB .NET, but the names used for the objects are button1, picturebox1, etc. I...
4
by: Patrick | last post by:
what are the general naming conventions for vb.net controls. esp txtUserName, lblPassword, btnSubmit What are the prefixes for the controls???? I've tried to search hi and low on the net, but...
9
by: kevininstructor | last post by:
Greetings, I am in the process of creating naming conventions for VB.NET controls i.e. CheckBox -> chkShowThisOnStartup ListBoxt -> lstSomeList What I am looking for other developers...
35
by: Smithers | last post by:
Is it common practise to begin the name of form classes with "frm" (e.g., frmOneForm, frmAnotherForm). Or is that generally considered an outdated convention? If not "frm" what is a common or...
1
by: Philipp Post | last post by:
Marcello, Not a big surprise as naming conventions have a lot to do with personal prefernces. There are a lot of threads in comp.databases and the other database groups. Just do a search for...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.