CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

how to comment code (the right way)

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
petfo
Guest







how to comment code (the right way)
PostPosted: Tue Apr 20, 2004 2:54 pm     Reply with quote

i know this isn't realy a CCS PCx related thread, but i would realy like to see how you guys comment your code.

how detailed etc. would you for example comment and explain the source code of ex_mouse.c ?

Code:
/////////////////////////////////////////////////////////////////////////
////                                                                 ////
////                           EX_MOUSE.C                            ////
////                                                                 ////
//// An example showing how to make a serial mouse using the CCS C   ////
//// compiler.  Your operating system / computer has to be able      ////
//// to use a Microsoft Serial mouse or a Mouse Systems serial       ////
//// mouse.                                                          ////
////                                                                 ////
//// Requires a special CPC cable connected to Port A.               ////
////                                                                 ////
//// Wiring diagram for prototype board:                             ////
//// -----------------------------------------------------------     ////
//// 40 to 53  (Button 40 becomes left mouse button)                 ////
//// 41 to 54  (Button 41 becomes right mouse buttom)                ////
//// 9 to 15   (Pot 9 becomes X movement)                            ////
//// 10 to 16  (Pot 10 becomes Y movement)                           ////
//// 7 to 12   (PC RxD connects to PIC C6 for Xmit of data)          ////
//// 11 to 47  (PC DTR connects to PIC B0 for reset interrupt)       ////
////                                                                 ////
////  This example will work with the PCB, PCM and PCH compilers.    ////
/////////////////////////////////////////////////////////////////////////
////        (C) Copyright 1996,2003 Custom Computer Services         ////
//// This source code may only be used by licensed users of the CCS  ////
//// C compiler.  This source code may only be distributed to other  ////
//// licensed users of the CCS C compiler.  No other use,            ////
//// reproduction or distribution is permitted without written       ////
//// permission.  Derivative programs created using this software    ////
//// in object code form are not restricted in any way.              ////
/////////////////////////////////////////////////////////////////////////


#if defined(__PCB__)
#include <16c56.h>
#device ADC=8
#fuses HS,NOWDT,NOPROTECT
#use delay(clock=20000000)
#use rs232(baud=1200, xmit=PIN_A3, rcv=PIN_A2,errors)  // Jumpers: 11 to 17, 12 to 18

#elif defined(__PCM__)
#include <16F877.h>
#device ADC=8
#fuses HS,NOWDT,NOPROTECT,NOLVP,NOPUT
#use delay(clock=20000000)
#use rs232(baud=1200, xmit=PIN_C6, rcv=PIN_C7,errors)  // Jumpers: 8 to 11, 7 to 12

#elif defined(__PCH__)
#include <18F452.h>
#device ADC=8
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=1200, xmit=PIN_C6, rcv=PIN_C7,errors)  // Jumpers: 8 to 11, 7 to 12
#endif

#define MICROSOFT TRUE
#define MOUSE_SYSTEMS FALSE   //not tested.  may not be compatable with Windows

#DEFINE X_CHANNEL 0
#DEFINE Y_CHANNEL 1

struct {
   short int delta;
   short int left;
   short int right;
   short int middle;
   signed int x;
   signed int y;
} mouse;

void clear_all_mouse(void) {
   mouse.delta=0;
   mouse.x=0;
   mouse.y=0;
   mouse.left=0;
   mouse.right=0;
   mouse.middle=0;
}


void get_data(void) {
   signed int pos;

   set_adc_channel(X_CHANNEL);
   delay_us(10);
   pos=read_adc() & 0xF8;
   mouse.x=(pos-128)/10;

   set_adc_channel(Y_CHANNEL);
   delay_us(10);
   pos=read_adc() & 0xF8;
   mouse.y=(pos-128)/10;

   if ((mouse.x)||(mouse.y)) {mouse.delta=1;}
}

void send_data(void) {
#if MICROSOFT
    putc(0xC0 | (mouse.left << 5) | (mouse.right <<4 ) | ((mouse.y >> 4) & 0x0C) | ((mouse.x >> 6) & 0x03));
    putc(0x80 | mouse.x & 0x3F);
    putc(0x80 | mouse.y & 0x3F);
   mouse.delta=0;
   mouse.x=0;
   mouse.y=0;
}

void main(void) {
   clear_all_mouse();

   setup_adc_ports(RA0_RA1_RA3_ANALOG);
   setup_adc(ADC_CLOCK_DIV_32);
   ext_int_edge(H_TO_L);
   enable_interrupts(int_ext);
   enable_interrupts(global);

   while (TRUE) {
      get_data();
      if (mouse.delta) {send_data();delay_ms(17);}
   }
}
 
Steve H.
Guest







PostPosted: Wed Apr 21, 2004 9:34 pm     Reply with quote

Commenting on comments, eh?

That's always a matter of personal taste. I personally like to write comments so I put bunches of them in. You can browse my hobby site and see some code examples at www.geocities.com/hagtronics

I would recommend that you buy and read "Code Complete" by Steve McConnell - this is the best book on coding I have ever read.

Steve H.
rwyoung



Joined: 12 Nov 2003
Posts: 563
Location: Lawrence, KS USA

View user's profile Send private message Send e-mail

PostPosted: Thu Apr 22, 2004 8:15 am     Reply with quote

Comment as much as the code needs and no more. Twisted Evil

In my opinion, comments have two lifetimes. Thier first lifetime is during program creation and debug when the likelyhood of the same person who wrote the code and comments will also be reading the comments. Their second lifetime is maintenence when it is more likely that somebody other than the original programmer(s) will be reading the comments.

When I comment my code I try to keep in mind that somebody else may have to work on the code after I've been killed in a freak espresso machine accident.

As to style, "Code Complete" is a good book to read but I always get a chuckle out of the fact that it is from Microsoft Press... Also the "Pragmatic Programmer" is good. There are several other "style" books around. And I found a nice (although probably a little too anal for hobby use) code formatting guide on Jack Ganssle's web site. That one you will have to Google for but I think the web site is www.ganssle.com

One last thing about comments, I try to use complete sentances with punctuation when describing code in the header block I put before each new subroutine. But inside a function I'm generally a bit more terse when commenting on the same line as code.
_________________
Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month!
dest



Joined: 17 Apr 2004
Posts: 2

View user's profile Send private message

PostPosted: Thu Apr 22, 2004 9:32 am     Reply with quote

Try to make your commenting efficient and to the point (don't comment for the sake of commenting). I don't like the huge comment block in ex_mouse.c because of all the time it would take to get the slashes to line up correctly. I see a lot of people try to get their comments to look good. Work on content rather than formatting.

I also recommend "Code Complete" it has some of most well rounded discussion on commenting that I have seen.

Also, always document your functions. Either in the declaration or definitions. Document the parameters and return value along with what are acceptable values for the parameters. It is also good to include the original author and creation date at the top of the file.

By using good variable and function naming (Code Complete talks about this) you can reduce the amount of documentation you will need.

I believe good, clear coding is more important than good commenting. However for embedded systems, sometimes you have to pull tricks to make your code more efficient. Anything that someone would have to think about to know what you are doing should be commented.

I was always taught to comment while you code rather than afterwards. I actually don't recommend this. Often, when first creating a file, you will have to make substantial changes and work out how it will work (even if you do good design). This will make it hard to maintain comments and code (and often make your comments out-of-date). I comment after I am fairly confident that the code won't need many modifications. If you don't put it off or rush it, your commenting end up being better.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Apr 22, 2004 11:16 am     Reply with quote

If you look at what Code Complete recommends and what MS
actually puts in for comments, there is quite a difference. LOL.
http://www.kuro5hin.org/story/2004/2/15/71552/7795
They say the code is dated in July, so maybe these guys were
mostly summer hires ?
Steve H
Guest







PostPosted: Tue Apr 27, 2004 7:39 pm     Reply with quote

PCM's remark is funny - there is always a THEORY and an actual PRACTICE! I think this is also where the: "Managing software people is like herding cat's" theory comes in!

RW's remarks were directly at the point. I hope no one took it that I suggested that lot's of meaningless comments are worthwhile. I try to document code so I can figure out what I was doing 6 months from now - or that someone else stands a chance of figuring out my code. In my case that takes a lot of comments!

There is nothing worse than looking at a three page function where everything is a pointer to a pointer to a pointer to a table of pointers with no comments! That's why Steve M's book is such a good read - he has "Coding Horror's" sprinkled around....

;-)

Steve H.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group