gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > Commenting density...

#134498 - keldon - Fri Jul 13, 2007 1:02 pm

Reason I ask: Coding style test for a development team

So what are your thoughts on commenting? My view is that your project has a general outline of where to find things and where to start looking for particular areas.

Then each class details its general purpose and gives enough information to be able to use it (I like the JavaDoc style) plus the key methods. Each method (obviously) is well documented with more than enough details to know its limitations, use, blah blah and so on.

Now when it gets to the methods some people comment every line or logical stage. Their task was to correct errors and write the code as you would, so maybe I'm over thinking this and should just write it in my style.

They originally had everything in a single main method,
Code:
// ---------------------- //
// Standard Include Files //
// ---------------------- //
#include <stdlib.h>
#include <stdio.h>

// --------------------- //
// Structure Definitions //
// --------------------- //

// Definition of a player
typedef struct PLAYER
{
   int id;
   char player_name[ 50 ];
   double ability;
   double potential;
};

// Function:   main()
// Purpose:    Main function for the program
void main( void )
{
   FILE *p_file;      // File being read in from
   PLAYER *player;      // Player stored in file

   // Open the file to read from
   p_file = fopen( "file.txt", "r" );

   // Read out the player information
   fread( &player, sizeof( PLAYER ), 1, p_file );

   // Print the players name to the screen
   printf( "%s\n", player->player_name );

} // main()

Which I have changed it to something more on the lines of...
Code:
/** Will create an example file (file.txt) with a single PLAYER entry */
void createExampleFile (){
   PLAYER myPlayer;
   FILE *p_file;

   // initialize player with basic information
   myPlayer.id = 1;
   myPlayer.ability = 1.0;
   myPlayer.potential = 1.0;
   strcpy ( (char *)&myPlayer.player_name, "Alan Smith");

   // open and write to file
   p_file = fopen ( "file.txt", "w");
   fwrite ( &myPlayer, sizeof(PLAYER),1,p_file);

   fclose(p_file);
} // createExampleFile ()


And also added:
Code:
/**
   id: Each player has a unique id number

   player_name: Can contain all names (first, middle names, surname) or any
   one of the three. If more than one is given the first is expected
   to be the first name and the last to the the surname. If one is given it
   is expected to be the surname. Players are referred to by the surname during
   play, and the first name is used to distinguish between players with the
   same name

   ability: in the range of 0-1, with 0 being poor and 1 being excellent

   potential: in the range of 0-1, with 0 being poor and 1 being excelelnt
*/
typedef struct
{
   int id;
   char player_name[ 50 ];
   double ability;
   double potential;
} PLAYER;


Maybe I'm over thinking this.

#134513 - simonjhall - Fri Jul 13, 2007 2:51 pm

The density of comments is often driven by the complexity of the piece of code being explained. For instance a class which outputs simple things (for instance multiplication tables) will need few comments as your 'average' programmer will be able to figure out what's going on.
However if you're commenting something which is complicated (eg stuff has to be done in a very specific order) you're gonna need more comments as the order may not be obvious to your 'average' programmer.

So really, you need to consider four things when writing documentation/commenting:
1) figure what out the level of your reader will be. eg if you're teaching programming then you should comment the most mundane things
2) decide how complicated the piece of code will look to your reader
3) if you were to pick this piece of code up in six months time, how many comments would you need to understand its function, and how many comments would it take to annoy you?
4) how much time do you have to spend on commenting?

One thing I've come across first-hand is commenting vectorised/unrolled code. As long as you comment one iteration - and make it obvious that the other iterations are running on different, but similar data - it should be ok :-)

PS: I don't comment my code.
_________________
Big thanks to everyone who donated for Quake2

#134520 - PeterM - Fri Jul 13, 2007 3:42 pm

I think comments are usually necessary when the purpose of the code isn't immediately obvious (due to task complexity or programming language deficiencies).

Commenting logical steps to allow readers to skim through code is good form.

Sensible identifiers also go a long way to make comments redundant. For example, "player.move(elapsed_time)" doesn't need a comment, whereas "p->do_it(t)" would warrant a comment (or some renaming).
_________________
http://aaiiee.wordpress.com/

#134535 - sajiimori - Fri Jul 13, 2007 6:54 pm

Good responses so far.

One thing I'd add: Try to reduce the need for comments. As you're writing a comment, ask yourself: "Why is this comment necessary, and is there a way to alter the code so the comment would become redundant and could therefore be deleted?"