Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

IIFE for Complex Initialization

DZone's Guide to

IIFE for Complex Initialization

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?

· Web Dev Zone
Free Resource

Learn how to build modern digital experience apps with Crafter CMS. Download this eBook now. Brought to you in partnership with Crafter Software

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;elsereturn 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 :)

References

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

IIFE from ~10:20 (about using const)

Crafter is a modern CMS platform for building modern websites and content-rich digital experiences. Download this eBook now. Brought to you in partnership with Crafter Software.

Topics:
java ,method ,function ,performance ,web dev

Published at DZone with permission of Bartłomiej Filipek, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}