Motherboard Forums


Reply
Thread Tools Display Modes

What coding standard are you using for C?

 
 





















Geek
Guest
Posts: n/a

 
      11-04-2009, 10:10 PM


What coding standard are you using for C (if any)?

Like naming variables, functions, constants, globals, locals, types etc.?

for example:

MyFunction();
myFunction();
My_Function()
my_function()

UART_read()
UartRead()
UARTRead();


 
Reply With Quote
 
D Yuniskis
Guest
Posts: n/a

 
      11-04-2009, 11:10 PM
Geek wrote:
> What coding standard are you using for C (if any)?
>
> Like naming variables, functions, constants, globals, locals, types etc.?
> for example:
>
> MyFunction();
> myFunction();
> My_Function()
> my_function()
>
> UART_read()
> UartRead()
> UARTRead();


You will get as many *different* answers as respondents! :>
Find one that *you* are comfortable with and be consistent
within that.

Things you might consider:
MaxValue vs. ValueMax -- the latter sorts better with
names like ValueMin (contrast this with MinValue)
Adding underbars improves readability but lengthens
identifiers.
If you adopt a naming convention that embeds the type of
an object in the name of the object (e.g., "Hungarian"
notation), then you have to change the name of the object
if you end up changing its type.
Globals are evil. :> You might also find limits on globals
are different (number of characters, etc.)
Decide if you want to be verbose or cryptic with your names.
E.g., char *p vs. char *ptr vs. char *pointer...
 
Reply With Quote
 
larwe
Guest
Posts: n/a

 
      11-04-2009, 11:28 PM
On Nov 4, 6:10*pm, D Yuniskis <not.going.to...@seen.com> wrote:

> You will get as many *different* answers as respondents! *:>


Why so few? Anyone here who works on code of different vintages or for
different platforms is probably working with at least three or four
different styles/standards at once.

My own style (where I am free to choose it) is heavily influenced by
having written code for OS/2 - I prefix all functions and variables
with an abbreviation indicating the module in which they reside, for
example...
 
Reply With Quote
 
Rube Bumpkin
Guest
Posts: n/a

 
      11-05-2009, 01:35 AM
Geek wrote:
> What coding standard are you using for C (if any)?
>
> Like naming variables, functions, constants, globals, locals, types etc.?
>
> for example:
>
> MyFunction();
> myFunction();
> My_Function()
> my_function()
>
> UART_read()
> UartRead()
> UARTRead();
>
>


Jack Ganssle has written quite a bit on this in Embedded Systems Design
(Is that their current name?) magazine, his weekly newsletter, and has
info on his website (www.ganssle.com). For example
http://www.ganssle.com/fsm.pdf has a whole lot of ideas about this.

RB
 
Reply With Quote
 
Leo Havmøller
Guest
Posts: n/a

 
      11-05-2009, 05:05 AM
"Geek" <> wrote in message
news:4af1fbe1$0$275$...
> What coding standard are you using for C (if any)?
>
> Like naming variables, functions, constants, globals, locals, types etc.?


Here's a few:
http://www.sourceformat.com/coding-standard.htm

Leo Havmøller.

 
Reply With Quote
 
42Bastian Schick
Guest
Posts: n/a

 
      11-05-2009, 06:20 AM
On Wed, 4 Nov 2009 23:10:37 +0100, "Geek" <> wrote:

>What coding standard are you using for C (if any)?
>
>Like naming variables, functions, constants, globals, locals, types etc.?


What ever you choose, keep it throughout the project.
>
>for example:
>
>MyFunction();
>myFunction();


I myself prefer:
Camel-style starting with lower letter, no type-information in symbols
(i.e. no int *ip_a.
Types appended with _t, structs with _s and classes with _c. Classes
start with an upper letter.
Pre-processor macros all upper-case and the values always surrounded
by brackets.
E.g.:
#define VALUE (1)
(I just debugged customer code, who did not do this:
#define VALUE 1<<5
And in the code:
mask &= ~VALUE;
Guess what happened ? :-)

Global functions get a module-prefix separated by "_"
Opening braces are in the line of if/while/for/do/switch, indention 2
spaces.
Code should be readable, so no
if(i==1||i==4)
but
if ( (i == 1) || (i == 4) )
Use spaces, good code should not look like prose rather like poetry.

But of course, writing code for customers forces other styles.
--
42Bastian
Do not email to , it's a spam-only account :-)
Use <same-name>@monlynx.de instead !
 
Reply With Quote
 
ArarghMail911NOSPAM@NOT.AT.Arargh.com
Guest
Posts: n/a

 
      11-05-2009, 07:40 AM
On Thu, 05 Nov 2009 06:20:01 GMT, (42Bastian
Schick) wrote:

<snip>
>Pre-processor macros all upper-case and the values always surrounded
>by brackets.
>E.g.:
>#define VALUE (1)

I always thought brackets were [] not () ?

<snip>
--
ArarghMail911 at [drop the 'http://www.' from ->] http://www.arargh.com
BCET Basic Compiler Page: http://www.arargh.com/basic/index.html

To reply by email, remove the extra stuff from the reply address.
 
Reply With Quote
 
Boudewijn Dijkstra
Guest
Posts: n/a

 
      11-05-2009, 08:51 AM
Op Thu, 05 Nov 2009 08:40:44 +0100 schreef
<>:
> On Thu, 05 Nov 2009 06:20:01 GMT, (42Bastian
> Schick) wrote:
> <snip>
>> Pre-processor macros all upper-case and the values always surrounded
>> by brackets.
>> E.g.:
>> #define VALUE (1)

> I always thought brackets were [] not () ?


Yes and no. There are four main types of brackets:
- round brackets, open brackets or parentheses: ( )
- square brackets, closed brackets or box brackets: [ ]
- curly brackets, squiggly brackets, swirly brackets, braces: { }
- angle brackets, diamond brackets, cone brackets, wickets, chevrons: < >
or ⟨ ⟩

http://en.wikipedia.org/wiki/Bracket


--
Gemaakt met Opera's revolutionaire e-mailprogramma:
http://www.opera.com/mail/
(remove the obvious prefix to reply by mail)
 
Reply With Quote
 
Paul Carpenter
Guest
Posts: n/a

 
      11-05-2009, 08:57 AM
In article <>,
says...
> On Wed, 4 Nov 2009 23:10:37 +0100, "Geek" <> wrote:
>
> >What coding standard are you using for C (if any)?
> >
> >Like naming variables, functions, constants, globals, locals, types etc.?


Choice of fonts printed can have a bearing avoid the ONE and L
confusions, let alone the zero and letter o confusions. There
are others.

> What ever you choose, keep it throughout the project.
> >
> >for example:
> >
> >MyFunction();
> >myFunction();


My personal preference on that would be how the parntheses are done
always put a space between, even for empty list. Avoids problems
with some fonts the parentheses being confused as a letter eg 0.

> I myself prefer:

.....
> Opening braces are in the line of if/while/for/do/switch, indention 2
> spaces.


More importantly indent with spces the block of code by the
same amount. Tabs can be different on systems/IDE/editors

> Code should be readable, so no
> if(i==1||i==4)
> but
> if ( (i == 1) || (i == 4) )


My personal method is ensure the opening parentheses for functions,
language construct ( if, while.. ) are by the name. All others spaced
out, so in a complex if( .... ), it becomes easier to spot at a glance
what is a function and what is a variable.

> Use spaces, good code should not look like prose rather like poetry.


Despite C, being free format, avoid too much free format, remembering
that at some time it may well be printed, 300 character lines wrap
very interestingly, if they wrap. Choosing indentation matching
can make following the code easier, even if you have managed
to get the function to fit on one page.

Look up 'obfuscated C' for examples of how not to do things, one
year they had code that compiled correctly but printed out you could
see the code made a picture.

> But of course, writing code for customers forces other styles.
>


--
Paul Carpenter |
<http://www.pcserviceselectronics.co.uk/> PC Services
<http://www.pcserviceselectronics.co.uk/fonts/> Timing Diagram Font
<http://www.gnuh8.org.uk/> GNU H8 - compiler & Renesas H8/H8S/H8 Tiny
<http://www.badweb.org.uk/> For those web sites you hate
 
Reply With Quote
 
ArarghMail911NOSPAM@NOT.AT.Arargh.com
Guest
Posts: n/a

 
      11-05-2009, 09:47 AM
On Thu, 05 Nov 2009 09:51:31 +0100, "Boudewijn Dijkstra"
<> wrote:

<snip>
>> I always thought brackets were [] not () ?

>
>Yes and no. There are four main types of brackets:
>- round brackets, open brackets or parentheses: ( )
>- square brackets, closed brackets or box brackets: [ ]
>- curly brackets, squiggly brackets, swirly brackets, braces: { }
>- angle brackets, diamond brackets, cone brackets, wickets, chevrons: < >
>or ? ?

Confusing. I call them (in the order above) parentheses, brackets,
braces, and angle brackets OR less & greater.

And there is always « » (174 & 175) which I don't call much of
anything. :-)
--
ArarghMail911 at [drop the 'http://www.' from ->] http://www.arargh.com
BCET Basic Compiler Page: http://www.arargh.com/basic/index.html

To reply by email, remove the extra stuff from the reply address.
 
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Standard for hard disk for Inspiron 8000 Ben Myers Dell 0 08-02-2009 11:37 PM
Re: Is there any way to fix Software Update in Standard user accounts? Ian Gregory Apple 17 04-26-2008 04:00 AM
Re: Is there any way to fix Software Update in Standard user accounts? Eric Lindsay Apple 0 04-08-2008 05:07 AM
NIST stages competition to improve cryptographic standard Frederick Lorca Dell 0 01-27-2007 06:44 PM
Re: Is "Tahoma" a MAC standard font? Glo8al Apple 0 07-22-2003 08:42 AM


All times are GMT. The time now is 07:18 AM.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43