Quantcast
Channel: Bartek's coding blog
Viewing all articles
Browse latest Browse all 325

IIFE for Complex Initialization

$
0
0

IIFE for complex initialization of const variables in C++

How do you initialize your variables, especially the const ones? What do you do when the code for the initialization is complicated? Do you move it to other method or just write inside the current scope?

Intro

I hope you’re initializing most of variables as const (so that the code is more verbose, explicit, and also compiler can reason better about the code and optimize).

It’s easy to write:

constint myParam = inputParam *10+5;

or even:

constint myParam = bCondition ? inputParam*2: inputParam +10;

But what about complex expressions? When we have to use several lines of code, or when the ? operator is not sufficient.

‘It’s easy’ you answer: you can wrap that initialization into a separate function.

While that’s a right answer in most cases, I’ve noticed that in reality a lot of people just writes code in the current scope. That forces you to stop using const and code is a bit uglier. So we have something like:

int myVariable =0;

if(bCondition)
myVariable
= bCond ? computeFunc(inputParam):0;
else
myVariable
= inputParam *2;

// more code of the current function...

I highly suggest wrapping such code into a separate method, but recently I’ve come across a new option.

I’ve got it from a great talk by Jason Turner about “Practical Performance Practices” where among various tips I’ve noticed “IIFE”. This acronym stands for “Immediately-invoked function expression” and thanks to lambdas it’s now available in C++. We can use it for complex initialization of variables. How does it look like?

IIFE

The imaginary code from the previous section could be rewritten to:

constint myVariable =[&]{
if(bCondition)
return bCond ? computeFunc(inputParam):0;
else
return inputParam *2;
}();

// more code of the current function...

We’ve enclosed the original code with a lambda. It takes no parameters but captures the current scope by reference. Also look at the end of the code - there’s () - so we’re invoking the function immediately.

Additionally, since this lambda takes no parameters, we can skip () in the declaration. Only [] is required at the beginning, since it’s the lambda-introducer .

Would you use such thing in your code?

I am a bit skeptical to such expression, but I probably need to get used to it. I wouldn’t use it for a long code. It’s probably better to wrap some long code into a separate method and give it a proper name. But if the code is 2 or three lines long… maybe why not.

One note: regarding generated code you should get the same optimized code no matter if it’s IIFE or static function (or in an anonymous namespace). At least this is what Compiler Explorer is showing me. Basically, the code should be inlined.

Your turn

  • What do you think about such syntax? Have you used it in your projects?
  • Do you have any guidelines about such thing?
  • Is such expression better than having lots of small functions?

BTW: maybe I should ask Java Script guys, since this concept comes from their world mostly :)

If you liked this article, please sign up for my free newsletter.

References

BTW: you can watch the whole Jason’s talk here:

IIFE from ~10:20 (about using const)

And here's another Jason's talk, solely about IIFE


Viewing all articles
Browse latest Browse all 325

Trending Articles