What I Learned from Scott Meyers' Effective C++, Part 1

Chapter 1: Accustoming Yourself to C++

C++ is a massive federation of languages according to Scott Meyers and it takes extensive studies of it to truly understand and utilize its features. Scott Meyer's Effective C++ book is highly recommended for anyone interested in understanding the quirks and mechanisms of C++. I will be sharing my learnings from the book in a distilled format, in a series of articles here. There are 9 chapters of various sizes in the book and I will be posting each chapter as a separate article.

Item 1: View C++ as a federation of languages

C++ is a multi-paradigm programming language featuring different approaches to programming. In essence, C++ is a superset of C language, it is also Object-Oriented, includes template programming features and has a huge Standard Template Library, featuring the most commonly used containers and algorithms. The rules of effective programming change depending on the features you are utilizing.

Item 2: Prefer consts, enums, and inlines to #defines

The compiler is a more reliable tool than the preprocessor. #define is essentially a text copy paste tool and is prone to logic errors when used without proper care. It is often a better idea to use const variables or enums instead of #define macros. #define is also commonly used to implement a macro function to reduce function call overhead. However, this is also error-prone and is better implemented using inline functions.

Item 3: Use const whenever possible

Const is a great keyword that enforces bitwise constness. It semantically enforces design and prevents unintended effects later in production. However, you should always program using logical constness. Use it wherever possible in variables, and member functions. It is possible for const member functions to cause code duplication and it is possible to avoid that by calling the const version from the non-const version of the function.

Item 4: Make sure that objects are initialized before they're used

C++ isn't completely reliable when it comes to initialization. Manually initialize built-in types and prefer member initialization list instead of initialization inside the constructor body. Also, make sure to list the data members in the initialization list in the same order of declaration.

That's it for this chapter, next article will be about Chapter 2, Constructors, Destructors, and Assignment Operators