Clarify uses of immediately invoked lambdas vs ?:

Issue #6
This commit is contained in:
Jason Turner
2015-05-19 16:19:17 -06:00
parent 99b4c7bcb5
commit 7e68f7ed00

View File

@@ -140,30 +140,53 @@ shared_ptr objects are much more expensive to copy than you think they should be
### Reduce Copies and Reassignments as Much as Possible
This can be facilited in some cases with an [immediate-invoked lambda](http://blog2.emptycrate.com/content/complex-object-initialization-optimization-iife-c11).
For more simple cases, the ternary operator can be used
```c++
// Bad Idea
std::string somevalue;
if (somecase()) {
if (caseA) {
somevalue = "Value A";
} else {
somevalue = "Value B";
}
```
```c++
// Better Idea
const std::string somevalue = caseA?"Value A":"Value B";
```
More complex cases can be facilited with an [immediately-invoked lambda](http://blog2.emptycrate.com/content/complex-object-initialization-optimization-iife-c11).
```c++
// Bad Idea
std::string somevalue;
if (caseA) {
somevalue = "Value A";
} else if(caseB) {
somevalue = "Value B";
} else {
somevalue = "Value C";
}
```
```c++
// Better Idea
const std::string somevalue = [&](){
if (somecase()) {
if (caseA) {
return "Value A";
} else {
} else if (caseB) {
return "Value B";
} else {
return "Value C";
}
}();
```
### Avoid Excess Exceptions
Exceptions which are thrown and captured internally during normal processing slow down the application execution. They also destroy the user experience from within a debugger, as debuggers monitor and report on each exception event. It is best to just avoid internal exception processing when possible.