![]() |
![]() |
![]() |
|
|
|||||||
| Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc. |
|
|
Thread Tools | Display Modes |
|
|
(permalink) |
|
People keep posting code that has lots of blank lines. Why? It just causes me to scroll up/down in order to read it. I can understand one blank line separating routines for readabilities sake but every other line blank is just annoying.
Is it just me? Mike. Edit, this is in no way aimed at elec123. Sorry to use your code as an example. P.S. Here is what I mean. Posted earlier, Code:
void main(void);
void Initialize_ADC(void);
void main()
{
unsigned char dig_input;
unsigned char frank;
Initialize_ADC();
while(1)
{
//Read_ADC
ADGO = 1; //captures analogue voltage on pin.
frank = 0x10;
while(frank != 0x00)
{frank -= 1;}
while (ADGO)
{}
frank = 0xFF;
while(frank != 0x00)
{frank -= 1;}
dig_input = ADRESH; //defines the digital input.
if (dig_input<128)
{
CCPR1L = 0x00; //sets pwm to have duty cycle of zero.
CCP1CON = 0x00;
}
else
{
dig_input -= 128; //dig_input = dig_input-128
dig_input += dig_input; //multiplies dig_input by 2
CCPR1L = look1[dig_input];
CCP1CON = look2[dig_input];
}
}
};
void Initialize_ADC()
{
TRISA = 0x01;
TRISC = 0x00;
PORTC = 0x00;
PR2 = 0x18;
T2CON = 0x05;
ADCON0 = 0x80; //clear ADCON0 A2D starts sampling again.
//Set ADCON1
ADCON1 = 0x00; // clear ADCON1 register.//
ADCON1 = 0x02; // set PCFG1 bit i.e AN0 is analogue input.
ADON = 1;
};
Code:
void main(){
unsigned char dig_input;
unsigned int i;
Initialize_ADC();
While(1){
//Read_ADC
ADGO = 1; //captures analogue voltage on pin.
while (ADGO);
dig_input = ADRESH; //defines the digital input.
if (dig_input<128){ //if solar panel (Vin) to boost converter is less than 10V don't boost.
CCPR1L = 0x00; //sets pwm to have duty cycle of zero.
CCP1CON = 0xc0;
}
else{
dig_input -= 128; //dig_input = dig_input-128
dig_input += dig_input; //multiplies dig_input by 2
CCPR1L = look1[dig_input];
CCP1CON = look2[dig_input];
}
for(i=0;i<1000;i++);
}
}
Last edited by Pommie; 4th February 2008 at 03:32 PM. |
|
|
|
|
|
|
(permalink) | |
|
Quote:
__________________
========================= Futz's Microcontrollers & Robotics ========================= |
||
|
|
|
|
|
(permalink) |
|
I agree, your version is much easier to read.
__________________
I also post at the following sites: http://www.stop-microsoft.org http://www.heated-debates.com Screen name: Aloone_Jonez |
|
|
|
|
|
|
(permalink) |
|
Somewhere between the two extremes is good. The first code example is abysmal and if I saw that going into one of my codebases I'd have a little chat with the coder about the value of indenting (!) and overuse of vertical whitespace.
The second code example however offers no visual cues beyond the indenting as to what's going on and needs work on the horizontal whitespacing, but is still more readable than the first example (IMHO good indenting and consistent bracing trumps vertical whitespacing). For a little chunk of code like the example it's OK but I'd hate to manage a quarter-million line application written like that. That said, discussions over code formatting often wind up in the same hundred-post arguments that vi vs. emacs or mac vs. windows do. Torben [Edit: I just saw the post with the original code. I take back my rating of "abysmal" on the original code since it wasn't posted in a code block, which screwed up the formatting. I do agree it's a bit heavy on the vertical whitespace though.] Last edited by Torben; 4th February 2008 at 09:42 PM. |
|
|
|
|
|
|
(permalink) | |
|
Quote:
Brian |
||
|
|
|
|
|
(permalink) |
|
My version;
Code:
void main()
{
unsigned char dig_input;
unsigned int i;
Initialize_ADC();
While(1)
{
//Read_ADC
ADGO = 1; //captures analogue voltage on pin.
while (ADGO);
dig_input = ADRESH; //defines the digital input.
if (dig_input<128){ //if solar panel (Vin) to boost converter is less than 10V don't boost.
CCPR1L = 0x00; //sets pwm to have duty cycle of zero.
CCP1CON = 0xc0;
}
else
{
dig_input -= 128; //dig_input = dig_input-128
dig_input += dig_input; //multiplies dig_input by 2
CCPR1L = look1[dig_input];
CCP1CON = look2[dig_input];
}
for(i=0;i<1000;i++);
}
|
|
|
|
|
|
|
(permalink) |
|
Style is (or should be) about readability and maintainability.
As Brian said there are books on the subject of style. Even more important are the books on writting "correct" code. Back when we used ASCII terminals we needed to crunch the code a bit but that caused problems. Many man years have been spent tracking down missing "{" or "}". For that reason it makes sense to put the opening and closing codeblock char at the same indent level and as the first character on a line. Another gotcha is the if without a code block. Code:
if (a>b)
c = d;
c++;
Code:
if (a>b)
c = d;
a = p/2;
c++;
Code:
if (a>b)
{
c = d;
a = p/2;
}
c++;
Code:
if (a>b)
{
c = d;
}
c++;
It is a pain but in the long run it pays.
__________________
search engine for electronic partsJunebug USB PIC programmer kit., USB Bit Wacker, Homepage The 15 Minute Printed Circuit Board! (+drill time) |
|
|
|
|
|
|
(permalink) |
|
3v0,
Surely, this is not a mistake that an experienced programmer would make. However, if someone wrote, Code:
if (a>b);
c = d;
c++;
Code:
if (a>b);
{
c = d;
}
c++;
Mike. |
|
|
|
|
|
|
(permalink) | |
|
Quote:
Torben |
||
|
|
|
|
|
(permalink) | |
|
Quote:
Code:
void main()
{
unsigned char dig_input;
unsigned int i;
Initialize_ADC();
while (1)
{
// Read_ADC
ADGO = 1; // captures analogue voltage on pin.
while (ADGO) {}
dig_input = ADRESH; // defines the digital input.
// if solar panel (Vin) to boost converter is less than 10V don't boost.
if (dig_input < 128)
{
CCPR1L = 0x00; // sets pwm to have duty cycle of zero.
CCP1CON = 0xc0;
}
else
{
dig_input -= 128; // dig_input = dig_input-128
dig_input += dig_input; // multiplies dig_input by 2
CCPR1L = look1[dig_input];
CCP1CON = look2[dig_input];
}
for (i = 0; i < 1000; i++) {}
}
}
Torben |
||
|
|
|
|
|
(permalink) |
|
I don't have a problem with any of the above examples. Looking at them, it appears my use of an open brace on the end of the line is frowned upon.
I.E. Code:
if(a==b){
somert;
}
(my apologies to the original poster.) Code:
#pragma romdata
//Example heirarchy items.
// **********************************************************************************************
// The following arrays contain menu items for each level of menu heirarchy.
MenuItem menu1Items[] =
{
// TopLevel , Name , subMenu, handler
1 ,"Menu1-1" , 0 , handler,
1 ,"Menu1-2" , 0 , 0
};
//This represents the settings sub menu.
MenuItem menu2Items[] =
{
// TopLevel , Name , subMenu, handler
1 ,"Menu2-1" , 0 , 0,
1 ,"Menu2-2" , 0 , 0,
1 ,"Menu2-3" , 0 , 0
};
//base items. also: menu initialises by cha startup item to baseItems[0]
MenuItem menu0Items[] =
{
// TopLevel , Name , subMenu, handler
0 , "Menu1" , &menuLevels[1] , 0,
0 , "Menu2" , &menuLevels[2] , 0,
1 , "Menu3" , 0 , 0
};
//*********************************************************************************************
//End of item arrays
//The following array is required by menu system.
//It contains MenuLevel items, which describe a level that contains menu items.
//First item should be the base menu.
MenuLevel menuLevels[] = { // start index , array size , array
0 , sizeof(menu0Items) / sizeof (MenuItem), menu0Items,
0 , sizeof(menu1Items) / sizeof (MenuItem), menu1Items,
0 , sizeof(menu2Items) / sizeof (MenuItem), menu2Items,
};
|
|
|
|
|
|
|
(permalink) |
|
|
|
|
|
|
|
|
(permalink) | ||
|
Quote:
Quote:
Torben |
|||
|
|
|
|
|
(permalink) |
|
The great thing about this topic is that *everyone* has an opinion and some of us are so indecisive we have two :-)
Here's my take: Code:
void main()
{
unsigned char dig_input;
unsigned int i;
Initialize_ADC();
while(1)
{
//Read_ADC
ADGO = 1; //captures analogue voltage on pin.
while (ADGO);
dig_input = ADRESH; //defines the digital input.
// Boost when (Vin) to boost converter is less than 10V (128)
if (dig_input<128)
{
CCPR1L = 0x00; //sets pwm to have duty cycle of zero.
CCP1CON = 0xc0;
}
else
{
dig_input -= 128; // dig_input = dig_input-128
dig_input += dig_input; // multiplies dig_input by 2
CCPR1L = look1[dig_input];
CCP1CON = look2[dig_input];
}
for(i=0;i<1000;i++);
}
} // main()
BTW: I improved the comment on the first if(), but the comments on the manipulations of dig_input leave a lot to be desired... comments ought to refer to the logic of the solution, not merely be a reiteration in English (or language of choice) of the code!!! I always put a comment on the closing brace of a function as above so I can navigate my code a little more easily. P. |
|
|
|
|
|
|
(permalink) | |||
|
Quote:
Quote:
Quote:
Regexps (and perl Torben |
||||
|
|
|
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|
|
|
||||
| Thread | Thread Starter | Forum | Replies | Latest |
| Magnets and Electromagnets | ElectroMaster | Electronic Theory | 11 | 27th November 2007 02:31 PM |
| PIC code problem | Gaston | Micro Controllers | 9 | 7th March 2007 02:23 AM |
| Charge batteries using tel lines | electroniks | Electronic Projects Design/Ideas/Reviews | 5 | 16th April 2006 06:54 PM |
| running sort of output lines.................... | anand_jain | Micro Controllers | 1 | 11th July 2004 02:31 AM |
| TV- Lines in the picture | Johnson777717 | General Electronics Chat | 2 | 1st April 2004 12:10 AM |