DZone
Web Dev Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Web Dev Zone > IIFE for Complex Initialization

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?

Bartłomiej Filipek user avatar by
Bartłomiej Filipek
·
Nov. 23, 16 · Web Dev Zone · Tutorial
Like (0)
Save
Tweet
2.11K Views

Join the DZone community and get the full member experience.

Join For Free

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
  • IIFE
  • Your turn
  • References

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)

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Python 101: Equality vs. Identity
  • Kafka vs. JMS: Which One Should You Be Using?
  • Everything You Need to Know About Building MVP With React and Firebase
  • How to Classify NSFW (Not Safe for Work) Imagery with AI Content Moderation using Java

Comments

Web Dev Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo