Coding Style: Please Help Improve

Started by
27 comments, last by GameDev.net 19 years, 8 months ago
Not commenting on actual code:

It's certainly much prettier then mine...

The only thing I can suggest is to tab out prototypes; at least the comments.

For me at least, I find prototypes much easier to read and just cleaner when they're in columns:

void       proto1        (int,int,float);          // some commentsint        proto2bigname ();                       // more commentsstruct foo proto3        (void *);                 // blah blah


or

void proto1(int,int,float);         // some commentsint proto2bigname();                // more commentsstruct foo proto3(void *);          // blah blah
Advertisement
Quote:Original post by Skizz
5) Put '{}'s after all 'if's, 'for's and 'while's. It can save a lot of headaches caused by not paying attention to what your typing.
7) Put breaks in multiple word variable names to make it easier
I totally agree apart from 5 and 7.
Leaving out {} after all 'if's etc has NEVER caused me any headaches.
And it's easy to accidentally type a space instead of an underscore and not notice it.

benryves: That coding style is fairly common. I for one use it all the time. Don't switch back, I sure aren't going to.
Otherwise by the time you put comments in, your functions still take more than a screenful - aaargh.

Terlenth: Doing variable initialisations like
 	int Startposition = 0;
is much nicer than doing the same in two lines.
			totallength = totallength - Length;
can also be simplified to:
			totallength -= Length;

And
			Staircounter = Staircounter - 1;
becomes
			Staircounter--;
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
The only thing I would add is to use constants for the different drawtypes instead of hard-coding the values 1, 2, and 3 everywhere. You could use enum, or define constants, or use #define (your preference) - but the main thing is to give them meaningful, self-documenting names.
I just thought I would mention this. I like prototypes. In fact I usually format them according to who calls who, and even have comments in place of functions who prototypes have already been defined. For example:

void FunctionA(void)    void FunctionB(void)void FunctionC(void)    // FunctionB


This allows for a nice index of all functions in the file at the top of your source file. I even go as far as to order the definitions of the functions in the leveled order that is:

void FunctionA(void){}void FunctionC(void){}void FunctionB(void){}


This sort of thing often allows me to access my functions quickly without the aid of a fancy editor, and in my oppinion just looks nicer.

Then again, as I have said, this is what I do, and I have not seen anyone else actually do this. :D
I've updated my code (see above). Tell me what you think.

Thanks
--Ter'Lenth
*bump*

I'm just going to bump this once just in case someone who missed it after the update has something they may be able to add. If not then I'll just leave it at that.

Thanks again
--Ter'Lenth
I have nothing to add to what's already been said. I just want to tell Terlenth that I respect that you took active initiative to improve your coding style. That initiative will land you a wonderful job one day. Good luck.
I'm hoping so =p

I hope that what I'm doing here will help me with my school and help me develop my coding skill so that I can hopefully get a good job.
--Ter'Lenth
Either separate different words in function names (and variables) with underscores or use java style capitalization from second word on. It really doesn't matter what you do, but it would really help the readability of your code.

functions that have names like superlongfunctionname aren't as pleasent on the eyes as:
super_long_function_name
superLongFunctionName
SuperLongFunctionName
...
Kevin.
Here's some recommendations for you:
Quote:
////////////////////////
// Greg Powell //
// 0221498 //
// Asignment #1 //
////////////////////////

This file header is way too complex. Having a border on the right is much more trouble than it's worth. Whenever you change a line to be longer you have to extend the entire border, making it a hassle.
Quote:
void stairsfromleft (int Length, int Height, int Numberstairs); //First type of staircase starting from the left
void stairsfromright (int Length, int Height, int Numberstairs); //Second type of staircase starting from the right
void convergingstairs (int Length, int Height, int Numberstairs1, int Numberstairs2); //Third type of staircase converging in the middle
int draw (int Length, int Startposition, int Drawtype, int Totallength, int Direction);

You should probably pick a better naming convention for functions. Analllowercasenamingconvention takes more effort to decipher than something like StairsFromLeft or stairsFromLeft. Also, you should almost always use verbs for function names. Also, the draw function doesn't specify what it's drawing in the name. If it's meant to draw the scene you should probably rename it to 'drawScene' or 'drawWorld.'
Quote:
int Length; //Length of stairs
int Height; //Height of stairs

You need to rethink these variable names. Renaming them so that they have 'Stair' in their names is much more readable than commenting it. Also, you should comment the type of units you are using.
Quote:
int Numberstairs1; //Number of stairs from the left
int Numberstairs2; //Number of stairs from the right

You should never put numbers in variable names. If they truly should be numbered, then you should put them in an array. You should probably reword those comments, they're fairly hard to understand. Also, you should think about using another naming convention for variable names.
Quote:
Length = (rand() % 2) + 3;
Height = Length / 2;
Numberstairs1 = (rand() % 5) + 2;
Numberstairs2 = (rand() % 5) + 2;

I see a lot of magic numbers here. Consider replacing them with named constants.
Quote:
for (Heightcounter = 0; Heightcounter < Height; Heightcounter++)
{
draw(Length, Startposition, DRAWTYPE1 , NO_LENGTH_REQUIRED, VERTICAL);
printf("\n");
}

I see duplicate code. You should put this into a seperate function.
Quote:
void convergingstairs(int Length, int Height, int Numberstairs1, int Numberstairs2)
{
int Staircounter;
int Heightcounter;
int Totallength;
int Startposition;
int Drawtype;
int Largernumberofstairs;
int Totalnumstairs;

Totalnumstairs = Numberstairs1 + Numberstairs2;
Totallength = (Length) * (Totalnumstairs);

if (Numberstairs1 == Numberstairs2)
{
Startposition = LEFT_START_POSITION;
Drawtype = DRAWTYPE3;
Largernumberofstairs = Numberstairs1;
for (Staircounter = 0; Staircounter < Largernumberofstairs; Staircounter++)
{
Startposition = draw(Length, Startposition, Drawtype, Totallength, HORIZONTAL);
printf("\n");
for (Heightcounter = 0; Heightcounter < Height; Heightcounter++)
{
draw(Length, Startposition, Drawtype, Totallength, VERTICAL);
printf("\n");
}
Totallength -= Length;
}
draw(Length, Startposition, DRAWTYPE1, NO_LENGTH_REQUIRED, HORIZONTAL);
printf("\n");
}
if (Numberstairs1 > Numberstairs2)
{
Startposition = LEFT_START_POSITION;
Drawtype = DRAWTYPE1;
Largernumberofstairs = Numberstairs1;
Staircounter = Numberstairs1;
while (Staircounter != Numberstairs2)
{
Startposition = draw(Length, Startposition, Drawtype, Totallength, HORIZONTAL);
printf ("\n");
for (Heightcounter = 0; Heightcounter < Height; Heightcounter++)
{
draw(Length, Startposition, Drawtype, Totallength, VERTICAL);
printf("\n");
}
Totallength -= Length;
Staircounter--;
}
Totallength += Length * (Numberstairs1 - Numberstairs2);
for (Staircounter = 0; Staircounter < Numberstairs2; Staircounter++)
{
Drawtype = DRAWTYPE3;
Startposition = draw(Length, Startposition, Drawtype, Totallength, HORIZONTAL);
printf ("\n");
for (Heightcounter = 0; Heightcounter < Height; Heightcounter++)
{
draw(Length, Startposition, Drawtype, Totallength, VERTICAL);
printf("\n");
}
Totallength -= Length;
}
draw(Length, Startposition, DRAWTYPE1, NO_LENGTH_REQUIRED, HORIZONTAL);
printf("\n");
}
if (Numberstairs2 > Numberstairs1)
{
Startposition = Totallength + Length;
Drawtype = DRAWTYPE2;
Largernumberofstairs = Numberstairs2;
Staircounter = Numberstairs2;
while (Staircounter != Numberstairs1)
{
Startposition = draw(Length, Startposition, Drawtype, Totallength, HORIZONTAL);
printf ("\n");
for (Heightcounter = 0; Heightcounter < Height; Heightcounter++)
{
draw(Length, Startposition, DRAWTYPE1, Totallength, VERTICAL);
printf("\n");
}
Totallength -= Length;
Staircounter--;
}
Startposition = LEFT_START_POSITION;
for (Staircounter = 0; Staircounter < Numberstairs1; Staircounter++)
{
Drawtype = DRAWTYPE3;
Startposition = draw(Length, Startposition, Drawtype, Totallength, HORIZONTAL);
printf ("\n");
for (Heightcounter = 0; Heightcounter < Height; Heightcounter++)
{
draw(Length, Startposition, Drawtype, Totallength, VERTICAL);
printf("\n");
}
Totallength -= Length;
}
draw(Length, Startposition, DRAWTYPE1, NO_LENGTH_REQUIRED, HORIZONTAL);
printf("\n");
}
}

This function should be broken up into other functions. Generally speaking, if you can't fit the whole function on the screen, it needs to be broken up. Also, >3 levels of nesting decreases the readability of a function. Whenever I program I try and keep the nesting level less than 3.
Quote:int draw (int Length, int Startposition, int Drawtype, int Totallength, int Direction)
{
int Returnvalue;
int Positioncounter;
char Space = ' ';
char Horizontaldash = '_';
char Verticaldash = '|';

if (Direction == HORIZONTAL)
{
switch (Drawtype)
{
case DRAWTYPE1:
for (Positioncounter = 0; Positioncounter < Startposition; Positioncounter++)
printf ("%c ", Space);
for (Positioncounter = 0; Positioncounter < Length; Positioncounter++)
{
printf ("%c ", Horizontaldash);
Startposition++;
}
break;
case DRAWTYPE2:
for (Positioncounter=Startposition; Positioncounter > Length; Positioncounter--)
printf("%c ", Space);
for (Positioncounter = 0; Positioncounter < Length; Positioncounter++)
{
printf ("%c ", Horizontaldash);
Startposition--;
}
break;
case DRAWTYPE3:
for (Positioncounter = 0; Positioncounter < Startposition; Positioncounter++)
printf ("%c ", Space);
for (Positioncounter = 0; Positioncounter < Length; Positioncounter++)
{
printf ("%c ", Horizontaldash);
Startposition++;
}
for (Positioncounter = Totallength; Positioncounter > Startposition; Positioncounter--)
printf ("%c ", Space);
for (Positioncounter = 0; Positioncounter < Length; Positioncounter++)
printf ("%c ", Horizontaldash);
break;
}
Returnvalue = Startposition;
}
if (Direction == VERTICAL)
{
switch (Drawtype)
{
case DRAWTYPE1:
for (Positioncounter = 0; Positioncounter < Startposition; Positioncounter++)
printf ("%c ", Space);
printf ("%c ", Verticaldash);
break;
case DRAWTYPE3:
for (Positioncounter = 0; Positioncounter < Startposition; Positioncounter++)
printf ("%c ", Space);
printf ("%c ", Verticaldash);
for (Positioncounter = Totallength; Positioncounter > Startposition + 1; Positioncounter--)
printf ("%c ", Space);
printf ("%c ", Verticaldash);
break;
}
Returnvalue = 0;
}
return Returnvalue;
}

This function also needs to be broken up.

I also recommend reading Code Complete; it has a truckload of information regarding readability and style. It does center around object oriented languages, but most of the stuff on procedures still apply.

This topic is closed to new replies.

Advertisement