Example of good C++ design/code

P

Poldie

I was just wondering what the erudite folks reading this group would
recommend as an example of a clean, well designed and coded C++
application. I'm particularly interested in the way classes are
composed, and I'm also fairly interested in exception handling. All C+
+ books are full of examples of cars and cats but these seem to be
idealized examples, and I'm interested in real work examples. Perhaps
there are some open source projects which are readily obtainable which
someone here can give me a pointer to.
 
C

cpp4ever

I was just wondering what the erudite folks reading this group would
recommend as an example of a clean, well designed and coded C++
application. I'm particularly interested in the way classes are
composed, and I'm also fairly interested in exception handling. All C+
+ books are full of examples of cars and cats but these seem to be
idealized examples, and I'm interested in real work examples. Perhaps
there are some open source projects which are readily obtainable which
someone here can give me a pointer to.

IMHO regardless of how good the code works, if it is not documented it
will potentially become unmaintainable. In light of this I recommend you
look at Doxygen which can generate code documentation from the code and
specialised comments.

IMHO I'd recommend using object orientation, which in simple terms means
creating classes to provide a black box function with well defined
interfaces. Code format style is not so important as code beautifiers
can reformat as required, (I use uncrustify).

IMHO you should keep all functions, or functional blocks to less than 2
pages, (~50 lines), for ease of understanding. Also your code should
attempt to catch any exceptions as close to the point of occurrence as
reasonable thus reducing the time determining their source if they do
occur. Lastly, you should pay special attention to constructors and
destructors of classes that allocate/free/share memory. These problems
can be difficult to debug, so be clear as to when they go in and out of
scope, and which are responsible for allocated memory! On that note, do
use the STL libraries which will save you a lot of time.

Regards

JB
 
P

Poldie

I agree with everything you say apart from the rather arbitrary limit of 50
lines for a function.  Functional decomposition is good yes but specifying
some artificial limit on the number of lines for a function is almost
irrational.

/Leigh

Thanks for the replies. But at this point I'm not seeking coding
advice, or tools which generate documentation. What I am after is the
names of applications which embody those rules which I can download
and explore the source of, so I can see how other people have done
things.

Cheers!
 
C

cpp4ever

Thanks for the replies. But at this point I'm not seeking coding
advice, or tools which generate documentation. What I am after is the
names of applications which embody those rules which I can download
and explore the source of, so I can see how other people have done
things.

Cheers!

The Qt cross platform GUI development kit and the examples are a good
place to start. Plenty of examples and all documented. Unfortunately it
does have some Qt extensions embedded within, but is still worthwhile.

Regards

JB
 
U

usenet only_tech_talk

I was just wondering what the erudite folks reading this group would
recommend as an example of a clean, well designed and coded C++
application.  I'm particularly interested in the way classes are
composed, and I'm also fairly interested in exception handling.  All C+
+ books are full of examples of cars and cats but these seem to be
idealized examples, and I'm interested in real work examples.  Perhaps
there are some open source projects which are readily obtainable which
someone here can give me a pointer to.

The boost library represents the best practice in C++ programming
using templates.

The boost::math library, in particular, has a thorough approach to <b>
exception handling </b>.

<b> Class composition </b> means different things in different
contexts. See Section 9.5 of C++ template metaprogramming, for example.
 
B

Brian

On that note, do
use the STL libraries which will save you a lot of time.

I suggest using flex_string as an alternative to std::string.
Boost has a number of containers libraries including the
Intrusive library that are worth considering also.


Brian Wood
http://webEbenezer.net
(651) 251-9384
 
P

Poldie

The boost library represents the best practice in C++ programming
using templates.

The boost::math library, in particular, has a thorough approach to <b>
exception handling </b>.

<b> Class composition </b> means different things in different
contexts. See Section 9.5 of C++ template metaprogramming, for example.

Thanks for that - I've downloaded Boost and will take a look. I might
even use it in my own projects - although at this point I'm quite
interested in "standard" stuff like STL. Also, I'm not sure how
similar library design is from application design - I'd imagine
there's quite a difference in some respects.
 
J

James Kanze

On 03/06/2010 12:01 PM, Poldie wrote:
IMHO you should keep all functions, or functional blocks to
less than 2 pages, (~50 lines), for ease of understanding.

I don't think you can set an absolute maximum limit, as there
are special cases. (Consider a function which consists of a
single switch statement, with a 100 or so cases.) But in
general, anything over about 10 lines in a function should raise
eyebrows; well written functions are *usually* fairly short. (I
did some statistics on my code once, and found that the average
function length was around 7 lines. I'm not too sure how
significant that is, however, since I'm pretty sure that the
variance is important.)
Also your code should attempt to catch any exceptions as close
to the point of occurrence as reasonable thus reducing the
time determining their source if they do occur.

The whole point of exceptions is that you don't have to catch
them near the point of occurance, and that you shouldn't have to
know where they occurred. If you need to know where the error
occurred, exceptions are not the right mechanism for reporting
it.
Lastly, you should pay special attention to constructors and
destructors of classes that allocate/free/share memory.

"Classes" that allocate, free or share memory should be very
rare, and generally only in the lowest layers. (Again, there
are exceptions.)
 
I

Ian Collins

Leigh said:
Where do you get this figure of 10 lines from? Seems like another
arbitrary number just like 50 to me.

It's not an uncommon guideline. It is one I strive to follow.
 
U

usenet only_tech_talk

Thanks for that - I've downloaded Boost and will take a look.  I might
even use it in my own projects  - although at this point I'm quite
interested in "standard" stuff like STL.   Also, I'm not sure how

It doesn't get more standard than boost. boost is based on the idea of
the STL (I expect the nitpicky lot to disagree vehemently), but for a
much wider range of applications than containers.
similar library design is from application design - I'd imagine
there's quite a difference in some respects.

Indeed, what's the difference?

People love to answer these kinds of questions (what book do you
recommend etc.). I'm one of them. My question is still pending
though ;-)
 
P

Pavel

Poldie said:
I was just wondering what the erudite folks reading this group would
recommend as an example of a clean, well designed and coded C++
application. I'm particularly interested in the way classes are
composed, and I'm also fairly interested in exception handling. All C+
+ books are full of examples of cars and cats but these seem to be
idealized examples, and I'm interested in real work examples. Perhaps
there are some open source projects which are readily obtainable which
someone here can give me a pointer to.
Bigger or "Real-world" applications are usually the least exciting in
terms of C++ code you will find there. They are also rarely written in
just C++. Sometimes I feel it's no coincidence. Except to spend a lot of
time understanding their processes like build, configuration management etc.

If you are still enthusiastic, take on something decently big like
Mozilla XPCOM architecture. One good starting point for code browsing
(for this and other apps) is Google codeSearch, e.g. for XPCOM see

http://www.google.com/codesearch/p?hl=en#e_ObwTAVPyo/xpcom/

Reading their C++ rules
(https://developer.mozilla.org/en/C___Portability_Guide) may help in
understanding why the coding style is what it is.


Hope this helps,
-Pavel

PS. Some may argue it's not a well-designed C++ and not "pure" C++ and
"not actually C++" and "not modern enough C++" and what's not. IMHO, if
an application of size of Seamonkey or Firefox works reasonably well and
evolves reasonably fast at the comparable number of platforms, it's damn
well-designed.
 
B

Balwinder S Dheeman

The Qt cross platform GUI development kit and the examples are a good
place to start. Plenty of examples and all documented. Unfortunately it
does have some Qt extensions embedded within, but is still worthwhile.

I think, the FLTK http://www.fltk.org/, is purely C++ with lot of
working examples.

Also the YATE http://yate.null.ro/, the base is C++ and they rewrote
most of the external libraries in C++ and mostly avoid using/mixing C.
 
P

Poldie

It doesn't get more standard than boost. boost is based on the idea of
the STL (I expect the nitpicky lot to disagree vehemently), but for a
much wider range of applications than containers.


Indeed, what's the difference?

People love to answer these kinds of questions (what book do you
recommend etc.). I'm one of them. My question is still pending
though ;-)

When I say `standard` I mean I don't want to learn something then find
when I (try to) get a job somewhere they've never heard of it and have
no interest in starting to use it. For my own uses, I have no
objections, as long as it's stable and well supported (ie not some
dead project which hasn't been touched for 12 years).

And in terms of the different between a library and an app, well,
perhaps I'm talking out of ignorance here but I think of an app as a
living piece of code where program flow goes round and round, with a
certain (sometimes substantial) amount of state being built up and
changed, whereas I tend to think of libraries as containing code which
writes to a log file; encrypts a string; acts as a container of some
type - you use it, get the job done and move it. I'd imagine they're
easier to test. But like I said, perhaps I'm wrong.
 
P

Poldie

Bigger or "Real-world" applications are usually the least exciting in
terms of C++ code you will find there. They are also rarely written in
just C++. Sometimes I feel it's no coincidence. Except to spend a lot of
time understanding their processes like build, configuration management etc.

I'm pragmatic and realize I won't only be coding in C++. I also
intend to port my test project to Java and C#, so a lot of what I want
to learn is just best practices for OO projects. So there'll be some
differences (ie no static classes in C++ but they're in C#), but
mostly I hope to be able to learn concepts (use of patterns, exception
handling, comment/variable naming style) which are portable between OO
languages.
Reading their C++ rules
(https://developer.mozilla.org/en/C___Portability_Guide) may help in
understanding why the coding style is what it is.

That guide contains this:

---
Don't use exceptions

Exceptions are another C++ feature which is not very widely
implemented, and as such, their use is not portable C++ code. Don't
use them. Unfortunately, there is no good workaround that produces
similar functionality.
 
P

Poldie

Also intelligent variable/function/class names can be good documentation in
itself.

I've read that if you need comments at all you're doing something
wrong. Personally, I can't imagine having no comments at all (you
might want to describe why you took a decision, not what/how code is
functioning), but it sounds like an ideal worth working towards.
 
U

usenet only_tech_talk

When I say `standard` I mean I don't want to learn something then find
when I (try to) get a job somewhere they've never heard of it and have
no interest in starting to use it.  For my own uses, I have no

If an employer intends on hiring someone to write an app/library of
significance and makes a point of ignoring the standard libraries that
are out there, I'd rather pass on the job the the next guy and wait
for a better opportunity.
 
A

Anthony Delroy

That guide contains this:

---
Don't use exceptions

Exceptions are another C++ feature which is not very widely
implemented, and as such, their use is not portable C++ code. Don't
use them. Unfortunately, there is no good workaround that produces
similar functionality.
---

Are there really any C++ compilers which don't provide exception
handling?  I thought that was fairly fundamental to the language and
OO design/coding.  What's the alternative?

That advice is dated... I'd ignore it. The alternatives are returning
error codes "C style" (not possible from constructors, operators etc),
and keeping some error state inside an object that must then be
queried in a later statement (i.e. X x(arg1, arg2); if (not
x.is_valid()) yikes();). Both techniques have their place, but so
does exception handling, and refusing to use exceptions in code that
needs to be reasonably portable hasn't been necessary for about a
decade.

Cheers,
Tony
 
P

Pavel

Poldie said:
I'm pragmatic and realize I won't only be coding in C++. I also
intend to port my test project to Java and C#, so a lot of what I want
to learn is just best practices for OO projects. So there'll be some
differences (ie no static classes in C++ but they're in C#), but
mostly I hope to be able to learn concepts (use of patterns, exception
handling, comment/variable naming style) which are portable between OO
languages.


That guide contains this:

---
Don't use exceptions

Exceptions are another C++ feature which is not very widely
implemented, and as such, their use is not portable C++ code. Don't
use them. Unfortunately, there is no good workaround that produces
similar functionality.
There are combinations of compilers+target platforms that don't provide
exception handling in accordance with the Standard. One such would be
g++ for Symbian OS (former EPOC32) unless something big happened
recently and they scrapped their "brilliant" concept of "Leaving" and
"Clean-up stacks".

It's actually an instructive recommendation well illustrating my
previous point: big multiplatform applications have to be
over-conservative in using language features so what you can really
learn from them is a combination of tools and features that are safe to
use in a big multi-platform application, not something exciting about C++.

I thought that was fairly fundamental to the language and
OO design/coding. What's the alternative?


Exceptions often come handy. I find them elegant. This does not make
them irreplaceable. The way XPCOM architects decided to process error is
one of the alternatives (they simply use result codes and it works just
fine although is not as exciting as exception and takes some additional
discipline).

BTW, when XPCOM crosses boundary between C++ and JavaScript, result
codes are converted to JavaScript exceptions... I suggest that you read
the code yourself; you will make many more discoveries on your own --
after all it was what you were asking for, right?

-Pavel
 
K

Krice

what I want to learn is just best practices for OO projects.

C++ is probably not the best of OO languages and in case
of C++ what you really need to learn is how to deal with
weaknesses of the language: memory management in general,
pointers, ownership issues with pointers/objects and raw arrays
(how to avoid them or make them safe) to name a few.

Class design is easy to understand but hard to master. I believe
it requires experience and is something you don't learn just like
that.

Last but not least, to become a better programmer you have to
forget two things: your ego and your cemented ideas of what
you thought was good programming. The only thing that determines
good programming is the quality of the results of programming.
 
J

Jeff Flinn

cpp4ever said:
....

The Qt cross platform GUI development kit and the examples are a good
place to start. Plenty of examples and all documented. Unfortunately it
does have some Qt extensions embedded within, but is still worthwhile.

Qt and it's examples are the last place I'd look for clean well designed
C++ code. I've never seen so much abuse of operator new in my life.
Their examples look like they been written for Java.

Jeff
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top