4 ms·
I have a question. I often see titles of blog posts or books such as "Learn Modern C++" or "Learn Modern Java" but how possible or practical is this for a newco
by bogomipz 10y ago
I have a question. I often see titles of blog posts or books such as "Learn Modern C++" or "Learn Modern Java" but how possible or practical is this for a newcomer when the languages have been around this long?
For instance what happens if you opt for the strictly "modern" just Java 8 or C++17 type approach to learning these. And then you end up as the case in the real world needing to maintain legacy code? Or you collaborate with people who have been using the language for a long time but don't use these modern approaches?
- pjmlp 10y agoYou cry, and use them just on your own private projects. Additionally keep looking for opportunities to sneak in some features, or pushing for toolchain updates.
- 35bge57dtjku 10y ago> Or you collaborate with people who have been using the language for a long time but don't use these modern approaches? That's always going to be a problem, but that's no reason to not learn the latest versions. > And then you end up as the case in the real world needing to maintain legacy code? It's not like learning the newer ways of doing things prevents you from learning everything else.
- khedoros1 10y agoThe point of learning the "Modern" style is so that the newer, safer features are the ones you think of when writing new code. No matter which style you're first introduced to, you're going to end up coming into contact with a lot of different programming styles. Given that you may have to learn them all eventually, and that you've got to start somewhere, doesn't it make sense to introduce students to the features of the language that try to correct past mistakes?
- millstone 10y agoA lot of what is considered modern C++ uses very advanced ideas. See this function from GOTW 94 [1], which Herb Sutter references approvingly, calling it "easy:" template<class Container, class Value> void append_unique( Container& c, const Value& v ) { if( find(begin(c), end(c), v) == end(c) ) c.emplace_back(v); assert( !c.empty() ); } Sutter likes this because it's written against an (conceptual) interface "Container", not an implementation like std::vector. He brags that not a single specific type appears anywhere in the code, making it much more powerful. This is modern C++, and it is totally inaccessible to a newcomer. It hits you with templates, iterators, and several generic STL functions. Call it slightly wrong, and you'll get an avalanche of impenetrable error messages. Dig a little ("what does emplace_back do?") and you're confronted with allocator traits, placement new, perfect forwarding, etc. Modern C++ is just not accessible to newcomers. The same thing in C (non-generically) would use concrete types and a for loop. 1: https://herbsutter.com/2013/08/12/gotw-94-solution-aaa-style-almost-always-auto/ https://herbsutter.com/2013/08/12/gotw-94-solution-aaa-style...
- eropple 10y agoI've never written code like that, nor am I a C++ guru by any stretch (maybe I've written two hundred total hours of C++ in the last five years, and none before that), but that code makes complete sense to me at a brief scan. And, sure, it's not the simplest way to do it, but those simplest ways exist (and are superior to the way you'd do it in C)--I mean, the same thing in reasonable, idiomatic C++ would use a for loop and an iterator, and Sutter wouldn't yell at you for it. He might hope that eventually you become comfortable enough with templates to not be freaked out by that piece of code, but he wouldn't be mad at it. It could be that I have experience with other statically typed languages, or that I have spent time internalizing why C++ works (from a conceptual, hands-off perspective) as opposed to just how, but...that seems reasonable to me. I probably wouldn't write it that way because I don't have code that relies on that kind of flexibility, but it's quite comprehensible. If your complaint is merely that C++ expects you to know stuff to read it...I mean...yeah, sure, it does, but if that's a problem we have deeper rot as professionals to deal with. And it is a problem, given how much we venerate C while it keeps on being wholly unsafe at any speed. But, hey. Some of us need buffer overflows to stay employed, I guess.
- millstone 10y agoYou can read code in most languages and guess at what it does. But that doesn't mean you can write it - or debug it. I'm not complaining. Herb Sutter has his arguments for why experts should write code this way. But I don't think newcomers should attempt to write in this way. You can't understand iterators before you understand pointers, or templates before concrete types and functions, or emplace_back before push_back, or Concepts before classes, or auto before concrete types. You may be able to use them, but you won't understand them, and you'll be useless when things go wrong. One last illustration: you mention your experience with statically typed languages. But C++ templates provide much less static type checking than non-template code. It is definitely not a place to start.
- eropple 10y agoC++ templates provide type checking exactly within their bounds, which this particular example illustrates, so...well, what? Posit for a second that you are correct. I think you're not, but let's posit that you are. And then I just think...so what? I don't mean that to be flip: what's the point of your post if it's not to air complaints that "newcomers might have trouble"? I mean, yeah, all things being equal, approachability is nice--but newcomers have trouble with chainsaws, flying planes, and 240V wiring. Hard things are hard. C++ is hard. It's why it's a tool of last resort, if you are wise. If you're not, nothing's gonna help you, yeah?
- aidenn0 10y ago"Modern C++" at least partly refers to generic programming techniques that were popularized by Alexandrescu's book "Modern C++ Design"; a lot of the features in C++11 improve language support for such techniques. The term also implies use of RAII[1] which is a technique that was used at least a decade before C++ was an ISO standard. The third implication is a rejection of what is often called "C+" or "C with classes" which is the treatment of C++ as C with a couple extra features. Since a lot of early C++ programmers were C programmers, and a lot of early C++ compilers were fairly mediocre in their implementation of language features[3], the 90s generated a lot of "C+" code. Really the first two points are a subset of this one as they are two prominent examples of programming techniques that are fairly foreign to C, but are extremely valuable for writing robust C++ code. So "Modern C++" really means "Something widely considered a good idea for about a decade" at this point. If you work on a good legacy code base, the typical old oddness will be "Version X of MSVC/GCC/whatever had poor RVO[2], so we did this ugly thing for optimization" and (obviously) either a complete lack of using the features in C++11 or using a proto-version of them via Boost. Even the nicest legacy code bases will have really odd things whose original motivation would require serious archaeology to divine, but that happens even with relatively static languages. 1: https://en.wikipedia.org/wiki/Resource_acquisition_is_initialization https://en.wikipedia.org/wiki/Resource_acquisition_is_initia... 2: https://en.wikipedia.org/wiki/Return_value_optimization https://en.wikipedia.org/wiki/Return_value_optimization 3: Believe it or not, a lot of the code from Alexandrescu's book could not compile in either MSVC or GCC when it was published. The library that accompanied the book became incorporated into many compiler vendor's conformance testing.
- bogomipz 10y agoThese were all great comments thanks. Would people still suggest Alexandrescu's book "Modern C++ Design" as the best source for newcomers to learn modern C++ then?
- pjmlp 10y agoIt is one source yes. But you should also have a look at "Tour of C++" and "Elements of Programming". Also Microsoft has lots of Modern C++ talks on Channel 9 done by Stephan T Lavavej. https://channel9.msdn.com/tags/STL/ https://channel9.msdn.com/tags/STL/