Discussion:
[9fans] c++
(too old to reply)
Steve Simon
2012-11-19 09:59:27 UTC
Permalink
I need to learn c++ for work - people have strong opinions on
languages I know, and not everyone likes c++ but its a requirment for me.

I really want to develop a good sence of c++ style, I learnt C at the feet of
K&R and then the plan9 sourcecode so I learnt how to write clean elegant code
(I think :-). The problem I am finding is there are many c++ styles and I have
yet to find a clean and elegant one.

anyone sugest a project that I could look at that contains well written code?
failing that is there a book that teaches good style?

I am refering to things like adding a leading m_ to class member variables
(which looks horrid to me but I am willing to learn), and smart locks (mutexs
which unlock on destruct).

Thanks for any suggestions.

-Steve
Mathieu Lonjaret
2012-11-19 10:10:57 UTC
Permalink
Hello,

Well, I haven't looked at the code, but Russ wrote Re2, so one could
hope it's written in a style that's more familiar to you than other
projects: http://code.google.com/p/re2/

Mathieu
Post by Steve Simon
I need to learn c++ for work - people have strong opinions on
languages I know, and not everyone likes c++ but its a requirment for me.
I really want to develop a good sence of c++ style, I learnt C at the feet of
K&R and then the plan9 sourcecode so I learnt how to write clean elegant code
(I think :-). The problem I am finding is there are many c++ styles and I have
yet to find a clean and elegant one.
anyone sugest a project that I could look at that contains well written code?
failing that is there a book that teaches good style?
I am refering to things like adding a leading m_ to class member variables
(which looks horrid to me but I am willing to learn), and smart locks (mutexs
which unlock on destruct).
Thanks for any suggestions.
-Steve
Aram Hăvărneanu
2012-11-19 11:31:34 UTC
Permalink
RE2 is very good code, but it doesn't contain any "modern" C++
features, and it's not written in "standard" style. That is part of
what makes it good, I guess, but it might not help Steve.
--
Aram Hăvărneanu
iainws
2012-11-19 10:45:03 UTC
Permalink
I would definitely use the new features of c++0x. Autos and Lambdas are the biggest noticeable change, so using them could be good style. Apparently the new constant expression offers optimisations from the compiler (probably equivalent to Java's final). Initialisation save's a couple of lines now. I'd also be interested in any books on the new threading part. Good luck.
Balwinder S Dheeman
2012-11-19 12:17:10 UTC
Permalink
Post by Steve Simon
I need to learn c++ for work - people have strong opinions on
languages I know, and not everyone likes c++ but its a requirment for me.
I really want to develop a good sence of c++ style, I learnt C at the feet of
K&R and then the plan9 sourcecode so I learnt how to write clean elegant code
(I think :-). The problem I am finding is there are many c++ styles and I have
yet to find a clean and elegant one.
anyone sugest a project that I could look at that contains well written code?
failing that is there a book that teaches good style?
I am refering to things like adding a leading m_ to class member variables
(which looks horrid to me but I am willing to learn), and smart locks (mutexs
which unlock on destruct).
I think, one should use proper design regardless of program size; keep
in mind that the small programs tend to become large programs.

In my view this means adhering to the OO (object oriented) programming
paradigm making full use of established design patterns. In essence this
boils down to programming against interfaces (pure virtual classes in C++).

The best books in the best practice category I've come across are:

1. C++ Coding Standards by Sutter and Alexandrescu, and
2. API design for C++ by Reddy.

The best OO design pattern introductory book I come across unfortunately
uses Java as model language but it's still worth reading for the insight,

3. Head First Design Patterns by Freeman & Freeman.

and a few of the must read books:

1. C++ Annotations by Frank B. Brokken
http://www.icce.rug.nl/documents/cplusplus/

See also:

1. http://www.cplusplus.com/

The best projects in the best/clean coding style I've come across are:

1. http://yate.null.ro/
2. http://qt.digia.com/

Hope that helps.
--
Balwinder S "bdheeman" Dheeman
(http://werc.homelinux.net/contact/)
hiro
2012-11-19 12:55:41 UTC
Permalink
Steve, what do they pay you for this? did you consider changing your
profession at all? I fear I might stand before the same kind of
decision some day.
Aram Hăvărneanu
2012-11-19 12:46:54 UTC
Permalink
Post by Balwinder S Dheeman
1. C++ Coding Standards by Sutter and Alexandrescu, and
Haven't read this book, but I read Modern C++ Design by Alexandrescu,
and it was one of the worst books on programming I have ever read,
pretty much the antithesis of what we consider good in this group, so
caveat emptor.
Post by Balwinder S Dheeman
1. http://yate.null.ro/
Bhaha. No.
--
Aram Hăvărneanu
c***@gmx.de
2012-11-19 13:22:33 UTC
Permalink
this is the problem. c++ programs follow a different
definition of "clean code" than idiomatic c programs.

by clean, they dont mean elegant or simple...

it usualy boils down to lots and lots of structure,
delegation and micro management and obsession with
consistency and patterns. there will be proxy classes
and visitor and mvc patterns and all that stuff and
a c++ programmer will call it clean code because it
follows constentent camel case naming and the funky
patterms can be looked up in some book, tho its
very hard to read or understand what the whole thing
is even supposed todo with all that indirection and
boiler plate.

--
cinap

PS: i think this is relevant:

Sometimes, the elegant implementation is just a function.
Not a method. Not a class. Not a framework. Just a function.
-- John Carmack
t***@polynum.com
2012-11-19 14:58:21 UTC
Permalink
It was not the question (since it is not a matter of taste but of
professional need), but I find quite significant that nobody has
cited a book of the author of the language as a good reading.

I'm sorry to say that, for me, this says something about the language
itself...
--
Thierry Laronde <tlaronde +AT+ polynum +dot+ com>
http://www.kergis.com/
Key fingerprint = 0FF7 E906 FBAF FE95 FD89 250D 52B1 AE95 6006 F40C
Bence Fábián
2012-11-19 15:17:03 UTC
Permalink
If i remember correctly

void foo();

in C++ is equivalent to

void foo(void)

in C. And the type of 'x' (character literal) in C++ is char. Apart
from that C++ is a superset of C.

But i think it hardly helps him. In the end he will have to follow the
conventions the others use. So I'd recommend reading the code you will
work on. We use C++ too. And sadly it is hardly consistent since it is
written by loads of people and noone bothers to check. But it's mostly
readable. No template metaprogramming voodoo. Thankfully i work on some
internal tools right now so i can get away with C and yacc. I'm even
using plan9 right now to write documentation for one of them :)
Post by t***@polynum.com
It was not the question (since it is not a matter of taste but of
professional need), but I find quite significant that nobody has
cited a book of the author of the language as a good reading.
I'm sorry to say that, for me, this says something about the language
itself...
--
Thierry Laronde <tlaronde +AT+ polynum +dot+ com>
http://www.kergis.com/
Key fingerprint = 0FF7 E906 FBAF FE95 FD89 250D 52B1 AE95 6006 F40C
Robert Raschke
2012-11-19 14:25:30 UTC
Permalink
The only C++ book that I ever found to be worth it, is Ruminations on C++
by Andrew Koenig.

Robby
Post by Steve Simon
I need to learn c++ for work - people have strong opinions on
languages I know, and not everyone likes c++ but its a requirment for me.
I really want to develop a good sence of c++ style, I learnt C at the feet of
K&R and then the plan9 sourcecode so I learnt how to write clean elegant code
(I think :-). The problem I am finding is there are many c++ styles and I have
yet to find a clean and elegant one.
anyone sugest a project that I could look at that contains well written code?
failing that is there a book that teaches good style?
I am refering to things like adding a leading m_ to class member variables
(which looks horrid to me but I am willing to learn), and smart locks (mutexs
which unlock on destruct).
Thanks for any suggestions.
-Steve
Calvin Morrison
2012-11-19 14:56:33 UTC
Permalink
Post by Steve Simon
I need to learn c++ for work - people have strong opinions on
languages I know, and not everyone likes c++ but its a requirment for me.
I really want to develop a good sence of c++ style, I learnt C at the feet of
K&R and then the plan9 sourcecode so I learnt how to write clean elegant code
(I think :-). The problem I am finding is there are many c++ styles and I have
yet to find a clean and elegant one.
anyone sugest a project that I could look at that contains well written code?
failing that is there a book that teaches good style?
I am refering to things like adding a leading m_ to class member variables
(which looks horrid to me but I am willing to learn), and smart locks (mutexs
which unlock on destruct).
Thanks for any suggestions.
-Steve
Isn't all C code valid C++? problem solved.
Kurt H Maier
2012-11-19 15:10:45 UTC
Permalink
Post by Calvin Morrison
Isn't all C code valid C++? problem solved.
As of c99, they have diverged.
David Leimbach
2012-11-22 14:10:37 UTC
Permalink
Post by Kurt H Maier
Post by Calvin Morrison
Isn't all C code valid C++? problem solved.
As of c99, they have diverged.
They weren't the same in 1998 either.
David Leimbach
2012-11-22 14:13:38 UTC
Permalink
My advice. Read Scott Meyer's books on Effective C++. He's probably the
most sane of the authors out there. Also Exceptional C++ isn't too bad by
Herb Sutter. It's a bunch of "puzzles" which are kind of fun to work out
but will reveal ignorance at the same time.

C++ Gotchas is kind of a fun book, but I'm not sure I've even read that one
all the way through.
Post by Kurt H Maier
Post by Calvin Morrison
Isn't all C code valid C++? problem solved.
As of c99, they have diverged.
They weren't the same in 1998 either.
c***@gmx.de
2012-11-19 14:09:45 UTC
Permalink
no, its not.

--
cinap
Bence Fábián
2012-11-19 15:38:25 UTC
Permalink
Then it's not. And if it were it still wouldn't do any good. Just check the
code of the others.
Post by c***@gmx.de
no, its not.
--
cinap
Jeff Sickel
2012-11-19 16:45:14 UTC
Permalink
Steve,

A lot will depend on the target compiler and toolchain you'll
be using. There's a big push in certain circles to go to Clang
and C++11

<n.tufnel>
Well, it's one louder, isn't it? It's not ten. You see, most
blokes, you know, will be playing at ten. You're on ten here,
all the way up, all the way up, all the way up, you're on ten
on your guitar. Where can you go from there? Where?
</n.tufnel>

though on the other hand there are a lot of groups trying to
boost Boost. I don't recommend looking at the Boost
implementation, it will make your compiler churn and your head
spin. And then once you understand it a bit, you'll have to
code tons of workarounds to slice out pieces of Boost.

Google's protocol buffers source has a decent C++ implementation
and should be a good example of writing relatively clean C++ code.
Also take a look at (http://www.250bpm.com/blog:4) for a few
pointers what not to do.

-jas
Andrés Domínguez
2012-11-19 16:21:26 UTC
Permalink
Post by Steve Simon
I need to learn c++ for work - people have strong opinions on
languages I know, and not everyone likes c++ but its a requirment for me.
"C++ Annotations" is good for C programers, in my opinion.
http://www.icce.rug.nl/documents/cplusplus/
Post by Steve Simon
I really want to develop a good sence of c++ style, I learnt C at the feet of
K&R and then the plan9 sourcecode so I learnt how to write clean elegant code
(I think :-). The problem I am finding is there are many c++ styles and I have
yet to find a clean and elegant one.
I don't think that an elegant c++ style exists, not even a usual c++
subset. Every
project uses his own c++ subsets and styles, so I think that adapting to every
project style is the key.

Andrés
Aharon Robbins
2012-11-19 20:02:01 UTC
Permalink
Hello Steve, 9fans.
Post by Steve Simon
I need to learn c++ for work - people have strong opinions on
languages I know, and not everyone likes c++ but its a requirment for me.
I really want to develop a good sence of c++ style, I learnt C at the feet of
K&R and then the plan9 sourcecode so I learnt how to write clean elegant code
(I think :-). The problem I am finding is there are many c++ styles and I have
yet to find a clean and elegant one.
anyone sugest a project that I could look at that contains well written code?
failing that is there a book that teaches good style?
This letter may end up being a bit long, and will be contrary to the popular
wisdom on 9fans. So be it.

I have over 30 years of C and Unix experience and ~13 years of C++ experience.
For whatever that's worth.

I will try to offer practical advice on the assumption that you want to
keep your job and therefore you want to learn C++ and learn it well.

First, realize that C++ is a MUCH bigger language than C. There's more to
C++ style than just naming conventions. You should work to understand
the language features, first, and in particular understand that they
often interact in somewhat strange ways.

For learning the language, you should probably go with the C++ Primer
by Lippman, Lajoie and Moo. I see that a new edition just came out:
http://www.amazon.com/Primer-5th-Stanley-B-Lippman/dp/0321714113/ref=sr_1_1?s=books&ie=UTF8&qid=1353353296&sr=1-1&keywords=c%2B%2B+primer

Write some code, get a feel for things. Work your way up slowly, since
there's a lot to learn.

To understand the intricacies of C++, you can't do better than Scott Myers
"Effective C++", "More Effective C++" and "Effective STL":

http://www.amazon.com/Effective-Specific-Improve-Programs-Designs/dp/0321334876/ref=sr_1_1?s=books&ie=UTF8&qid=1353353626&sr=1-1&keywords=Scott+Myers+C%2B%2B

http://www.amazon.com/More-Effective-Improve-Programs-Designs/dp/020163371X/ref=sr_1_4?s=books&ie=UTF8&qid=1353353626&sr=1-4&keywords=Scott+Myers+C%2B%2B

http://www.amazon.com/Effective-STL-Specific-Standard-Template/dp/0201749629/ref=sr_1_3?s=books&ie=UTF8&qid=1353353626&sr=1-3&keywords=Scott+Myers+C%2B%2B

In addition, a book that I found really helpful was Stanley Lippman's
"Inside the C++ Object Model": http://www.amazon.com/Inside-Object-Model-Stanley-Lippman/dp/0201834545/ref=sr_1_1?s=books&ie=UTF8&qid=1353353699&sr=1-1&keywords=C%2B%2B+object+model

Lippman worked with Stroustrup on cfront at Bell Labs, he's been involved
since the beginning. Also, he's a great writer.

Despite the scorn expressed here for Design Patterns, they have value.
The canonical reference is the book by the Gang of Four: http://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612/ref=sr_1_1?s=books&ie=UTF8&qid=1353353825&sr=1-1&keywords=Design+Patterns

Not all the patterns see the same amount of use in C++, but a few are
critical for understanding and working with everyday C++: Factory, Singleton
and Visitor come to mind, there are some others. In any case, the book is
worth having as a reference.

Java and C# provide large "frameworks" out of the box (vendor supplied),
making programming in them a much more "pick the parts and stich them
together" practice. C++ does not have this, although the Qt framework
goes a long way towards filling the gap. The Qt classes are well designed
and make for clean code that also avoids your having to reinvent certain
wheels yet again.

C++ is far from a perfect language. It tries to be many things to many people.
Using it well requires discipline on the part of the programmer, as opposed
to newer languages where the compiler imposes much of the discipline. The
Myers books will help with that.

And it's possible to do awful things with it. I think template metaprogramming
is the work of the devil and regular programmers shouldn't even consider
going anywhere near it.

And yes, there's a lot of miserable C++ out there. There's also a lot
of miserable C and shell and even awk code out there. This just proves
that there are lots of crappy programmers.

However, having worked on a project containing over 4 Million lines of C++
code, I can say that there are things you can do with object oriented
design and code in C++ that you simply could not do were you trying to
do them in C.

Some of the cleanest and most elegant code I have ever seen, as well as
some of the best code I have ever written, was in C++.

There is much, Much, MUCH more that could be said, for and against C++.
I'm probably aware of most of it, so don't bother flaming me. I do think
that C++ is worth learning, knowing, and using well. (I think that Walter
Bright's D is an even better language, but that's another story.)

So Steve, look upon this as an adventure and keep an open mind. You may find
that you are doing new, different, and maybe even enjoyable things as
your horizons open up.

I think the books I've listed will give you a good start if you're serious
about working in C++. There are others I can name also. Feel free to
ping me (probably best off list - remove the 9fans from the subject header
so that it doesn't get sent to the wrong mailbox).

Thanks,

Arnold Robbins
Taj Khattra
2012-11-20 02:24:11 UTC
Permalink
Post by Steve Simon
anyone sugest a project that I could look at that contains well written code?
https://code.google.com/p/leveldb (mostly written by sanjay ghemawat i
believe)

given its pedigree, it probably follows the google c++ style guide:
http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml
Bruce Ellis
2012-11-20 03:47:22 UTC
Permalink
best answer yet. the google c++ style guide is your friend, but you will be
lucky to find code in the wild that adheres to the rules. i like this:

C++11
▶
Use only approved libraries and language extensions from C++11 (formerly
known as C++0x). Currently, none are approved.


brucee
Post by Steve Simon
anyone sugest a project that I could look at that contains well written
code?
https://code.google.com/p/leveldb (mostly written by sanjay ghemawat i
believe)
http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml
--
Don't meddle in the mouth -- MVS (0416935147, +1-513-3BRUCEE)
Continue reading on narkive:
Loading...