C++ is a high-performance, general-purpose programming language created as an extension of the C programming language. It provides object-oriented features, generic programming, and low-level memory manipulation.
C++ Anti-Patterns Overview
Raw Pointer Ownership
std::unique_ptr
, std::shared_ptr
) to clearly express ownership semantics.Manual Resource Management
Not Using const Correctly
const
for methods that don’t modify object state, parameters that shouldn’t be modified, and return values that shouldn’t be modified.Using Raw Loops Instead of Algorithms
Premature Optimization
Not Using Modern C++ Features
std::string
, std::string_view
, std::optional
, std::variant
, and others to write safer, more expressive code.Not Using nullptr
nullptr
instead of NULL
or 0
for null pointers. It’s type-safe and avoids ambiguity with integer literals.Not Using auto for Complex Types
auto
for complex types to improve readability and maintainability, especially for iterators and lambda types.Not Using Range-Based For Loops
Not Using Structured Bindings
Not Using std::optional
std::optional
(C++17) to represent values that may or may not be present, instead of using special values or output parameters.Not Using std::variant
std::variant
(C++17) for type-safe unions, and std::visit
with overloaded lambdas for processing.Not Using Move Semantics
Not Using std::string_view
std::string_view
(C++17) for functions that only need to read string data, to avoid unnecessary copies.Not Using Proper Exception Handling
Not Using Proper Memory Management
Not Using Rule of Zero/Three/Five
Not Using Forward Declarations
Not Using Proper Initialization
Not Using Proper Error Handling
std::expected
(C++23) or similar for expected failures.