Originally Posted by
mike_g
In java I use the same style that everything else is coded in except I like to have the opening brace to a block of code sit on its own line.
Most people seem to like this, however, I've always been more fond of keeping the opening brace on the same line as the definition (or statement, as the case may be) and no indentation on the closing brace.
Code:
if (true) {
/* Code Body */
}
void myFunction(void) {
/* Code Body */
}
Constants are generally written in full caps with underscores between words (if it is several words, which my constants usally aren't), variables are camel case, function and class definitions are the same way... pointers generally will have "ptr" somewhere in its identifier (the exception being obvious pointers like the *child member of a BST class). I guess that's about it... easy rules to follow and I haven't had any trouble reading my code, nor has anyone else that's read it. I am in work so I don't have any modern C code to show... however, I found an old little snake game I wrote on the internet that should give an idea of my style. Here is a sample (nevermind the poor coding (like not checking the result of malloc)):
Code:
void initGame(snakeGame *game) {
HANDLE hOut;
CONSOLE_CURSOR_INFO ConCurInf;
SMALL_RECT DisplayArea = {30, 0, 72, 23};
/* Initialize the game window */
hOut = GetStdHandle(STD_OUTPUT_HANDLE);
ConCurInf.dwSize = 1;
ConCurInf.bVisible = FALSE;
SetConsoleCursorInfo(hOut, &ConCurInf);
SetConsoleWindowInfo(hOut, TRUE, &DisplayArea);
SetConsoleTitle("Snake");
srand(time(0)); /* rand() is used in generating the apple location */