Part 2 - Almost Always Auto & brace initialization
Almost Always Auto
When declaring anything use Almost Always Auto (AAA) - unless it changes the meaning of the code use auto
. It prevents you from leaving uninitialized variables as well as makes your code cleaner.
Example: simple variables
// int
auto a = 7;
// float
auto b = 7.0;
// Animal
auto c = Animal{};
Example: for loops
// for loop
for (auto d = 0; i < 10; c++);
// ranged for loop
for (auto e : storage);
// iterator for loop
for (auto it = storage.begin(); it != storage.end(); ++it);
Example: functions
Note: this one is hard to get used to and even I often break this rule.
// trailing return type with auto
auto function(const Animal& arg) -> bool;
Brace initialization
When initializing object use "brace initialization" (property called "uniform initialization")
// explicit type
auto a = int{3};
// default constructor
auto b = Animal{};