--0__=80256A2B00381E078f9e8a93df938690918c80256A2B00381E07 Content-type: text/plain; charset=us-ascii Hi, From working in a team, I have found that coding styles is almost a religous subject and caused frequent "discussions" ! "Code Complete", from the Microsoft Press, has some good pointers on coding styles. At the end of the day, it's down to personal taste and it should be what you feel comfortable with although things like correct indentation, "intention" commenting and consistent use of functions do improve 'understandability.' For example, Code Complete suggests that you do not do the following: return_value = SomeFunction (in1, in2, out); (where 'out' is passed as a pointer) as there are two exit points; for consistency, it is suggested to do : SomeFunction(in1, in2, out1, return_value) if(return_value == xxxx)........ But this is down to the person. Like someone else has said, I'm not sure why you are returning a value for the start condition unless you are checking to see if the bus is still busy prior to wading in. Some of my I2C code goes along the lines of : uint8_t I2C_ReadTemp(void) { uint8_t Temp; // Drive the I2C bus i2c_start(); // Select device with address offset i2c_write(TMP_BASEADDR | TMP_ADDR_A0); // Select read temperature command i2c_write(TMP_R_READT); i2c_start(); // Tell device I want to read the temperature i2c_write(TMP_BASEADDR | TMP_ADDR_A0 | TMP_READ); // Obtain value (unsigned) - NAK as only one byte wanted Temp=i2c_read(NAK); i2c_stop(); return(Temp); } Please critcise/destroy as necessary(!) As to if elses/switches, it is always worth checking the list file to see what the compiler has been up to in order to determine efficiency. My tuppence worth, anyway Dan (Embedded Michael Rigby-Jones @MITVMA.MIT.EDU> image moved 11/04/2001 09:41 to file: pic15718.pcx) Please respond to pic microcontroller discussion list Sent by: pic microcontroller discussion list To: PICLIST@MITVMA.MIT.EDU cc: Subject: [OT]: Coding style Security Level:? Internal This is a plea for some advice on coding style from any of the experts on the list. I have written some I2C drivers for use under Windows for a project, and whilst they are perfectly functional I feel that my coding style could definately be improved. Now, a function should preferably have one entry and one exit. However my code looks something like the function below. Here I am calling several functions that return a boolean to indicate success or failure, and I want to exit the calling function if a failure is returned. bool _stdcall X9241Write(unsigned char address, unsigned char instruction) { /* Function: X9241Write Arguments: address = slave address of device, instruction = instruction byte to send to pot Return: True if success, false if fail. Description: Generic write command for instructions that required no additional data. */ unsigned char fullAddr; fullAddr = (unsigned char)(X9241_ADDR | (address & 0x0F)); if(!I2CStart()) return false; // send slave address if(!I2CTransmit(fullAddr)) return false; if(!I2CGetAck()) return false; // send the MSB if(!I2CTransmit(instruction)) return false; if(!I2CGetAck()) return false; if(!I2CStop()) return false; return true; } Now I could nest all the if() statements, but to my mind this would become far more confusing than the above, especially with some of the longer functions. Or perhaps set a local variable and check it before calling each sub function i.e. bool success; success = I2CStart(); if(success) success = I2CTransmit(fullAddr); if(success) success = I2CGetAck(); etc, etc... This would work and I could use one return at the end, but to my mind a failure at the top of the function causes unnecessary overhead in in checking the success variable multiple times to reach the end of the function. I could use a goto and a label at the end of the function, but that seems infinitely worse than multiple returns. Any other suggestions? Regards Mike -- http://www.piclist.com hint: To leave the PICList mailto:piclist-unsubscribe-request@mitvma.mit.edu --0__=80256A2B00381E078f9e8a93df938690918c80256A2B00381E07 Content-type: application/octet-stream; name="pic15718.pcx" Content-Disposition: attachment; filename="pic15718.pcx" Content-transfer-encoding: base64 CgUBCAAAAABBADEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAABQgABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAA= --0__=80256A2B00381E078f9e8a93df938690918c80256A2B00381E07-- -- http://www.piclist.com hint: To leave the PICList mailto:piclist-unsubscribe-request@mitvma.mit.edu