DEV Community

Unicorn Developer
Unicorn Developer

Posted on

Collecting the best C++ practices

Reading our articles, you may wonder: why are they always talking about bad things? This code contains bug; there are code smells here; it's an antipattern — don't code like that. So, would you help me to look on the bright side of C++ programming? Feel free to leave comments 💬

Image description

Recently I came across a discussion on Reddit "Good repos for beginners to browse that follow best modern C++ practices (including testing, static analysis etc...)". The thread is not the longest one, but it's still quite interesting. Then, an idea popped into my head — why don't I post a survey on Habr.com? I did the same with terrible coding tips.

By the way, the idea to write an article about terrible coding tips is evolving. Soon I plan to publish a book with 60 anti-patterns in C++. Subscribe to our monthly newsletter, if you don't want to miss the most interesting publications.

So, the topic of terrible coding tips is covered :). It's time to talk about good practices!

I go first. Then, I invite you to join me and write in the comments:

  • what tools you recommend;
  • what interesting and useful libraries you use;
  • what projects are useful in learning C++;
  • what practices, coding standards you can advise;
  • and so on.

Starters

  1. ModernCppStarter. Kick-start your C++! A template for modern C++ projects using CMake, CI, code coverage, clang-format, reproducible dependency management and much more.
  2. gui_starter_template. This is a C++ Best Practices GitHub template for getting up and running with C++ quickly.

Projects to learn C++

Note to self. If I write an article on the collected information later, I need to check these and other proposed projects with PVS-Studio. Libraries are different, you know... I don't want to recommend something potentially buggy as an example to follow.

  1. Diligent Engine. A Modern Cross-Platform Low-Level 3D Graphics Library and Rendering Framework Tweet.
  2. JSON for Modern C++. Intuitive syntax. Trivial integration. Serious testing. Memory efficiency. Speed.
  3. Stroika is a modern, portable, thread-savvy, C++ application framework. It makes writing high performance C++ applications easier by providing safe, flexible, modular building blocks.
  4. concurrencpp. Modern concurrency for C++. Tasks, executors, timers and C++20 coroutines to rule them all.
  5. awesome-hpp. A curated list of awesome header-only C++ libraries.

Speeding up the build

  1. Here I invite you to read my colleague's article "Speeding up the build of C and C++ projects".
  2. There are various old and new discussions on this topic. I am excited to learn about new, interesting things on this topic from you. Thanks in advance.

Coding standards and style guides

  1. The C++ Core Guidelines are a collaborative effort led by Bjarne Stroustrup, much like the C++ language itself. They are the result of many person-years of discussion and design across a number of organizations.
  2. Collaborative Collection of C++ Best Practices. This online resource is part of Jason Turner's collection of C++ Best Practices resources. By the way, since I mentioned Jason, here's the link to his C++ Weakly channel.
  3. Google C++ Style Guide. The goal of this guide is to manage this complexity by describing in detail the dos and don'ts of writing C++ code. These rules exist to keep the code base manageable while still allowing coders to use C++ language features productively.

Dynamic code analysis

  1. AddressSanitizer finds memory errors.
  2. LeakSanitizer searches for memory leaks.
  3. ThreadSanitizer detects data races and deadlocks.
  4. MemorySanitizer looks for uninitialized memory.
  5. HWASAN, or Hardware-assisted AddressSanitizer, a new variant of AddressSanitizer that uses less memory.
  6. UBSan finds undefined behavior in a program.

Static code analyzers

  1. Here I will play dumb and mention only our PVS-Studio. Well, why not? It's a wonderful and powerful static code analyzer. The tool helps find lots of errors and potential vulnerabilities even at the stage of writing C++ code.
  2. TODO. I'm also excited to hear about your successful experiences of using other static code analyzers.

Books and other references

  1. I always recommend everyone to read Code Complete by Steve McConnell (ISBN 0-7356-1967-0).
  2. C++ Best Practices (2019).
  3. The Ultimate Question of Programming, Refactoring, and Everything.
  4. C++ Coding Standards: 101 Rules, Guidelines, and Best Practices.
  5. Scott Meyers. Effective C++: 55 Specific Ways to Improve Your Programs and Designs (3rd edition).
  6. Scott Meyers. Effective Modern C++: 42 Specific Ways to Improve Your Use of C++11 and C++14".

Now, it's your turn 💥

Please share everything that you consider useful to C++ programmers!

Top comments (1)

Collapse
 
creative_fuel22 profile image
Pauline Al • Edited

I would recommend keeping YouTube in mind. There's always something you'll find interesting, like conference talks, podcasts, and tutorials. Here’s my personal list of favorite channels:

One more thing, since you mentioned static and dynamic code analysis, let's not forget about unit testing as well :) There are more than enough libraries for this, but I use the following ones: