How to Stay Sane With Modern C++
How to Stay Sane With Modern C++
C++ conjures up a lot of feelings for folks... some good, some bad. Here, Bartlomiej Filipek does his best to sum up the pros and cons for us, while explaining its evolution.
Join the DZone community and get the full member experience.Join For Free
Bugsnag monitors application stability, so you can make data-driven decisions on whether you should be building new features, or fixing bugs. Learn more.
Have you seen my recent blog post with the list of C++17 features? This is probably not the best measurement, but it got only ~30% of the average read (while other articles might get like 70, or even 90%). I am not complaining, the list is crazy and contains probably too many details of the new standard.
Another stat: C++ standard page count: from 879 pages for C++98/03 to 1586 for C++17 (draft)!
Do you need to learn all of that stuff to write good code?
How to stay sane in the C++ world today?
You probably know that C++ is a complex language. As I’ve found, there’s even a whole Wiki page about the criticism of Cpp. Modern C++ adds even more stuff to the package!
Here’s the full data about the page count in the specs that I’ve mentioned before:
- C++98/03 - 879, N1905, Oct 2005
- C++11 - 1324, last draft, N3337, Jan 2012
- C++14 - 1368, last draft, Nov 2014
- C++17 - 1586, draft, N4606
It looks like C++17 is almost ~80% ‘larger’ than C++98/03. You can complain about added complexity and that it’s hard to learn all of those things. But, is this so terrible? What can you do about the whole situation?
This post was motivated by some stories recently found:
- HN: C++11 FAQ
- r/programming, lvalues and rvalues used to be simple things. Now, only a handful of people truly understand all facets of C++ value categories.
- HN: Why I don’t spend time with Modern C++ anymore | Hacker News
- r/programming, Modern C++ Features – decltype and std::declval
First, let’s see some problems that you might bump into in C++...
Just to name a few:
Too Slow Pace
In 2016, as I wrote in my summary post, we got the draft for C++17. While it’s great, that we get a new standard every three years, a lot of developers complained that the new standard is not what everyone waited for.
A lot of features: like concepts, modules, ranges, co-routines, … were not accepted and we need to wait at least three more years to get them in the spec.
So, for some features, the pace of standardization is very slow.
As a positive aspect, I can only say that most of the mentioned features are already implemented, at least in the experimental form. So you can play with them and be prepared for the final version. Take a look at GCC concepts, modules in VS or Clang, Ranges, etc.
Too Fast Pace
As usual, we might have two contradicting opinions here. Although for some people the pace is slow, for others it’s hard to keep up with the changes.
You’ve just learned C++11… and now you need to update the knowledge with C++14, and then C++17 is along the way. Three years is not that short time, but bear in mind the compiler conformance, company policies, team guidelines might walk at a different pace.
Do your companies update to the most modern C++ version immediately?
Confusion / Complexity
Just read that comment:
I love c++. It’s my go to language, but you have to admit its ‘hodge podge’ implementation of value types is bizarre at best. Most programmers including me prefer simple well-defined language constructs over bizarre and over complicated grammar. I’m not a computer language lawyer. I’m a programmer.
Is C++ clear in every part? Probably not…
The principle of move semantics are quite clear: instead of copying just try to exchange the pointers to allocated memory, and you should get a nice performance boost. But the devil is in the detail.
I don’t write a lot of generic code, so fortunately I don’t have to think about move semantics all the time. But I was quite confused when I bumped into move and const - see my last article on that. I don’t believe every C++ will understand the rules here. Especially that you now need to remember about 6 default operations generated by the compiler: default constructor, destructor, copy constructor, move constructor, assign operator, and move assignment operator.
Rvalues/xvalues/prvalues… myValues, fooValues
The last ones are made up… but still having all of the value categories is overwhelming!
Previously, you just had to know lvalue vs rvalue, now it’s a bit more subtle.
Still, the question is if you need to know it by hard on a daily basis?
Some good comments:
It is complicated, but not on a daily basis.
Can this value be addressed ? can it be copied ? can it be moved ? Should it be moved ?
There is very few situation where you want to actively to be very specific and need a full understanding. ( templated library writing, hot paths, etc).
Most of the time C++ is not more complicated than java or something. Sadly this is lost on most people. C++ may be the most complex language out there but you can write very good code without caring about the specific.
BigObject o = getBigObject();
I was quite lost when I saw all the changes for C++17; there are so many details about templates deduction!
Although you might not write template code all the time (unless you’re a library developer), it’s good to remember that deduction rules now also apply on ‘auto’ type deduction since we have AAA (Almost Always Auto) rule.
What are your main problems with the language?
So far, we’ve discussed some problems, so how to live with them… and possibly how to solve them?
How to Stay Sane
There’s no the perfect programming language, every one of them has some issues. Here are my suggestions how to cope with the problems of Modern C++.
Stay Positive, the Language Is Evolving
No one wants to write code using old tools. We’ve already seen a lot of complaints about old C++ before C++11. It took almost 13 years (counting from major C++98, not including minor C++03) to came up with the major version: C++11. Now we can be happy that we get back on track, and every three years there will be some changes. At the end of the day, you cannot say that your language is dead and old.
And the Tools as Well!
Thanks to Clang and also improved development speed in other platforms, we get tools like:
- Clang Tidy (previously clang- modernize)
- Clang Format
- Clang Static Analyzer
- Clion/Resharper C++
- VisualStudio - tools like C++ Core Checker
- PVS Studio
While it’s not super great as for other languages (especially Java based or .NET based), it’s getting better and better. Bear in mind that because of the complex C++ grammar it’s very hard to implement tools that analyze the code on the fly.
Try to Stay Up to Date
The C++ community is very much alive. There are many blogs, books, conferences… and there’s even a chance a local community is in your city!
For a start, I'd suggest going to isocpp.org the central place for all of the events/news/articles. Then you might check Meeting C++ and info about local C++ groups. There’s also reddit/cpp where constantly more and more good stuff is posted.
And remember books like:
- The C++ Programming Language, 4th Edition 4th Edition
- Effective Modern C++
- Programming: Principles and Practice Using C++
You can also take a look at my recent C++17 Lang Ref Card!
Too Much Details? Just Don’t Open the Hood
One of the reasons C++ has so much power is that it allows you to implement code very close to the metal. You have control over all of the details, memory layout, performance optimizations, etc, etc… At the same time, such abilities increase the complexity of the language.
Still, if you don’t need to go that far, you can stay at a relatively higher level of abstraction.
Use What You Need
C++ is a multi-paradigm language; you can use it many different ways. Recently, I’ve read an interesting comment that said that a Cpp programmer might for years do very well without touching advanced stuff like template metaprogramming or even exceptions. This heavily depends on the code style of the project.
Even such companies like Google limit features of C++, for example, they don’t use exceptions.
This is a bit of repetition, but if you’re not a library developer you might not get into troubles with custom move operators or move constructors. Similarly, advances metaprogramming stuff might also not be a crucial part of your code.
If you start from scratch or have a small code base then going to C++11/14 should be relatively easy. What about million-line of code, code that was started 20 years (or more!) ago?
Just do it step by step.
At least for the new code, you should start using Modern C++. Moreover, by applying “The Boy Scout Rule” you can improve surrounding code that you touch.
This will probably result in some mixed code, but still, it’s better than staying with the legacy style only.
Last Resort: Your Old Code Will Still Compile!
One of the reasons why C++ specs are getting larger and larger is that the language is backward compatible. So the committee usually introduces new features, but rarely removes the old stuff. So… your code can still compile. If you don’t want to move and use newer things then you can stay with your current style.
From time to time, you’ll get some warnings about deprecated stuff or removed features (like
auto_ptr in C++17), but even in that case, you can switch the compiler to target some older C++ standard.
This article is partially a rant, partially a ‘glorification.’ I try to see the bad sides of the language and its evolution process and some positive signs as well.
While we can complain about the complexity, pace of changes, etc… I don't think we can say that the language is dead. That’s a good thing.
I don’t think you have to rapidly chase the new features and immediately rewrite your existing code. Just try to stay up to date with news, use the features that really improves your work and gradually your code should improve and be more ‘modern’ (however, can that be defined - see meetingcpp article on that).
- What’s your approach when adopting new features from C++11/14/17/1z ?
- What’s your main problem with C++?
- Do you use modern C++ in your work?
Published at DZone with permission of Bartłomiej Filipek , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.