DZone
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
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report

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 · Tutorial
Like (0)
Save
Tweet
Share
2.27K 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

  • Introduction to Containerization
  • Implementing PEG in Java
  • File Uploads for the Web (2): Upload Files With JavaScript
  • Demystifying the Infrastructure as Code Landscape

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • 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: