This rule is very obvious. Instead of
1 | std::string encryptPassword(const std::string& password) |
We should define encrypted
right before we need to use it
1 | std::string encryptPassword(const std::string& password) |
Another common question is which way is better looking at following examples of doing for
loop
1 | // example 1 |
The answer is always use the second approach unless performance is critical to your application. If performance is your main requirement, you need to compare which one is cheaper to execute between 1 constructor + 1 destructor + n assignments
for example 1 and n constructors + n destructors
for example 2.