开发者

What should I read to improve my C++ style

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 c开发者_StackOverflow社区ode 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?


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!


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


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜