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.

IoT
IoT
Software
News
11/15/2015
12:06 PM
50%
50%

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.
Previous
1 of 11
Next

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.
Previous
1 of 11
Next
Comment  | 
Print  | 
More Insights
Comments
Newest First  |  Oldest First  |  Threaded View
arne_mertz
100%
0%
arne_mertz,
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.

 
TechBeamers
100%
0%
TechBeamers,
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.
Slideshows
What Digital Transformation Is (And Isn't)
Cynthia Harvey, Freelance Journalist, InformationWeek,  12/4/2019
Commentary
Watch Out for New Barriers to Faster Software Development
Lisa Morgan, Freelance Writer,  12/3/2019
Commentary
If DevOps Is So Awesome, Why Is Your Initiative Failing?
Guest Commentary, Guest Commentary,  12/2/2019
White Papers
Register for InformationWeek Newsletters
State of the Cloud
State of the Cloud
Cloud has drastically changed how IT organizations consume and deploy services in the digital age. This research report will delve into public, private and hybrid cloud adoption trends, with a special focus on infrastructure as a service and its role in the enterprise. Find out the challenges organizations are experiencing, and the technologies and strategies they are using to manage and mitigate those challenges today.
Video
Current Issue
Getting Started With Emerging Technologies
Looking to help your enterprise IT team ease the stress of putting new/emerging technologies such as AI, machine learning and IoT to work for their organizations? There are a few ways to get off on the right foot. In this report we share some expert advice on how to approach some of these seemingly daunting tech challenges.
Slideshows
Flash Poll