C++ Code Optimization: 10 Tips And Tricks - InformationWeek

InformationWeek is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them.Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.

12:06 PM

C++ Code Optimization: 10 Tips And Tricks

C++ is a great enterprise programming language if you know how to get the most from its arcane features. Here are 10 tips to help you do so, and end up with high-performing applications for your business.
1 of 11

When it comes to enterprise programming, C++ reigns supreme. That's one of the reasons why figuring out how to write the best C++ code possible is a hot topic on the Web. Another reason programmers come together to figure out best practices for C++ programming is the nature of the language itself. C++ gets its underlying structure from C, exists in multiple versions, and is wildly configurable and extensible. It is possible to use those qualities to write compact, efficient, elegant code in C++. It's equally possible to use those qualities to write a resource-devouring mess.

There are a lot of ways to nibble at the problem of bad C++ code. If you don't believe me, a quick Google search will prove the point. But which tips can have a larger impact on code quality? I took a look and came up with 10 that I believe might make a difference in the way your code behaves and the impact it has on the system as a whole.

[ Just getting started? Read 10 Top Programming Languages For Learning To Code.]

It's important to note that two of the tips in this article could apply to any programming language. They don't even require a computer. The rest, though, assume that you're actually writing some code in C++ and have the ability to compile it into executable code. Two of these are concerned with how you compile your code, leaving six that deal with the code you write.

As I mentioned upfront, these are suggestions that I think will have a real impact on your work. They're among thousands of tips that exist. Once you've reviewed them, tell me whether you think they're some of the best? I'd love to hear your thoughts and exchange some practical information with you in the comments section below. Which tactics and techniques have helped your code? Who knows, maybe you'll be featured in a future "how we code" article right here at InformationWeek.

**New deadline of Dec. 18, 2015** Be a part of the prestigious InformationWeek Elite 100! Time is running out to submit your company's application by Dec. 18, 2015. Go to our 2016 registration page: InformationWeek's Elite 100 list for 2016.

Curtis Franklin Jr. is Senior Editor at Dark Reading. In this role he focuses on product and technology coverage for the publication. In addition he works on audio and video programming for Dark Reading and contributes to activities at Interop ITX, Black Hat, INsecurity, and ... View Full Bio

We welcome your comments on this topic on our social media channels, or [contact us directly] with questions about the site.
1 of 11
Comment  | 
Print  | 
More Insights
Newest First  |  Oldest First  |  Threaded View
User Rank: Apprentice
11/18/2015 | 1:18:32 PM
Some of these tips are outdated in modern C++
As appropiate as many of those tips may have been some time in the past, C++ and its compilers have evolved in the last decades. In fact, I consider some of these tips to be harmful in modern C++, since there are better alternatives. I'll address each tip separately
  1. use delete[] for cleanup: Please don't. C++ has smart pointers in the standard library for half a decade now, and had them in libraries like boost and the TR1 addition for more than half a decade before that. In modern C++, plain new and delete is a code smell, unless you are implementing a smart pointer, container or similar library class. Replace that tip with "use RAII classes". Also: "prefer stack allocation over heap allocation, if applicable."
  2. Minimize temporary objects: Return value optimization and named return value optimization have been around for a looooong time now. A very long time. If you use a compiler that has been released this century, it almost certainly implements those optimizations, so there are no temporary object except in debug builds. In addition, a = b + c is clearer to read which is much more valuable than an attempt at an optimization that does not exist in a piece of code that maybe does not even need it. My replacement tip: "Don't prematurely optimize, use a profiler instead." And, of course, "initialize variables right when you define them, don't define them until you use them." -->  Complex a = b + c;
  3. I agree in general. However, you suggest to first use arrays, then to get comfortable with containers. Containers should be among the first things to get comfortable with, preferably even before learning about raw allocation for arrays (see above). std::vector is a good replacement for C arrays, with little to no overhead.
  4. = != ==: modern compilers have some builtin static analysis, and dedicated static analyzers should be part of every serious build chain. If you use assignment instead of comparison e.g. in conditionals, compilers usually emit warnings, as they do if you do not use the return of a comparison because you meant a assignment. So, instead of being pedantic about "=" symbols use a modern compiler and static analysis to defend against this and many more common errors.
  5. No globals is a good policy. Even if you need communications between functions - prefer passing that communication explicitly via arguments rather than implicitly via globals.
  6. Nothing to add - except that static analyzers often catch misused postfix increment/decrement.
  7. Replace "makefile" with "build tool" here - there are lots of them, and many have an less clumsy syntax and are easier to use than make. Apart from that, I definitely agree.
  8. Yes, compile on different compilers.
  9. Flowcharts: I generally agree, although it sounds a bit low level. Most low level flow should be obvious from the code (refactor if it isn't). For complex high level stuff class diagrams, data flow diagrams etc are a useful tool.
  10. Yes, definitely.

I already added some "replacement tips" that I consider to be worthy of the top ten. One I am missing is "use automated tests". The fastest, safest code is worth nothing if it is wrong.

User Rank: Apprentice
11/16/2015 | 11:00:12 AM
Thanks for the Splendid list of C++ Optimization Tips.
All the tips are amazing and effective to be used in production development environment. Thanks for coming up with such a great post.
2021 Outlook: Tackling Cloud Transformation Choices
Joao-Pierre S. Ruth, Senior Writer,  1/4/2021
Enterprise IT Leaders Face Two Paths to AI
Jessica Davis, Senior Editor, Enterprise Apps,  12/23/2020
10 IT Trends to Watch for in 2021
Cynthia Harvey, Freelance Journalist, InformationWeek,  12/22/2020
White Papers
Register for InformationWeek Newsletters
The State of Cloud Computing - Fall 2020
The State of Cloud Computing - Fall 2020
Download this report to compare how cloud usage and spending patterns have changed in 2020, and how respondents think they'll evolve over the next two years.
Current Issue
2021 Top Enterprise IT Trends
We've identified the key trends that are poised to impact the IT landscape in 2021. Find out why they're important and how they will affect you.
Flash Poll