Archived

This topic is now archived and is closed to further replies.

Formats of coding !

This topic is 5109 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

Well. i was wondering what format you guys use for the following , this is what i use : Classe name : C + Name - CCore Classe func : Engine init in l.c + Func name - cbStartEngine Classe vari : Engine init in l.c + var name - cbMouseX Struct name : Engine init in u.c + name - CBIMAGE Struct vari : Engine init in l.c + var name - cbData enum : Engine init in u.p + name - CBPARAMS function params : all in lower case - .. (int x,int y) var inside functions : all in lower case - bits #infndef : _ + Class name in u.c + _ + H + _ - _CCORE_H_ if i''m missing something please tell me :D thx , and i''m waiting to see some replies
Metal Typhoon

Share this post


Link to post
Share on other sites
I personally prefer _C prepended to my classes and _S prepended to my structs. Also, I do''t do anything special with class member''s/functions, i just leave them as is. I don''t always make function params all lower case, for example, (int xPos, int yPos).

Nothing wrong with your format, as long as it''s easy enough to read.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Please do a search for coding style, or just style, or code formatting. There has been plenty of discussion about style and there isn''t any specific style. It''s what you prefer.

Try asking more focused questions if you want real answers that will help you out.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Classe name : Capatalized with _ between words : ex Test_Class
Classe func : lower case with _ between words : small_func
Classe vari : lower case with m_ prefeix and _p for ptr, : ex m_p_test ( test is a pointer )
Struct name : Same as Classes
Struct vari : Samve as Classes func
enum : All upper case : TEST_ENUM
function params : all lower case as well

local clases, structs, and enum defined within a class : prepend the _ : ex _Sub_Class, _SUB_ENUM

-ddn

Share this post


Link to post
Share on other sites
quote:
Original post by Beer Hunter
Names beginning with underscores are reserved for the implementation.
I am the implementor of my own classes! I prefix private memebers with underscore, and nobody, and I repeat NOBODY!, can stop me! MUAHAHAHA... /runs into wall

Share this post


Link to post
Share on other sites
I think just about all class, struct, enum, identifiers and local and global variables I capitalize the first letter of each word in the identifier name (ex: SomeVariable, SomeClass, SomeStruct, etc). Then I prepend some small descriptor to the identifier name, as follows:

Interface classes - prepend an I (ex: IRenderer)
Concrete classes - prepend a C (CDirectDrawRenderer)

Following pertains to members of classes:
Boolean variable - prepend a m_b (ex: bool m_bFavorVRAM)
Numerical (int/float/double) variable - prepend a m_n (ex: int m_nWidth)
Pointer variable - prepend a m_p (ex: LPDIRECTDRAWSURFACE7 m_pBackBuffer)
Variable that does not fall under one of the above categories - prepend a m_ (ex: SurfaceProps m_SurfaceProps)

In the next batch, I refer to local as either declared in local scope, or a function parameter:
Local boolean variable - prepend a b (ex: bool bSuccess)
Local numerical variable - prepend a n (ex: int nNumBytes)
Local pointer variable - prepend a p (ex: unsigned char* pBuffer)
Local variable that does not fall under one of the above categories - prepend nothing (ex: DDPIXELFORMAT SurfacePixelFormat)

Pertains to global variables:
Boolean variable - prepend a g_b (ex: bool g_bMinimized)
Numerical (int/float/double) variable - prepend a g_n (ex: int g_nSomeRandomCounter)
Pointer variable - prepend a g_p (ex: IRenderer* g_pRenderer)
Variable that does not fall under one of the above categories - prepend a g_ (ex: WindowProps g_WinProps)

Some special notes:
Macros and Constants - all capital letters, each word separated by an underscore (ex: const int SCREEN_WIDTH = 640)
Enumerations in an enum - all captial letters, each word separated by an underscore, the first word is the enum name (ex: enum PixelFormat { PIXELFORMAT_24BIT }; )


[edited by - MRom on December 12, 2003 10:58:51 PM]

Share this post


Link to post
Share on other sites
Hmm, just make sure its standard in all your code. Personaly,
i like to mush things together in an almost hungarian notation sort of deal. generaly it works like this.

Classes: MYLIBClass

for instance, if i was writing the back end of PuzzleMania, and i was doing a map class, it would be something like PMMap.

for boolean variables its always

ifImdead, ifThisistrue, ifWater, im sure you get the drift

numbers are

nMax, nSize, exc.

width and height are just w and h though.

Strings are sMyString, sThis, exc.

hmm.... i think thats about it. oh yea, floating point variables are always fudged for me, since i usualy only use it internaly in 3d things, so it doesent matter the consistancy.

Share this post


Link to post
Share on other sites
quote:
Original post by PaulCesar
Hmm, just make sure its standard in all your code. Personaly,
i like to mush things together in an almost hungarian notation sort of deal. generaly it works like this.

Classes: MYLIBClass

for instance, if i was writing the back end of PuzzleMania, and i was doing a map class, it would be something like PMMap.

for boolean variables its always

ifImdead, ifThisistrue, ifWater, im sure you get the drift

numbers are

nMax, nSize, exc.

width and height are just w and h though.

Strings are sMyString, sThis, exc.

hmm.... i think thats about it. oh yea, floating point variables are always fudged for me, since i usualy only use it internaly in 3d things, so it doesent matter the consistancy.
Gee... I understand why you are on probation. On a second thought, I don''t; Hungarian Notation calls for death sentence, at least.

Come on people, do I have to keep the flame going all by my self?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Coding/naming style for classes, structs, and vars:
class ClassName
{
int VarName;
char *SomeNameOrArrayToAllocateAndUse;
short nNames or NumOfNames or NumberOfNames;
char AnotherVar;
float Score1,Score2,Score3;
};
// All my structs are typedef''s, usually my classes aren''t
typedef struct JELLOSTRUCT
{
// same as classes
} Jello (or JelloStruct depening on my mood), *Jello_Ptr;

Any function(class/struct or regular):
void MyFuntion(....);

defines:
#define MAXSOMETHING 0x00

Header file definitions(for every little .h I make):
#ifndef _HEADER_FILE_NAME_H
#define _HEADER_FILE_NAME_H
...
#endif

Every function(not prototypes) are devided by a line of comment:
void Blee(void)
{
}
/************************************************************/
void Blah(void)
{
}

What am I missing... umm.. oh! Brackets; never on the same line as code, indented 2 spaces from the previous block making the code 3 indented 3 spaces(exept functions), and used to make things easier to read:
// Ifs
if (Blee)
{
...
}
// Whiles
while (Monkey)
{
...
}
// Functions
void FuncName(...)
{

}
// Readability
glBegin(...)
{
...
}
glEnd();

Thats basically how I do things or atleast the bare minimum(unless I''m lazy ;-D).

-Linolium

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
Aww man, my spacing didn''t show up :-(

Use the mighty

int main()
{
printf("[code] tag!!!!"");
// you can have as many spacing as you want
}

This topic has been discussed so many times, but it doesn''t hurt to post it back. After all I am proud of my style now.

class ClassName
{
protected:
int somethingHere;
float anotherHere;
string yetAnotherHere;
public:
void DoSomething( int paramname, string anotherone );
void KillMe( int action );
};

I tend to make parameters names really short. for constants, I put them in an enum, and I don''t use UPPERCASE, they just don''t look nice.

enum UI
{
Button,
Label
};

Local variables all are written in lowercase, same with parameters. prefix g_ for globals.

Share this post


Link to post
Share on other sites
I''m slowly getting sucked into the habit of using Java naming conventions. I started out with gung-ho hungarian, got tired of that, fell into what is now the C# convention, and have been busy sliding into lowercaseness ever since.

By this time next year, I expect to be using lowercase for all identifiers everywhere.

Help me. ;_;


"Without deviation, progress itself is impossible." -- Frank Zappa

Share this post


Link to post
Share on other sites

class ALLTYTPENAMESAREINALLCAPITALS
{
private: // Unlike most people, I always start with private, and then the puclics, but maybe I''ll change that
int variableNames;
std::string areInCamelBackNotation;
int because;

void Foo()
{
// it''s annoying to use a capitalized I as the counter variable
for(int i = 0;i < 10;i++);

// and I want one style for all my variable names,
// so I use camel back

}

public:
void FunctionNamesAreEachWordWithANewCapital();
};

typedef unsigned long DWORD; // DWORD is a type too

enum LALAL
{
CONSTANTS,
ARE,
ALSO,
CAPATILIZED_WITH_UNDERSCORES_BETWEEN_THE_WORDS,
};

int main()
{
// no hungarian notation for variable names
// only for somethings, like number of ... I add an n,
int nVertices; // like this

return 0;
}


My Site

Share this post


Link to post
Share on other sites
Hungarian notation should die. If I see one more C in front of a class declaration I am going to explode. All I use is a very rare p in front of pointers, such as when I get the pointer to the buffer from vertexbuffer->Lock().

Share this post


Link to post
Share on other sites
My style tends to adapt depending on what I'm doing. When working with the STL or boost I'll lean towards lower_case, where as with OpenGL or the Windows API I'll favour CapitializingEveryWord. Whatever makes things flow nicely.

The only real convention I have is an underscore appended to private_ member_ data_ which is more because I got sick of having to do things like this->foo = foo because I didn't feel like coming up with a unique name to pass into a member function. It's also useful in discerning between local data, parameters, and member data in methods.

There's nothing particularly wrong with prepending C to classes as long as you're actually distinguishing between objects, functions and interfaces ie. CObject vs IInterface vs SomeFunc, though a comment or a descriptive name would do just as well. If you just automatically tack on a C just because it follows the class keyword plz stop, kthx.



[My site|SGI STL|Bjarne FAQ|C++ FAQ Lite|MSDN|Jargon]
Ripped off from various people

[edited by - wild_pointer on December 13, 2003 7:36:16 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Beer Hunter
quote:
Original post by PlayGGY
You should use a C in front of a class name... it shows it isn''t a function or an object.
So does the name of the class. So does context. In some styles, so does capitalisation.


What name of a class could show that it is a class?

Share this post


Link to post
Share on other sites