3 Better modern C++: General programming

 

This chapter covers

  • Uniform initialization
  • Enhanced if statements
  • Tuples
  • Structured binding

In the dynamic landscape of C++ programming, coding practices are often shaped by deeply ingrained habits, influencing our problem-solving approaches. However, as we navigate the intricacies of traditional loops, selection statements, and variable initialization techniques, it becomes evident that adherence to tradition only sometimes guarantees readability or effectiveness. While these familiar approaches may initially seem intuitive, they often conceal correctness, readability, and overall code efficiency challenges.

As C++ builds upon the foundational principles of its predecessor, C, it has introduced new initialization techniques and data aggregation methods. This evolution brings forth its own set of complexities. The array of initialization methods can result in cognitive overload and inconsistencies across source files within a code base. Similarly, the reliance on structures or classes for aggregating diverse data types can prove cumbersome, mainly when simplicity is paramount. Although Standard Template Library templates offer an alternative solution, they, too, present challenges, potentially hindering code readability and effectiveness.

3.1 Mistake 10: Failing to use if with initialization

3.2 Mistake 11: Not using type inference for variables

3.3 Mistake 12: Using typedef

3.4 Mistake 13: Writing common algorithms

3.5 Mistake 14: Not using uniform initialization

3.6 Mistake 15: Failing to use emplacement

3.7 Mistake 16: Failing to use tuples

3.8 Mistake 17: Not using structured binding