What are some dos and don'ts in C++ ?

Here are some guidelines to follow when programming in C++, along with a brief list of dos and don'ts in C++. Let's start with the don'ts section first.
Don'ts
Avoid using raw arrays in C++
Why should we avoid raw arrays in modern C++? Raw arrays get converted to pointers when used as function arguments. We say they decay to pointers. With raw arrays, we can also inadvertently read out of array bounds and cause undefined behavior.
What should we use instead of raw arrays? Use the std::array
container instead. This container does not get converted to a pointer when used as a function argument.
Avoid using raw pointers in C++
With raw pointers, we must manually allocate and deallocate memory. Take, for example, the following simple code that uses a raw pointer to dynamically allocate a space for a single integer:
#include <iostream>
int main()
{
int* p = new int{123};
std::cout << "The pointed-to value is " << *p << '\n';
delete p;
}
If we forget to deallocate the memory manually, we can cause a memory leak:
#include <iostream>
int main()
{
int* p = new int{123};
std::cout << "The pointed-to value is " << *p << '\n';
// we forgot to manually deallocate the memory
} // and are causing a memory leak
What should we use instead of raw pointers? Use smart pointers instead. Smart pointers automatically clean up the allocated memory when they go out of scope. More precisely, use the std::unique
smart pointer instead of a raw pointer. The above code can be rewritten as:
#include <iostream>
#include <memory>
int main()
{
std::unique_ptr<int> p = std::make_unique<int>(123)
std::cout << "The pointed-to value is " << *p << '\n';
}
Avoid using character arrays or pointers to manipulate strings in C++
In C, it is perfectly fine to use them, whereas in C++, it is not. Raw arrays and pointers can cause undefined behavior and memory leaks. And in C++, we already have a dedicated string type called std::string
. This type is declared inside a <string>
header file and is used for defining and modifying strings in C++. It is the idiomatic way of manipulating strings in C++.
So, instead of writing:
char arr[] = "C-style string";
const char* p = "C-style string";
Use the std::string
type instead:
std::string s = "C++ string";
Don't think in terms of bits and bytes
Avoid thinking about data in terms of bits and bytes in C++. Think in terms of objects. An object is an occupied region of memory that has a type, an address, and potentially a name and a value.
Do not mix C with C++
Try not to mix C and C++, and avoid writing C-style code in C++. C and C++ are two distinct languages with different paradigms and coding styles.
Dos
Use containers and algorithms from the C++ Standard Library
Get into the habit of using containers to store your data, specifically an array of data. Some of the most widely used containers are std::array
, std::vector
, std::list
, std::set
and std::map
. Additionally, utilize the ready-made algorithms to manipulate data stored in these containers. Some of the most widely used algorithms are std::sort
, std::find
, std::reverse
, std::count
, and similar.
Use smart pointers instead of raw pointers
Smart pointers automatically destroy the pointed-to object and deallocate the memory when they go out of scope. Two major kinds of smart pointers are unique pointer - std::unique_ptr
and shared pointers - std::shared_ptr
. Prefer the std::unique_ptr
to raw pointers in your C++ programs.
Use the std::string type for strings
Use the std::string
type to declare and manipulate strings in C++. This class has a plethora of built-in functions that make string manipulation in C++ that much easier.
Think in terms of objects
Think about data in terms of objects. We can think about objects as rectangular areas representing a certain amount of occupied memory. Objects in memory are characterized by a type, an address, and usually a name and a value. These objects should not be confused with instances of classes, which are also referred to as objects. The following code:
int c = 'A';
int x = 123;
double d = 456.789;
int* p = &x;
Can be visualized in terms of objects using the following diagram:

Use at least the C++11 standard
Everything starting with C++11 onwards is considered to be modern C++. The C++11 standard should be the bottom line when writing C++ programs nowadays. Some of the notable C++11 features are:
- Smart pointers
- Range-based loops
- The
constexpr
specifier - Lambda expressions
- The move semantics