Style guides for C and/or C++

V

Vicent Giner-Bosch

I've been searching at this forum and at the Internet for style
guides for C and/or C++, and I've found these ones:

http://www.psgd.org/paul/docs/cstyle/cstyle.htm
http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml

Also, I've read some threads about C style matters. I think there is
really a lot of stuff!

I just would like to ask you for a quick answer to this question:
which style guide or style rules do you recommend me to use when
writing C/C++ code? It would be great if many of you would answer, so
we all could have here a thread with a good summary about this
subject.

Thank you for the feed-back!! :)


PS: I've also posted this theme at comp.lang.c, so I apologize for
cross-posting.
 
A

Alf P. Steinbach

* Vicent Giner-Bosch:
I've been searching at this forum and at the Internet for style
guides for C and/or C++, and I've found these ones:

http://www.psgd.org/paul/docs/cstyle/cstyle.htm
http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml

Also, I've read some threads about C style matters. I think there is
really a lot of stuff!

I just would like to ask you for a quick answer to this question:
which style guide or style rules do you recommend me to use when
writing C/C++ code?

Different ones. C is not C++. C++ is not C.

For C++ consider the Alexandrescu and Sutter guide (it's a book).

It would be great if many of you would answer, so
we all could have here a thread with a good summary about this
subject.

Thank you for the feed-back!! :)


PS: I've also posted this theme at comp.lang.c, so I apologize for
cross-posting.

You didn't cross-post. You may perhaps have multi-posted.


Cheers & hth.,

- Alf
 
T

Timothy Madden

Vicent Giner-Bosch wrote:
[...]
I just would like to ask you for a quick answer to this question:
which style guide or style rules do you recommend me to use when
writing C/C++ code? It would be great if many of you would answer, so
we all could have here a thread with a good summary about this
subject.

I write C++ and I use the following indentation which I care about:

template< typename charT, typename traits=char_traits<charT> >
class charConvertor
{
public:
static
std::basic_string<charT> strMessage;

template<typename ExternalCharType>
static
inline
void Transcode(ExternalCharType const *szMessage)
{
strMessage = szMessage;
}
}
convertor;

int main()
{
int iResult = 0;

if
(
iResult <= 10
&&
iResult >= -2
&&
iResult == 0
)
{
cout << "Right result";
cout << endl;

return iResult;
}
else
::CreateWindow
(
szClassName,
szTitle,
dwStyle,
xPos,
yPos,
cxWidth,
cxHeight,
hwndParent
);

return -1;
}


It looks better with syntax highlighting though ... :)

My tab characters are 8 columns wide, although some editors made it a
habit to use tab stops at 4 columns...

I really do not understand the style some people use for blocks:

int ProcessIndex(int iIndex) {
return iIndex * 3 / 4;
}


It is so ugly and un-intuitive ...

Timothy Madden
 
R

Richard McBeef

Vicent said:
I've been searching at this forum and at the Internet for style
guides for C and/or C++, and I've found these ones:

http://www.psgd.org/paul/docs/cstyle/cstyle.htm
http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml

Also, I've read some threads about C style matters. I think there is
really a lot of stuff!

I just would like to ask you for a quick answer to this question:
which style guide or style rules do you recommend me to use when
writing C/C++ code?
Here is my style "rule" :
In netbeans right mouse click in an open code
window. Select "Format".
Hope That Helps :)
 
M

Marcelo Pinto

I just would like to ask you for a quick answer to this question:
which style guide or style rules do you recommend me to use when
writing C/C++ code? It would be great if many of you would answer, so
we all could have here a thread with a good summary about this
subject.
[snip]

My 2 cents:

- Don't sweat on small stuff (chapter 1 of C++ Coding Standards by
Sutter & Alexandrescu)
- Don't use Hungarian notation
- Use a tool to enforce formatting (such as AStyle or ident)
- Be consistent

HTH,

Marcelo Pinto
 
R

Richard

[Please do not mail me a copy of your followup]

"Alf P. Steinbach" <[email protected]> spake the secret code
For C++ consider the Alexandrescu and Sutter guide (it's a book).

I'll second that.

There's another earlier one by Plum & Saks that I have found useful,
but its out of print and some of its advice is dated. Alexandrescu
and Sutter is a good choice.
 
S

Sousuke

I've been searching at this forum and at the Internet for style
guides  for C and/or C++, and I've found these ones:

http://www.psgd.org/paul/docs/cstyl...leguide.googlecode.com/svn/trunk/cppguide.xml

Also, I've read some threads about C style matters. I think there is
really a lot of stuff!

I just would like to ask you for a quick answer to this question:
which style guide or style rules do you recommend me to use when
writing C/C++ code? It would be great if many of you would answer, so
we all could have here a thread with a good summary about this
subject.

It's not a good idea to take advice from both C style guides *and* C++
style guides, because they will contradict each other on some matters.
And the reason is that some practices that might be considered good
style in C are actually bad style in C++, because in C++ there are
better alternatives.

To take the two style guides you suggested as an example, here is one
such contradiction: on defining names for constants,
http://www.psgd.org/paul/docs/cstyle/cstyle12.htm says
"The #define feature of the C preprocessor should be used to give
constants meaningful names."
while http://google-styleguide.googlecode...owone=Preprocessor_Macros#Preprocessor_Macros
says
"Instead of using a macro to store a constant, use a const variable."

There are similar arguments for parameterized macros vs. inline
functions, pointers vs. smart pointers, raw arrays vs. array classes
(like std::vector), etc.
 
V

Vladimir Jovic

Timothy said:
int main()
{
int iResult = 0;

if
(
iResult <= 10
&&
iResult >= -2
&&
iResult == 0
)
{
cout << "Right result";
cout << endl;

return iResult;
}
else
::CreateWindow
(
szClassName,
szTitle,
dwStyle,
xPos,
yPos,
cxWidth,
cxHeight,
hwndParent
);

return -1;
}

I see you haven't been bit by this bug-type yet.

I would write this code snippet like this:


int main()
{
int iResult = 0;

if
(
iResult <= 10
&&
iResult >= -2
&&
iResult == 0
)
{
cout << "Right result";
cout << endl;

return iResult;
}
else
{
::CreateWindow
(
szClassName,
szTitle,
dwStyle,
xPos,
yPos,
cxWidth,
cxHeight,
hwndParent
);
}

return -1;
}
 
J

Jorgen Grahn

I've been searching at this forum and at the Internet for style
guides for C and/or C++, and I've found these ones:

http://www.psgd.org/paul/docs/cstyle/cstyle.htm
http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml

Also, I've read some threads about C style matters. I think there is
really a lot of stuff!

I just would like to ask you for a quick answer to this question:
which style guide or style rules do you recommend me to use when
writing C/C++ code? It would be great if many of you would answer, so
we all could have here a thread with a good summary about this
subject.

You'll get no consensus or summary, that's for sure. Too many
different styles and opinions.
PS: I've also posted this theme at comp.lang.c, so I apologize for
cross-posting.

Thanks for mentioning it. Note that C and C++ are different
languages, and you cannot just mindlessly use the same style in both.

/Jorgen
 
J

Jorgen Grahn

Vicent Giner-Bosch wrote:
[...]
I just would like to ask you for a quick answer to this question:
which style guide or style rules do you recommend me to use when
writing C/C++ code? It would be great if many of you would answer, so
we all could have here a thread with a good summary about this
subject.

I write C++ and I use the following indentation which I care about:

template< typename charT, typename traits=char_traits<charT> >
class charConvertor
{
public:
static
std::basic_string<charT> strMessage;

template<typename ExternalCharType>
static
inline
void Transcode(ExternalCharType const *szMessage)
{
strMessage = szMessage;
}
}
convertor;

int main()
{
int iResult = 0;

if
(
iResult <= 10
&&
iResult >= -2
&&
iResult == 0
)
{
cout << "Right result";
cout << endl;

return iResult;
}
else
::CreateWindow
(
szClassName,
szTitle,
dwStyle,
xPos,
yPos,
cxWidth,
cxHeight,
hwndParent
);

return -1;
}


It looks better with syntax highlighting though ... :)

My tab characters are 8 columns wide, although some editors made it a
habit to use tab stops at 4 columns...

I really do not understand the style some people use for blocks:

int ProcessIndex(int iIndex) {
return iIndex * 3 / 4;
}


It is so ugly and un-intuitive ...

Since you brought it up, I find *your* style extremely ugly and hard
to read. I've seen nothing like it before.

The style you say is ugly and used by "some people" is very common,
and used by people like Stroustrup. (Maybe not for function
definitions like in your example, but for most other blocks.) If you
have problems reading it, learn to. Otherwise you'll have problems
working with just about any other C++ programmer.

For reference, I would have written your code like this:

int main()
{
const int i = 0;

if(i<=10 && i>=-2 && i==0) {
std::cout << "Right result\n";
return 0;
}

::CreateWindow(name, title, style,
x, y,
width, height,
parent);

return 1;
}

(I don't believe in returning -1 from main().)

/Jorgen
 
R

Robert Fendt

You'll get no consensus or summary, that's for sure. Too many
different styles and opinions.

Maybe one should quote from Marshall Cline's (excellent) C++ FAQ here:

http://www.parashift.com/c++-faq-lite/coding-standards.html#faq-27.1

"27.1 What are some good C++ coding standard?

[...] Obviously anyone who asks this question wants to be trained so they don't run off on their own ignorance, but nonetheless posting a question such as this one to comp.lang.c++ tends to generate more heat than light. [...]"

Which is very much to the point, actually. The question of which style exactly is 'superior' is moot. Just choose one and stick with it. If you are working with other people, just choose one (together!) and then stick with it. When reading code written by other people, I (like most even semi-experienced C++ programmers) usually do not really care about subtle differences in indentation and such, as long as (1) there *is* some level of indentation to make block levels clear, (2) it is readable and (3) *consistent*.

Consistency is key. To that end, source code formatters like astyle and the like can help you. But of course they do not support every formatting style on the planet, just what their authors deemed popular and/or sensible. So using e.g. astyle limits your choices, which can also mean that your code formatting style implicitely gets less 'individual' thus making it easier for others to read. Which can be a positive effect as well.

Regards,
Robert
 
K

Krice

- Don't use Hungarian notation

HN could be good sometimes. I'm using HN in enums and
sometimes in classes, like G_Screen (G=graphics) to make it
easier to find/list them in class browser.
 
J

Jonathan Lee

HN could be good sometimes. I'm using HN in enums and
sometimes in classes, like G_Screen (G=graphics) to make it
easier to find/list them in class browser.

Something I prefer is to put enums in a namespace or
class. So "G_Screen" becomes "Graphics::Screen".

--Jonathan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top