3

I was developing for quite long time already on C/C++ (mostly C, which makes style poorer). So, I know how to use it. However, quite often I stuck with style decisions like: - should I return error code here, throw exceptions, return error through a parameter - should I have all this stuff in constructor or should I create separate init function for that. and so on.

Any solutions WILL work. However, each of them has cons and pros, which I know and most importantly which I don't know.

It would be very nice to read something regarding overall C++ development style, coding practices and so forth. What do you recommend?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Victor Ronin
  • 22,758
  • 18
  • 92
  • 184

5 Answers5

3

I've been recommended this by many people and I have a copy. Effective C++: 55 Specific Ways to Improve Your Programs and Designs (3rd Edition) http://www.amazon.com/exec/obidos/ASIN/0321334876/christopherheng

Girish Rao
  • 2,609
  • 1
  • 20
  • 24
3

Here is a list of really good books on C++:

The Definitive C++ Book Guide and List

Read few of them as per your level. That would most certainly improve your coding style!

I would personally suggest you to read:

  • Effective C++ series C++ by Scott Meyers
  • Exceptional C++ series by Herb Sutter

Exceptional C++ discusses exception-safe code in-depth. After reading this book, I learned how exception-safety effects design of classes, and interfaces. Highly recommended!

Community
  • 1
  • 1
Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • 3
    And read More Effective C++, Effective STL, and the others in the Exceptional C++ series. – Dr. Watson Feb 04 '11 at 23:23
  • +1 for Meyers' stuff, -1 for Exceptional C++ that IMO is not that interesting (especially about style). – 6502 Feb 04 '11 at 23:29
  • @6502: I disagree. I think it's Exceptional C++ which discusses exception-safe code in depth. After reading this book, I learned how exception effects design of classes, and interfaces! – Nawaz Feb 04 '11 at 23:33
  • True indeed... the exception safety part is reasonably good (even if IMO doesn't cover IIRC the dark sides of C++ exception model and why it's not a silver bullet - I've all my C++ books at the office and I can't check right now). +1 but still I think Effective C++ is better. – 6502 Feb 04 '11 at 23:42
2

One book in particular jumps out: C++ Coding Standards. This book does recommend some slightly questionable/useless ideas (like postfix _ for members) but mostly it's very solid.

Next important ones for style are the "Exceptional" book by Sutter. The good thing about them is that they cover important areas where C++ is rather..."different". It covers how to protect your code against exceptions and explains in detail the effects of exceptions upon coding practices.

The Myers books are good too, but a little dated. The Red books are more important IMHO.

Another dated book that is often overlooked is Generic Programming and the STL. It's almost, possibly IS, pre-standard but it discusses the hows and whys of the STL, which is pretty darn important for any C++ developer. Always amazes me how amazed people are when I show them a bit of tag-dispatching code.

Edward Strange
  • 40,307
  • 7
  • 73
  • 125
1

It's an older book, but I'm a big fan of 'Code Complete' from Microsoft. It helped me to think about some of the design choices like the ones you've described. They advocate a strategy of designing code that helps to debug itself.

A very rudimentary example is variable testing in an if statement

if(nCheck = 1) {
    // will always do this because nCheck was assigned 0
}

if you get into the habit of writing your if statements like the following

if(1 = nCheck) {
    // Now the compiler will catch the assignment as an error
}

Your code should help you by catching most of the easy syntactical bugs. This is just the tip of the iceberg. The book has many such examples of clever practices that microsoft has used over the years.

zeroSkillz
  • 1,448
  • 1
  • 11
  • 17
0

For better understanding C++ and for style I would recommend

  1. Effective C++ series from Meyers
  2. C++ FAQs (the book) from Cline

Both are also an "easy reading" thanks to the small-pills format.

6502
  • 112,025
  • 15
  • 165
  • 265