Sign in to follow this  

Capitalizing Variables

This topic is 3343 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Usually when I see a capitalized variable, I presume it's a constant. I prefer to see naming conventions that work with what I am used to seeing.

Share this post


Link to post
Share on other sites
Quote:
Original post by MrPickle
I have got into a habit of capitalizing the variables and I wasn't sure whether it was a bad one?


Almost without exception, capitalized names are used for types, and sometimes functions.

Share this post


Link to post
Share on other sites
Standards (or concensus at best) are language specific. Java and C# have relatively well established standards relative to C++.

In general, consistency is most important. Whatever you do, stick with it. If you are working in someone elses code, follow their standards.

Do a search for camelcase to find discussions on various standards (I'm not advocating it specificly, but its a decent search keyword)

Share this post


Link to post
Share on other sites
My style, the way I have stuck for a good few years now:

Types - Prefixed and Capitalised.
Enums - 'e' Prefix, and enum values prefixed lowercase with capital shorthand.
Member Enums - Lowercase prefix dropped.
Variables - CamelCase. Pointers are exclusion to this rule and prefixed 'p'
Member Variables - Same rule applied + trailing underscore.
Global Variables - Prefixed 'g', otherwise same as Member Variables.
Methods - Lower case.
Global functions - Prefixed same as types.
Defines - Capitals (With a few exceptions to some macros)

And also - Doxygen, Doxygen, Doxygen.

Sample class and some functions from my engine's content system:


//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
// From the H:
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////


//////////////////////////////////////////////////////////////////////////
/** \class CsArchive
* \brief Resource Archive.
*
* An archive to contain resources and provide runtime access to them.
*/

class CsArchive:
public BcThread // NOTE: This will be a worker thread interface soon.
{
public:
CsArchive();
~CsArchive();

/**
* Get a resource of a specific type.
* @param _Ty Class/Struct of resource.
* @param Name String name of resource in archive (no file extension).
* @return Pointer to resource class/struct.
*/

template< class _Ty >
_Ty* pResource( const BcChar* Name );

/**
* Get a raw resource of a specific type by its ID.
* @param Type Type of resource.
* @param ID ID of the resource.
* @return Pointer to raw resource.
*/

CsResource* pResourceByID( eResourceType Type, BcU32 ID );


/**
* Get user file packed into archive.
* @param Name of resource.
* @param Pointer to a BcU32 which will store the size.
* @return Pointer to raw data.
*/

BcU8* pFile( const BcChar* Name, BcU32* pSize = NULL );

/**
* Check if archive is ready to be used.
* @return BcTrue if archive is ready.
*/

BcBool isReady();

/**
* Check if archive is in error, ie, failed to load.
* @return BcTrue if in error.
*/

BcBool inError();

/**
* Get reference count of this archive.
*/

BcU32 refCount() const;


// Shortcut routines. Implemented to make sip's life easier. Don't use normally.
RsFlashMovie* getFlashMovie( const BcChar* Name )
{
return (RsFlashMovie*)pInternalResource( rt_RS_FLASH, Name );
}

RsFont* getFont( const BcChar* Name )
{
return (RsFont*)pInternalResource( rt_RS_FONT, Name );
}

SgNode* getNode( const BcChar* Name )
{
return (SgNode*)pInternalResource( rt_SG_NODE, Name );
}

private:
virtual void execute();

private:
friend class CsCoreImpl;

//
BcU32 RefCount_;
BcChar ArchiveName_[64];

//
enum eInternalState
{
IS_LOAD_PENDING = 0,
IS_LOADING_HEADER,
IS_LOADING_RESOURCE_HEADERS,
IS_LOADING_RESOURCE_DATA,
IS_DECOMPRESSING,
IS_DECOMRESSED,
IS_LOADED,
IS_UNLOAD_PENDING,
IS_UNLOADED,
IS_ERROR
};

//
void* pInternalResource( eResourceType Type, const BcChar* Name, BcU32* pSize = NULL );

//
void initLoad( const BcChar* FileName );
void initUnload();

//
void load();
void unload();

//
FsFile ArchiveFile_;

//
BcCompress Decompressor_;

// Useful structures
CsHeader ArchiveHeader_;
CsResource* pResourceList_;

// Start of raw data
BcU8* pArchiveData_;
BcU8* pArchiveResourceData_;

// Housekeeping
eInternalState InternalState_;

// Unload behavoir.
BcU32 TimeOut_;
};

//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
// From the CPP:
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////


//////////////////////////////////////////////////////////////////////////
// initLoad
void CsArchive::initLoad( const BcChar* FileName )
{
// Copy across name.
BcStrCopy( ArchiveName_, FileName );

// Open the archive
if( ArchiveFile_.open( FileName, fsFM_READ ) )
{
// Stream archive header in.
ArchiveFile_.streamTo( (BcU8*)&ArchiveHeader_, sizeof( ArchiveHeader_ ) );

// Set state
InternalState_ = IS_LOADING_HEADER;
}
else
{
BcPrintf( "** ERROR: Archive \"%s\" failed to open **\n", FileName );
InternalState_ = IS_ERROR;
}
}

//////////////////////////////////////////////////////////////////////////
// initUnload
void CsArchive::initUnload()
{
BcAssert( InternalState_ == IS_LOADED );

InternalState_ = IS_UNLOAD_PENDING;
}




Oh, answer to the capitalised variable names - much prefer it. Easier to read as the capital breaks up lines quite nicely - as you typically use more variables in a statement than function calls [smile]

Share this post


Link to post
Share on other sites
Richy's code makes me cry.

I use the boost standards:

namespace my_enum
{
enum Enum
{
a_choice,
another_choice
};
}

class a_class_name
{
int private_variable_;
public:
int public_variable; // no trailing underscore!

int function_name();
int another_function(my_enum::Enum e = my_enum::a_choice);
};




The enums aren't exactly boost-style, but I find they're really good. C++09 ftw (strongly named enums).


Share this post


Link to post
Share on other sites
Quote:
Original post by _goat
Richy's code makes me cry.

I use the boost standards:

*** Source Snippet Removed ***


The enums aren't exactly boost-style, but I find they're really good. C++09 ftw (strongly named enums).


Hey, i started doing that with my enums a long time ago.
Because scoping and qualifying is so much better.
Except instead of Enum i use type.

I didnt even realise boost did this too. So i must've had a few decent ideas after all :)

Share this post


Link to post
Share on other sites
I use the prime number system. Basically, if a character's position in the variable name is a prime number, it's capitalized, otherwise it isn't. For public variables I use 0-based indexing, and for private variables I use 1-based indexing. For static member variables, the indexing rule is inverted. For member functions, I index in reverse, from the end of the name to the beginning. Works like a charm!

But seriously, for local variables I tend to stick with camelCase, for enumerations I like ALL_CAPS, for member variables I LikeToCapitalize, and for member function I like to Capitalize_With_Some_Underscores. If it's only a single word, then it's easy to tell if it's a function or a variable from the name itself (verbs versus nouns, usually).

Share this post


Link to post
Share on other sites
Quote:
Original post by Richy2k
My style, the way I have stuck for a good few years now:

Change it. First, no one likes hungarian notation (Not even Microsoft, who invented it), but eh, that's subjective.
Also, as far as I'm aware, it's camelCase, not CamelCase. (Don't capitalize the first letter).
A much bigger reason is that your template types have leading underscore + starts with capitalized letter. That's nasty.
C++ reserves all identifiers starting with _ followed by capital letter for the implementation. (And all identifiers starting with _ in the global namespace, and all identifiers starting with double __, IIRC. It's easiest to just avoid all leading underscores)

Change that, even if you want to stick with the more subjective oddities, get rid of the leading _'s.

Share this post


Link to post
Share on other sites
Usually what my mood is how i decide my naming conventions :P.

I usually go with Lowercase only if i feel lazy. (Because sometimes pressing Shift + Character is too cumbersome.)

Share this post


Link to post
Share on other sites
I realize this is very much a "Color of the Bikeshed", aesthetic issue, but I hate the trailing underscore to indicate private variables.

These:
variableName_
m_variableName
_variableName

Seem to be the most common. I was introduced to the last by my freshman Computer Science professor at uni, in the context of Java, and I've used it ever since. My style looks like this (other bits come from Effective C++, and a large dose of personal preference):

class ClassName
{
public:
void methodName(int varName);

private:
void _methodName();
int _someVariable;
};

void function_name();

enum MyEnumType { FooBar, BarBaz };







And something similar for Python. Oh, and I always use tabs for indentation. So there. Just don't get me started on where to put the curly brackets.

Share this post


Link to post
Share on other sites
Quote:
Original post by drakostar
Seem to be the most common. I was introduced to the last by my freshman Computer Science professor at uni, in the context of Java, and I've used it ever since. My style looks like this (other bits come from Effective C++, and a large dose of personal preference):


How about doing nothing at all to member variables? When you need to tell them apart from other variables, use the this-> prefix.

While the underscore should be valid in your case (as far as I can remember), it still seems like tempting fate. _variableName is reserved in the global namespace (so yes, it's technically legal in class scope, but if you accidentally make a typo, inst, you may end up referring to an identifier in the global namespace that you didn't know existed, instead of the class member you wanted to reference), and as long as you stick to lower-case for the first letter, you again dodge the "reserved by the implementation" identifiers in other namespaces.

I realize it's common in C# and Java, but I'd still avoid it in C++, if only because it comes damn close to breaking the rules... [grin]

Share this post


Link to post
Share on other sites
Quote:
Original post by Zipster
I use the prime number system. Basically, if a character's position in the variable name is a prime number, it's capitalized, otherwise it isn't. For public variables I use 0-based indexing, and for private variables I use 1-based indexing. For static member variables, the indexing rule is inverted. For member functions, I index in reverse, from the end of the name to the beginning. Works like a charm!


I love it. See also the recent C++ true random numbers thread for more exciting possibilities.

Share this post


Link to post
Share on other sites
Quote:
Original post by Spoonbender
How about doing nothing at all to member variables? When you need to tell them apart from other variables, use the this-> prefix.
this-> gives a warning in visual studio about non-standard extensions when used in initializationlists, without this-> it is silent. I am unsure if it is safe to send members to other constructors (at all) during initialization though.

Share this post


Link to post
Share on other sites

This topic is 3343 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this